auth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package auth provides the CMS's user accounts: password hashing, the Postgres-backed user store, roles, and login throttling.

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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

func HashPassword(password string) (string, error)

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

func VerifyPassword(password, phc string) (bool, error)

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

func (r Role) IsAdmin() bool

IsAdmin reports whether the role carries admin powers (user management, unsanitized content, page CSS/JS). Superadmin is a superset of admin.

func (Role) IsSuperadmin

func (r Role) IsSuperadmin() bool

IsSuperadmin reports whether the role may edit raw page HTML in the in-place editor.

func (Role) Valid

func (r Role) Valid() bool

Valid reports whether r is a role the CMS knows about.

type Store

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

Store reads and writes users in Postgres.

func NewStore

func NewStore(db *sqldb.DB) *Store

NewStore returns a Store backed by db.

func (*Store) All

func (s *Store) All(ctx context.Context) ([]User, error)

All returns every user, ordered by name then email.

func (*Store) Authenticate

func (s *Store) Authenticate(ctx context.Context, email, password string) (*User, error)

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) Count

func (s *Store) Count(ctx context.Context) (int, error)

Count returns the total number of users.

func (*Store) GetByEmail

func (s *Store) GetByEmail(ctx context.Context, email string) (*User, error)

GetByEmail returns the user with the given email (case-insensitive), or ErrNotFound.

func (*Store) GetByID

func (s *Store) GetByID(ctx context.Context, id int64) (*User, error)

GetByID returns the user with the given id, or ErrNotFound.

func (*Store) Insert

func (s *Store) Insert(ctx context.Context, u *User) (int64, error)

Insert stores a new user and returns its id. Email is normalized to lower case. Returns ErrDuplicateEmail if the address is taken.

func (*Store) Update

func (s *Store) Update(ctx context.Context, u *User) error

Update saves email, name, role, and active for an existing user. It does not touch the password; use UpdatePassword for that.

func (*Store) UpdatePassword

func (s *Store) UpdatePassword(ctx context.Context, id int64, passwordHash string) error

UpdatePassword replaces a user's password hash.

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

func NewThrottle(limit int, window time.Duration) *Throttle

NewThrottle returns a Throttle allowing limit failures per key per window.

func (*Throttle) Blocked

func (t *Throttle) Blocked(key string) bool

Blocked reports whether key has exceeded its failure allowance.

func (*Throttle) Fail

func (t *Throttle) Fail(key string)

Fail records a failed attempt for key.

func (*Throttle) Reset

func (t *Throttle) Reset(key string)

Reset clears the failure count for key, e.g. after a successful login.

type User

type User struct {
	ID           int64
	Email        string
	Name         string
	PasswordHash string
	Role         Role
	Active       bool
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

User is a CMS account.

Jump to

Keyboard shortcuts

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