auth

package module
v0.0.0-...-9f56b5f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2018 License: Apache-2.0 Imports: 14 Imported by: 1

README

auth

Generic Server Authentication Helper

Documentation

Index

Constants

View Source
const (
	ErrInvalidToken  = errors.Error("invalid token")
	ErrMissingID     = errors.Error("missing id")
	ErrInvalidLogin  = errors.Error("invalid login")
	ErrNoPassword    = errors.Error("the password is empty")
	ErrNoID          = errors.Error("invalid id")
	ErrUserExists    = errors.Error("user already exists")
	ErrUserNotFound  = errors.Error("user not found")
	ErrBadStatus     = errors.Error("bad status")
	ErrNewUserWithID = errors.Error("a new user can't have an id set")
	ErrPlainPassword = errors.Error("plain password")
)

common errors returned from the library.

View Source
const (

	// BCryptRounds the default number of rounds passed to bcrypt.
	BCryptRounds = 11
)
View Source
const ErrProfileNotPtr = errors.Error("profile must be a pointer")

ErrProfileNotPtr is returned froom UnmarshalUser if profle is not nil and is not a pointer.

Variables

This section is empty.

Functions

func CheckPassword

func CheckPassword(hash string, password string) bool

CheckPassword checks a hashed password against a plain-text password.

func EditUserTx

func EditUserTx(tx turtleDB.Txn, id string, fn func(u *User) error) (err error)

EditUserTx is a helper func for Auth.EditUser.

func GetUserIDTx

func GetUserIDTx(tx turtleDB.Txn, username string) (string, error)

GetUserIDTx is a helper func for Auth.GetUserID.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a password using bcrypt and returns the string representation of it.

func IsHashedPass

func IsHashedPass(hash string) bool

IsHashedPass checks if a password hash is a valid bcrypt hash or not.

func RandomToken

func RandomToken(ln int, b64 bool) string

RandomToken returns a random `string` crypto/rand generated token with the given length. If b64 is true, it will encode it with base64.RawURLEncoding otherwise uses hex.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth is a generic user authentication helper.

func New

func New(path string) (*Auth, error)

New returns a new Auth db at the specificed path.

func NewEncrypted

func NewEncrypted(path string, key, iv []byte) (*Auth, error)

NewEncrypted returns a new Auth db that is encrypted with the specified key/iv. if key is nil, it returns a non-encrypted store.

func (*Auth) Close

func (a *Auth) Close() error

Close closes the underlying database.

func (*Auth) CreateUser

func (a *Auth) CreateUser(username, password string) (id string, err error)

CreateUser will add the passed user to the database and hash the given password. the passed user will be modified with the hashed password and the new ID.

func (*Auth) CreateUserWithID

func (a *Auth) CreateUserWithID(id string, username, password string) (uid string, err error)

CreateUserWithID will add the passed user to the database and hash the given password as the provided ID

func (*Auth) EditUserByID

func (a *Auth) EditUserByID(id string, fn func(u *User) error) error

EditUserByID edits a user by their ID, returning an error will cancel the edit.

func (*Auth) EditUserByName

func (a *Auth) EditUserByName(username string, fn func(u *User) error) error

EditUserByName edits a user by their username, returning an error will cancel the edit.

func (*Auth) ForEach

func (a *Auth) ForEach(fn func(User) error) (err error)

ForEach will iterate through each of the users

func (*Auth) GetUserByID

func (a *Auth) GetUserByID(id string) (u User, err error)

GetUserByID returns a User by their ID.

func (*Auth) GetUserByName

func (a *Auth) GetUserByName(username string) (u User, err error)

GetUserByName returns a User by their UserName.

func (*Auth) NewProfileFn

func (a *Auth) NewProfileFn(fn func() interface{})

NewProfileFn is used on loading users from the database to fill in the User.Profile field. it is 100% optional

type Status

type Status int8

Status represents different user statuses

const (
	StatusActive Status
	StatusInactive
	StatusBanned
)

Status values.

type User

type User struct {
	ID string `json:"id,omitempty"`

	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"`

	Status Status `json:"status,omitempty"`

	CreatedTS     int64 `json:"created,omitempty"`
	LastUpdatedTS int64 `json:"lastUpdated,omitempty"`

	Profile interface{} `json:"profile,omitempty"`
}

User is a system user.

func GetUserByIDTx

func GetUserByIDTx(tx turtleDB.Txn, id string) (usr User, err error)

GetUserByIDTx is a helper func for Auth.GetUserByID.

func GetUserByNameTx

func GetUserByNameTx(tx turtleDB.Txn, username string) (usr User, err error)

GetUserByNameTx is a helper func for Auth.GetUserByName.

func UnmarshalUser

func UnmarshalUser(b []byte, profile interface{}) (*User, error)

UnmarshalUser attempts to unmarshal json with the optional Profile field and returns the *User. if profile is NOT nil, it must be a pointer.

func (*User) Created

func (u *User) Created() time.Time

Created returns the creation time of the user.

func (*User) LastUpdated

func (u *User) LastUpdated() time.Time

LastUpdated returns the time of the last user update, if it was never updated it will return the creation time.

func (*User) PasswordsMatch

func (u *User) PasswordsMatch(plainPassword string) bool

PasswordsMatch returns true if the current user's hashed password matches the plain-text password.

func (*User) UpdatePassword

func (u *User) UpdatePassword() error

UpdatePassword checks if the password is hashed, if not it will hash it and assign the hashed password.

func (*User) Validate

func (u *User) Validate() error

Validate checks if the User struct is valid or not.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL