user

package
v0.0.0-...-e7a724a Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound abstracts the mgo not found error.
	ErrNotFound = errors.New("Entity not found")

	// ErrInvalidID occurs when an ID is not in a valid form.
	ErrInvalidID = errors.New("ID is not in its proper form")

	// ErrAuthenticationFailure occurs when a user attempts to authenticate but
	// anything goes wrong.
	ErrAuthenticationFailure = errors.New("Authentication failed")

	// ErrForbidden occurs when a user tries to do something that is forbidden to them according to our access control policies.
	ErrForbidden = errors.New("Attempted action is not allowed")
)

Functions

func Delete

func Delete(ctx context.Context, dbConn *db.DB, id string) error

Delete removes a user from the database.

func Update

func Update(ctx context.Context, dbConn *db.DB, id string, upd *UpdateUser, now time.Time) error

Update replaces a user document in the database.

Types

type NewUser

type NewUser struct {
	Name            string   `json:"name" validate:"required"`
	Email           string   `json:"email" validate:"required"` // TODO(jlw) enforce uniqueness.
	Roles           []string `json:"roles" validate:"required"` // TODO(jlw) Ensure only includes valid roles.
	Password        string   `json:"password" validate:"required"`
	PasswordConfirm string   `json:"password_confirm" validate:"eqfield=Password"`
}

NewUser contains information needed to create a new User.

type Token

type Token struct {
	Token string `json:"token"`
}

Token is the payload we deliver to users when they authenticate.

func Authenticate

func Authenticate(ctx context.Context, dbConn *db.DB, tknGen TokenGenerator, now time.Time, email, password string) (Token, error)

Authenticate finds a user by their email and verifies their password. On success it returns a Token that can be used to authenticate in the future.

type TokenGenerator

type TokenGenerator interface {
	GenerateToken(auth.Claims) (string, error)
}

TokenGenerator is the behavior we need in our Authenticate to generate tokens for authenticated users.

type UpdateUser

type UpdateUser struct {
	Name            *string  `json:"name"`
	Email           *string  `json:"email"` // TODO(jlw) enforce uniqueness.
	Roles           []string `json:"roles"` // TODO(jlw) Ensure only includes valid roles.
	Password        *string  `json:"password"`
	PasswordConfirm *string  `json:"password_confirm" validate:"omitempty,eqfield=Password"`
}

UpdateUser defines what information may be provided to modify an existing User. All fields are optional so clients can send just the fields they want changed. It uses pointer fields so we can differentiate between a field that was not provided and a field that was provided as explicitly blank. Normally we do not want to use pointers to basic types but we make exceptions around marshalling/unmarshalling.

type User

type User struct {
	ID    bson.ObjectId `bson:"_id" json:"id"`
	Name  string        `bson:"name" json:"name"`
	Email string        `bson:"email" json:"email"` // TODO(jlw) enforce uniqueness
	Roles []string      `bson:"roles" json:"roles"`

	PasswordHash []byte `bson:"password_hash" json:"-"`

	DateModified time.Time `bson:"date_modified" json:"date_modified"`
	DateCreated  time.Time `bson:"date_created,omitempty" json:"date_created"`
}

User represents someone with access to our system.

func Create

func Create(ctx context.Context, dbConn *db.DB, nu *NewUser, now time.Time) (*User, error)

Create inserts a new user into the database.

func List

func List(ctx context.Context, dbConn *db.DB) ([]User, error)

List retrieves a list of existing users from the database.

func Retrieve

func Retrieve(ctx context.Context, claims auth.Claims, dbConn *db.DB, id string) (*User, error)

Retrieve gets the specified user from the database.

Jump to

Keyboard shortcuts

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