Documentation ¶
Index ¶
- Constants
- func CheckPassword(hash string, password string) bool
- func EditUserTx(tx turtleDB.Txn, id string, fn func(u *User) error) (err error)
- func GetUserIDTx(tx turtleDB.Txn, username string) (string, error)
- func HashPassword(password string) (string, error)
- func IsHashedPass(hash string) bool
- func RandomToken(ln int, b64 bool) string
- type Auth
- func (a *Auth) Close() error
- func (a *Auth) CreateUser(username, password string) (id string, err error)
- func (a *Auth) CreateUserWithID(id string, username, password string) (uid string, err error)
- func (a *Auth) EditUserByID(id string, fn func(u *User) error) error
- func (a *Auth) EditUserByName(username string, fn func(u *User) error) error
- func (a *Auth) ForEach(fn func(User) error) (err error)
- func (a *Auth) GetUserByID(id string) (u User, err error)
- func (a *Auth) GetUserByName(username string) (u User, err error)
- func (a *Auth) NewProfileFn(fn func() interface{})
- type Status
- type User
Constants ¶
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.
const (
// BCryptRounds the default number of rounds passed to bcrypt.
BCryptRounds = 11
)
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 ¶
CheckPassword checks a hashed password against a plain-text password.
func EditUserTx ¶
EditUserTx is a helper func for Auth.EditUser.
func GetUserIDTx ¶
GetUserIDTx is a helper func for Auth.GetUserID.
func HashPassword ¶
HashPassword hashes a password using bcrypt and returns the string representation of it.
func IsHashedPass ¶
IsHashedPass checks if a password hash is a valid bcrypt hash or not.
func RandomToken ¶
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 NewEncrypted ¶
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) CreateUser ¶
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 ¶
CreateUserWithID will add the passed user to the database and hash the given password as the provided ID
func (*Auth) EditUserByID ¶
EditUserByID edits a user by their ID, returning an error will cancel the edit.
func (*Auth) EditUserByName ¶
EditUserByName edits a user by their username, returning an error will cancel the edit.
func (*Auth) GetUserByID ¶
GetUserByID returns a User by their ID.
func (*Auth) GetUserByName ¶
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 ¶
GetUserByIDTx is a helper func for Auth.GetUserByID.
func GetUserByNameTx ¶
GetUserByNameTx is a helper func for Auth.GetUserByName.
func UnmarshalUser ¶
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) LastUpdated ¶
LastUpdated returns the time of the last user update, if it was never updated it will return the creation time.
func (*User) PasswordsMatch ¶
PasswordsMatch returns true if the current user's hashed password matches the plain-text password.
func (*User) UpdatePassword ¶
UpdatePassword checks if the password is hashed, if not it will hash it and assign the hashed password.