auth

package
v0.0.0-...-b5a4b96 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2016 License: LGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package auth implements methods to handler users activities.

Index

Constants

View Source
const (
	// Anonymous is a name of anonymous user.
	Anonymous = "anonymous"
	// Administrator is a name of administrator user.
	Administrator = "admin"
)

Variables

View Source
var (
	// AnonUser is anonymous user.
	AnonUser = &User{Name: Anonymous}
	// ErrAnonymous is error of anonymous authentication
	ErrAnonymous = errors.New("anonymous request")
)

Functions

func Authenticate

func Authenticate(ctx context.Context) (context.Context, error)

Authenticate checks user's authentication. It doesn't validate user's token value and doesn't detect anonymous request as error, so this should be identified before. It writes User pointer to new context.

func CheckToken

func CheckToken(ctx context.Context, r *http.Request, api bool) (context.Context, error)

CheckToken verifies the token, checks length and hash value. If the token is valid, then its 2nd part (hash) will be added to the returned context. It also marks empty token as ErrAnonymous error.

func EqualBytes

func EqualBytes(x, y []byte) bool

EqualBytes compares two byte slices. It is crypto-safe, because successful and unsuccessful attempts have around a same duration time.

func ExtractTokenKey

func ExtractTokenKey(ctx context.Context) (string, error)

ExtractTokenKey extracts user's from from context.

func InitUsers

func InitUsers(c *conf.Config) error

InitUsers initializes admin and anonymous users.

Types

type User

type User struct {
	Name     string    `bson:"_id"`
	Disabled bool      `bson:"off"`
	Token    string    `bson:"token"`
	Roles    []string  `bson:"roles"`
	Modified time.Time `bson:"mt"`
	Created  time.Time `bson:"ct"`
}

User is structure of user's info.

func ExtractUser

func ExtractUser(ctx context.Context) (*User, error)

ExtractUser extracts user from from context.

func (*User) HasRole

func (u *User) HasRole(role string) bool

HasRole check user has a requested role.

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

IsAnonymous returns true if user is anonymous.

func (*User) String

func (u *User) String() string

String returns user's name

type UserResult

type UserResult struct {
	U    *User
	Name string
	T    string
	Err  string
}

UserResult is result of user creation.

func ChangeUsers

func ChangeUsers(ctx context.Context, names []string) ([]UserResult, error)

ChangeUsers updates user's tokens. Administrator permissions should be checked before this call.

func CreateUsers

func CreateUsers(ctx context.Context, names []string) ([]UserResult, error)

CreateUsers creates new users.

func DisableUsers

func DisableUsers(ctx context.Context, names []string) ([]UserResult, error)

DisableUsers deactivates users' accounts. Administrator permissions should be checked before this call.

Jump to

Keyboard shortcuts

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