Documentation ¶
Index ¶
- Variables
- func CheckPassword(h Hasher, cleartext, encoded string) bool
- func ConstantTimeStringCompare(x, y string) bool
- func EncodeBase64String(input []byte) string
- func MakePassword(h Hasher, cleartext string) string
- func MockHasher(name string, n int, digest func() hash.Hash) *mockHasher
- func Pbkdf2(cleartext, salt []byte, rounds int, h func() hash.Hash) []byte
- func RandomBytes(n int) []byte
- func RandomKey() string
- func RandomKeyN(n int) string
- func RegisterHasher(name string, hasher Hasher)
- func SetCookie(w http.ResponseWriter, c config.Cookie, session Session)
- type Auth
- func (auth *Auth) ByPassword(email, password string) (user User, err error)
- func (auth *Auth) BySession(key string) (user User)
- func (auth *Auth) ByToken(id int64, key string) (user User)
- func (auth *Auth) ByUserToken(id int64, key string) (user User, err error)
- func (auth *Auth) CookieName() string
- func (auth *Auth) CreateSession(w http.ResponseWriter, user User) error
- func (auth *Auth) CreateSessionAndRedirect(w http.ResponseWriter, r *http.Request, user User, next string) error
- func (auth *Auth) CreateUser(email, first, last, clear string) (User, error)
- func (auth *Auth) Logout(w http.ResponseWriter, r *http.Request) error
- func (auth *Auth) MakePassword(cleartext string) string
- func (auth *Auth) ResetUserToken(user *User)
- func (auth *Auth) Sessions() *SessionManager
- func (auth *Auth) Tokens() *TokenManager
- func (auth *Auth) Users() *UserManager
- type BaseHasher
- type Hasher
- type KeyFunc
- type PBKDF2_Base
- type Session
- type SessionManager
- type Token
- type TokenManager
- type User
- type UserManager
- func (m *UserManager) Create(email, first, last, clear string) (User, error)
- func (m *UserManager) CreateSuperuser(email, first, last, clear string) (User, error)
- func (m *UserManager) Delete(id int64) error
- func (m *UserManager) GetByEmail(email string) (user User, err error)
- func (m *UserManager) GetByID(id int64) (user User, err error)
- func (m UserManager) Hasher() Hasher
Constants ¶
This section is empty.
Variables ¶
var Sessions = postgres.Table("sessions", sol.Column("key", types.Varchar().NotNull()), sol.ForeignKey( "user_id", Users.C("id"), types.Integer().NotNull(), ).OnDelete(sol.Cascade).OnUpdate(sol.Cascade), sol.Column("expires", postgres.Timestamp().WithTimezone()), sol.PrimaryKey("key"), )
Sessions is the postgres schema for sessions
var Tokens = postgres.Table("tokens", sol.Column("key", types.Varchar().NotNull()), sol.ForeignKey( "user_id", Users.C("id"), types.Integer().NotNull(), ).OnDelete(sol.Cascade).OnUpdate(sol.Cascade), sol.Column("expires", postgres.Timestamp().WithTimezone()), sol.Column( "created_at", postgres.Timestamp().WithTimezone().NotNull().Default(postgres.Now), ), sol.PrimaryKey("key"), )
Tokens is the postgres schema for user API tokens.
var Users = postgres.Table("users", sol.Column("id", postgres.Serial()), sol.Column("email", types.Varchar().Limit(256).NotNull()), sol.Column("first_name", types.Varchar().Limit(64).NotNull()), sol.Column("last_name", types.Varchar().Limit(64).NotNull()), sol.Column("about", types.Varchar().Limit(512).NotNull()), sol.Column("photo", types.Varchar().Limit(512).NotNull()), sol.Column("is_active", types.Boolean().NotNull().Default(true)), sol.Column("is_superuser", types.Boolean().NotNull().Default(false)), sol.Column("password", types.Varchar().Limit(256).NotNull()), sol.Column("token", types.Varchar().Limit(256).NotNull()), sol.Column( "token_set_at", postgres.Timestamp().WithTimezone().NotNull().Default(postgres.Now), ), sol.Column( "created_at", postgres.Timestamp().WithTimezone().NotNull().Default(postgres.Now), ), sol.PrimaryKey("id"), sol.Unique("email"), )
Users is the postgres schema for users
Functions ¶
func CheckPassword ¶
CheckPassword verifies the given cleartext password against the given encoded string using the given hasher.
func ConstantTimeStringCompare ¶
ConstantTimeStringCompare is wrapper around subtle.ConstantTimeCompare that takes two strings as parameters and returns a boolean instead of an int.
func EncodeBase64String ¶
EncodeBase64String is a wrapper around the standard base64 encoding call.
func MakePassword ¶
MakePassword hashes the given cleartext string using the given Hasher
func RandomBytes ¶
RandomBytes returns random bytes from the crypto/rand Reader or it panics.
func RandomKey ¶
func RandomKey() string
RandomKey generates a new session key. It does so by producing 24 random bytes that are encoded in URL safe base64, for output of 32 chars.
func RandomKeyN ¶
RandomKeyN generates a new Base 64 encoded random string. N is the length of the random bytes, not the final encoded string.
func RegisterHasher ¶
RegisterHasher adds a new Hasher to the registry with the given name.
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
func (*Auth) ByPassword ¶
ByPassword attempts to authenticate the given email using the given cleartext password. On failure, a specific error will be returned.
func (*Auth) ByToken ¶
ByToken returns an authenticated user if the given token is valid for the given user id. Tokens are used for API access.
func (*Auth) ByUserToken ¶
ByUserToken returns an authenticated user if the given user's token matches the given token. Companies are not added as this method is used only for password resets and initial account creation. The user token also is attached to the user's model, not the separate tokens table, which is used for API access.
func (*Auth) CookieName ¶
CookieName returns the name of the cookie used by this auth
func (*Auth) CreateSession ¶
func (auth *Auth) CreateSession(w http.ResponseWriter, user User) error
CreateSession creates a new session for the given user and redirects to the given next URL.
func (*Auth) CreateSessionAndRedirect ¶
func (*Auth) CreateUser ¶
CreateUser creates a new user.
func (*Auth) MakePassword ¶
MakePassword returns an encrypted string of the given cleartext password using the auth user hasher.
func (*Auth) ResetUserToken ¶
ResetUserToken generates a new user token and resets the token timestamp.
func (*Auth) Sessions ¶
func (auth *Auth) Sessions() *SessionManager
Sessions returns the internal session manager
func (*Auth) Tokens ¶
func (auth *Auth) Tokens() *TokenManager
Tokens returns the internal token manager
type BaseHasher ¶
type BaseHasher struct {
// contains filtered or unexported fields
}
BaseHasher is the parent of all included Hashers
func NewBaseHasher ¶
func NewBaseHasher(algorithm string) BaseHasher
func (*BaseHasher) Algorithm ¶
func (h *BaseHasher) Algorithm() string
Algorithm returns the algorithm of this Hasher
func (*BaseHasher) Salt ¶
func (h *BaseHasher) Salt() string
Salt generates nine random bytes encoded to base64 for use as a salt.
type Hasher ¶
type Hasher interface { Encode(string, string) string Salt() string Verify(string, string) bool Algorithm() string }
Hasher is the target interface for included hashers.
type KeyFunc ¶
type KeyFunc func() string
KeyFunc is the function type that will be used to generate new session keys.
type PBKDF2_Base ¶
type PBKDF2_Base struct { BaseHasher // contains filtered or unexported fields }
TODO declare private?
func NewPBKDF2Hasher ¶
func NewPBKDF2Hasher(alg string, n int, digest func() hash.Hash) *PBKDF2_Base
func (*PBKDF2_Base) Encode ¶
func (h *PBKDF2_Base) Encode(cleartext, salt string) string
func (*PBKDF2_Base) Verify ¶
func (h *PBKDF2_Base) Verify(cleartext, encoded string) bool
type Session ¶
type Session struct { Key string `db:"key"` UserID int64 `db:"user_id"` Expires time.Time `db:"expires"` // contains filtered or unexported fields }
Session is a database-backed user session.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager is the internal manager of sessions
func NewSessions ¶
func NewSessions(c config.Cookie, conn sol.Conn) *SessionManager
NewSessions will create a new internal session manager
func (*SessionManager) Create ¶
func (m *SessionManager) Create(user User) (session Session)
Create creates a new session using a key generated for the given User
func (*SessionManager) Delete ¶
func (m *SessionManager) Delete(key string) error
Delete removes the session with the given key from the database.
func (*SessionManager) Get ¶
func (m *SessionManager) Get(key string) (session Session)
Get returns the session with the given key.
type Token ¶
type Token struct { Key string `db:"key"` UserID int64 `db:"user_id"` Expires *time.Time `db:"expires"` CreatedAt time.Time `db:"created_at,omitempty"` // contains filtered or unexported fields }
Token is a database-backed user API token. The Expires field is nil if the token never expires.
type TokenManager ¶
type TokenManager struct {
// contains filtered or unexported fields
}
TokenManager is the internal manager of tokens
func NewTokens ¶
func NewTokens(conn sol.Conn) *TokenManager
func (*TokenManager) All ¶
func (m *TokenManager) All(id int64) (tokens []Token)
All returns all tokens for the given user ID
func (*TokenManager) Count ¶
func (m *TokenManager) Count() (count int64)
func (*TokenManager) Delete ¶
func (m *TokenManager) Delete(key string) error
Delete removes the token with the given key from the database. It will return an error if the token does not have a key or the key was not deleted from the database. It will panic on any connection error.
func (*TokenManager) ForeverToken ¶
func (m *TokenManager) ForeverToken(user User) (token Token)
Create creates a new token for the user. It will panic on error. The user ID must exist.
func (*TokenManager) Get ¶
func (m *TokenManager) Get(key string) (token Token)
Get returns the token with the given key. Panic on database error.
type User ¶
type User struct { ID int64 `db:"id,omitempty"` Email string `db:"email"` FirstName string `db:"first_name"` LastName string `db:"last_name"` About string `db:"about"` Photo string `db:"photo"` IsActive bool `db:"is_active"` IsSuperuser bool `db:"is_superuser"` Password string `db:"password"` Token string `db:"token"` TokenSetAt time.Time `db:"token_set_at,omitempty"` CreatedAt time.Time `db:"created_at,omitempty"` // contains filtered or unexported fields }
User is a database-backed user.
func (User) Delete ¶
Delete removes the user with the given ID from the database. It will return an error if the user does not have an ID or the ID was not deleted from the database. It will panic on any connection error.
type UserManager ¶
type UserManager struct {
// contains filtered or unexported fields
}
UserManager is the internal manager of users
func MockUsers ¶
func MockUsers(conn sol.Conn) *UserManager
func NewUsers ¶
func NewUsers(conn sol.Conn) *UserManager
func (*UserManager) Create ¶
func (m *UserManager) Create(email, first, last, clear string) (User, error)
Create will create a new user with given email and cleartext password. It will panic on any crypto or database connection errors.
func (*UserManager) CreateSuperuser ¶
func (m *UserManager) CreateSuperuser(email, first, last, clear string) (User, error)
CreateSuperuser will create a new superuser with given email and cleartext password. It will panic on any crypto or database connection errors.
func (*UserManager) Delete ¶
func (m *UserManager) Delete(id int64) error
Delete removes the user with the given ID from the database. It will return an error if the ID was not deleted from the database. It will panic on any connection error.
func (*UserManager) GetByEmail ¶
func (m *UserManager) GetByEmail(email string) (user User, err error)
GetByEmail returns the user with the given email.
func (*UserManager) GetByID ¶
func (m *UserManager) GetByID(id int64) (user User, err error)
GetByID returns the user with the given id.
func (UserManager) Hasher ¶
func (m UserManager) Hasher() Hasher
Hasher returns the hasher used by the UserManager