auth

package
v0.0.0-...-ea08b5a Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2017 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DBTableUsers              = "auth_users"
	DBTableUsersUsernameIndex = "username"
)
View Source
const (
	// AdminGroup is the administration group.
	AdminGroup = "admin"
)
View Source
const (
	// ModuleName specifies the BitMonster module name.
	ModuleName = "auth"
)

Variables

View Source
var (
	ErrEmptyResult           = errors.New("empty result")
	ErrUserNotFound          = errors.New("user not found")
	ErrUsernameAlreadyExists = errors.New("a user with the username already exists")
)
View Source
var (
	ErrNotAuth = errors.New("not authenticated")
)

Functions

func AddUser

func AddUser(u *User) error

AddUser adds a new user to the database. Returns ErrUsernameAlreadyExists if the username already exists.

func ChangeUsername

func ChangeUsername(u *User, newUsername string) error

ChangeUsername changes the username of the user in the database. Returns ErrUsernameAlreadyExists if the username already exists. Hint: This implementation is currently not atomic if multiple BitMonster instances are used to provide the backend service. This is due to limitations of the RethinkDB database. See TODO. See: https://github.com/rethinkdb/rethinkdb/issues/1716

func DeleteUser

func DeleteUser(u *User) error

DeleteUser removes a user from the database.

func Groups

func Groups() []string

Groups returns a slice of all registered groups.

func IsAuth

func IsAuth(s *bitmonster.Socket) bool

IsAuth returns a boolean whenever the socket is authentication by a user.

func MustAdminGroup

func MustAdminGroup() bitmonster.Hook

MustAdminGroup returns a BitMonster Hook which requires an authenticated user who is member of the admin group.

func MustGroup

func MustGroup(groups ...string) bitmonster.Hook

MustGroup returns a BitMonster Hook which requires an authenticated user who is member of one of the passed groups.

func MustIsAuth

func MustIsAuth() bitmonster.Hook

MustIsAuth returns a BitMonster Hook which requires an authenticated user session.

func OffDeleteUser

func OffDeleteUser(f OnDeleteUserFunc)

OffDeleteUser unbinds the event function again.

func OffNewUser

func OffNewUser(f OnNewUserFunc)

OffNewUser unbinds the event function again.

func OnDeleteUser

func OnDeleteUser(f OnDeleteUserFunc)

OnDeleteUser triggers the event function if an user was deleted by the current BitMonster server instance. To listen for all database change events of all running instances, use the changefeeds package.

func OnNewUser

func OnNewUser(f OnNewUserFunc)

OnNewUser triggers the event function if an user was added by the current BitMonster server instance. To listen for all database change events of all running instances, use the changefeeds package.

func RegisterGroup

func RegisterGroup(name string) error

RegisterGroup registers a new group. This method should only be called during the initialization process.

func UpdateUser

func UpdateUser(u *User) error

UpdateUser updates a user in the database. The user fields replace all fields of the document stored in the database. Don't call this function if the username of the user was changed. Instead use ChangeUsername to change usernames.

Types

type AuthSession

type AuthSession struct {
	Fingerprint  string    `gorethink:"fingerprint"`
	Token        string    `gorethink:"token"`
	TokenCreated time.Time `gorethink:"tokenCreated"`
	Created      time.Time `gorethink:"created"`
	LastAuth     time.Time `gorethink:"lastAuth"`
}

type AuthSessions

type AuthSessions map[string]*AuthSession

type OnDeleteUserFunc

type OnDeleteUserFunc func(user *User)

type OnNewUserFunc

type OnNewUserFunc func(user *User)

type User

type User struct {
	ID       string   `gorethink:"id"        json:"id"           valid:"uuidv4,required"`
	Username string   `gorethink:"username"  json:"username"     valid:"printableascii,length(3|50),required"`
	Name     string   `gorethink:"name"      json:"name"`
	Email    string   `gorethink:"email"     json:"email"        valid:"email,length(3|100),required"`
	Enabled  bool     `gorethink:"enabled"   json:"enabled"`
	Groups   []string `gorethink:"groups"    json:"groups"`

	Created   time.Time `gorethink:"created"    json:"created"`
	LastLogin time.Time `gorethink:"lastLogin"  json:"lastLogin"`

	AuthSessions AuthSessions `gorethink:"authSessions"    json:"-"`
	PasswordHash string       `gorethink:"passwordHash"    json:"-"`
}

func CurrentUser

func CurrentUser(s *bitmonster.Socket, enableCache ...bool) (*User, error)

CurrentUser returns the current authenticated user of the socket session. If the socket session is not authenticated, then ErrNotAuth is returned. Optionally pass one variadic boolean to enable caching. If caching is enabled, multiple calls to this method will return the cached user value instead of always obtaining the value from the database. Note: The cached value might be out-of-date. Don't enable the cache if the returned user value is used to be written to the database.

func GetUser

func GetUser(id string) (*User, error)

GetUser obtains a user by its ID. Returns a ErrUserNotFound error if the user does not exists.

func GetUserByUsername

func GetUserByUsername(username string) (*User, error)

GetUserByUsername obtains a user by its username. Returns a ErrUserNotFound error if the user does not exists.

func NewUser

func NewUser(username, name, email, password string) (*User, error)

NewUser creates a new user value.

func (*User) AddGroup

func (u *User) AddGroup(groups ...string)

AddGroup adds the user to the passed group(s).

func (*User) ChangePassword

func (u *User) ChangePassword(password string) (err error)

ChangePassword changes a user's password.

func (*User) ClearAuthSessions

func (u *User) ClearAuthSessions()

ClearAuthSessions removes all current authenticated sessions of the user. This will force an overall relogin.

func (*User) ComparePasswords

func (u *User) ComparePasswords(password string) (match bool)

ComparePasswords compares the passed password with the user's password hash. This method is safe against timing attacks.

func (*User) HasGroup

func (u *User) HasGroup(group string) bool

HasGroup checks if the user is member of the group.

func (*User) RemoveGroup

func (u *User) RemoveGroup(groups ...string)

RemoveGroup removes the user from the group(s).

func (*User) Validate

func (u *User) Validate() error

Validate the struct fields.

type Users

type Users []*User

func GetUsers

func GetUsers(groups ...string) (Users, error)

GetUsers obtains all users from the database. Optionally pass groups. If the user is at least in one of the passed groups it is added to the result slice. Returns ErrEmptyResult if no users are found. Don't use this method for setups with a large user database.

Directories

Path Synopsis
Package changefeeds provides the database change events for the auth package.
Package changefeeds provides the database change events for the auth package.

Jump to

Keyboard shortcuts

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