Documentation
¶
Overview ¶
Package auth provides the CMS's user accounts: password hashing, the Postgres-backed user store, roles, and login throttling.
Index ¶
- Variables
- func HashPassword(password string) (string, error)
- func VerifyPassword(password, phc string) (bool, error)
- type Role
- type Store
- func (s *Store) All(ctx context.Context) ([]User, error)
- func (s *Store) Authenticate(ctx context.Context, email, password string) (*User, error)
- func (s *Store) Count(ctx context.Context) (int, error)
- func (s *Store) GetByEmail(ctx context.Context, email string) (*User, error)
- func (s *Store) GetByID(ctx context.Context, id int64) (*User, error)
- func (s *Store) Insert(ctx context.Context, u *User) (int64, error)
- func (s *Store) Update(ctx context.Context, u *User) error
- func (s *Store) UpdatePassword(ctx context.Context, id int64, passwordHash string) error
- type Throttle
- type User
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when no user matches the query. ErrNotFound = errors.New("auth: user not found") // ErrDuplicateEmail is returned by Insert/Update when the email is taken. ErrDuplicateEmail = errors.New("auth: email already in use") // ErrInvalidCredentials is returned by Authenticate for a bad email or // password, or an inactive account. It deliberately does not say which. ErrInvalidCredentials = errors.New("auth: invalid credentials") )
var ErrInvalidHash = errors.New("auth: stored password hash is malformed")
ErrInvalidHash is returned by VerifyPassword when the stored hash is not a well-formed argon2id PHC string.
Functions ¶
func HashPassword ¶
HashPassword derives an argon2id hash of password and returns it in PHC string format, e.g. $argon2id$v=19$m=65536,t=1,p=4$<salt>$<hash>.
func VerifyPassword ¶
VerifyPassword reports whether password matches the PHC-format argon2id hash. The comparison is constant time in the derived key.
Types ¶
type Role ¶
type Role string
Role controls what a user may do in the admin area.
const ( // RoleSuperadmin has every admin power plus raw HTML access in the // in-place editor (the whole-page source view) — for users who are // comfortable hand-editing markup. RoleSuperadmin Role = "superadmin" // RoleAdmin may manage users and site settings in addition to content. RoleAdmin Role = "admin" // RoleEditor may create and edit content but not manage users. RoleEditor Role = "editor" )
func (Role) IsAdmin ¶
IsAdmin reports whether the role carries admin powers (user management, unsanitized content, page CSS/JS). Superadmin is a superset of admin.
func (Role) IsSuperadmin ¶
IsSuperadmin reports whether the role may edit raw page HTML in the in-place editor.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store reads and writes users in Postgres.
func (*Store) Authenticate ¶
Authenticate checks email and password and returns the matching active user, or ErrInvalidCredentials. To resist timing probes for valid addresses, it verifies a dummy hash when the email is unknown.
func (*Store) GetByEmail ¶
GetByEmail returns the user with the given email (case-insensitive), or ErrNotFound.
func (*Store) Insert ¶
Insert stores a new user and returns its id. Email is normalized to lower case. Returns ErrDuplicateEmail if the address is taken.
type Throttle ¶
type Throttle struct {
// contains filtered or unexported fields
}
Throttle is a small in-memory failed-login limiter. Keys are typically "email|remote-ip". It is per-process; that is sufficient to blunt online password guessing, which is all it aims to do.
func NewThrottle ¶
NewThrottle returns a Throttle allowing limit failures per key per window.