auth

package
v0.0.0-...-b44a069 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2015 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPwTooShort  = errors.New("Supplied password too short")
	ErrPwTooLong   = errors.New("Supplied password too long")
	ErrIncorrectPw = errors.New("Supplied password does not match the hash")
)
View Source
var (
	ErrNoSession = errors.New("No session present for the request")
	ErrWrongType = errors.New("The value stored in the session was the wrong type")
)
View Source
var (
	ErrPwMismatch   = errors.New("Passwords supplied do not match")
	ErrNoToken      = errors.New("There is no token associated with the user")
	ErrAlreadySaved = errors.New("Cannot insert as user already saved")
)

Functions

func NewCookieSessionStore

func NewCookieSessionStore(pairs ...[]byte) *cookieSessionStore

Types

type Mailer

type Mailer interface {
	Signup(*User) error
	PasswordReset(*User) error
}

type PasswordHasher

type PasswordHasher interface {
	Hash(string) (string, error)
	Compare(string, string) error
}

func NewBcryptHasher

func NewBcryptHasher(minPwLen, maxPwLen, bcryptCost int) PasswordHasher

type SessionStore

type SessionStore interface {
	User(http.ResponseWriter, *http.Request, Storer) (*User, error)
	LogUserOut(http.ResponseWriter, *http.Request) error
	LogUserIn(http.ResponseWriter, *http.Request, *User) error
}

type Storer

type Storer interface {
	UserByEmail(string) (*User, error)
	UserByID(int64) (*User, error)
	UserByToken(string) (*User, error)
	Users() ([]*User, error)
	Delete(*User) error
	Insert(*User) error
	Update(*User) error
}

type User

type User struct {
	ID        int64      `db:"id" json:"id"`
	Email     string     `db:"email" json:"email"`
	PwHash    string     `db:"pw_hash" json:"-"`
	Admin     bool       `db:"admin" json:"-"`
	Active    bool       `db:"active" json:"active"`
	Token     string     `db:"token" json:"token"`
	Name      string     `db:"name" json:"name"`
	CreatedAt *time.Time `db:"created_at" json:"createdAt"`
}

func (*User) String

func (u *User) String() string

type UserManager

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

func NewUserManager

func NewUserManager(h PasswordHasher, s Storer, m Mailer, sm SessionStore) *UserManager

NewUserManager creates an object which can be used to manipulate User objects.

func (UserManager) Activate

func (m UserManager) Activate(u *User) error

Activate ensures that a user is able to log on.

func (UserManager) AdminFromSession

func (m UserManager) AdminFromSession(w http.ResponseWriter, r *http.Request) (*User, error)

func (UserManager) Authenticate

func (m UserManager) Authenticate(u *User, pw string) error

Authenticate checks to see if the password supplies is the same as the password that was used to create the hash

func (UserManager) ByEmail

func (m UserManager) ByEmail(email string) (*User, error)

ByEmail obtains the user from the email supplied

func (UserManager) ById

func (m UserManager) ById(id int64) (*User, error)

ById obtains the user by their id field

func (UserManager) ByToken

func (m UserManager) ByToken(tok string) (*User, error)

ByToken obtains the user from the generated token. This can be used for password resets or signup links

func (UserManager) Deactivate

func (m UserManager) Deactivate(u *User) error

Deactivate deactivates the user, disabling the user from logging on.

func (UserManager) DeleteUser

func (m UserManager) DeleteUser(u *User) error

func (UserManager) DeleteUserById

func (m UserManager) DeleteUserById(id int64) error

func (UserManager) FromSession

func (m UserManager) FromSession(w http.ResponseWriter, r *http.Request) (*User, error)

FromSession retrieves the current user associated with the session

func (UserManager) Insert

func (m UserManager) Insert(u *User) error

Insert saves a new User. This cannot be called if the user has already been saved.

func (UserManager) LogIn

func (m UserManager) LogIn(w http.ResponseWriter, r *http.Request, u *User) error

func (UserManager) LogOut

func (m UserManager) LogOut(w http.ResponseWriter, r *http.Request) error

func (UserManager) New

func (m UserManager) New(name, email, pw, confirmPw string, active, admin bool) (*User, error)

New creates a new user. Note that this only creates the user, it does not save the user.

func (UserManager) RequestPwReset

func (m UserManager) RequestPwReset(u *User, disableCurrentPw bool) error

RequestPwReset sends a password reset email to the user and optionally disables the user from logging on/

func (UserManager) SendSignupMail

func (m UserManager) SendSignupMail(u *User) error

SendSignupMail sends the user a signup email

func (UserManager) SignupUser

func (m UserManager) SignupUser(w http.ResponseWriter, r *http.Request, name, email, pw, confirmPw string, admin, active bool) (*User, error)

SignupUser creates a new user, emails a signup email, saves the user and logs them in.

func (UserManager) Update

func (m UserManager) Update(u *User) error

Update saves any changes to the User object. This can only be called if the user has already been saved.

func (UserManager) UpdatePw

func (m UserManager) UpdatePw(u *User, pw, confirm string) error

UpdatePw forces a password change. This can be useful for situations when the user does not know their password or if an admin wants to request a password change.

func (UserManager) UserResetPw

func (m UserManager) UserResetPw(u *User, old, pw, confirm string) error

UserResetPw is used when a user requests a password update. The user must supply the correct password in order for the update to be successful

func (UserManager) Users

func (m UserManager) Users() ([]*User, error)

Jump to

Keyboard shortcuts

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