Documentation
¶
Overview ¶
Package users stores console users (username, bcrypt hash, role) in the shared SQLite database and validates the credentials that go into it.
Index ¶
- Constants
- Variables
- func CheckPassword(hash, password string) bool
- func GeneratePassword() (string, error)
- func HashPassword(password string) (string, error)
- func ValidatePassword(username, password string) error
- func ValidateUsername(name string) error
- type Role
- type Store
- func (s *Store) Create(u User) error
- func (s *Store) Delete(username string) error
- func (s *Store) EnsureInitialAdmin() (password string, created bool, err error)
- func (s *Store) Get(username string) (User, error)
- func (s *Store) HasEnabledUsers() (bool, error)
- func (s *Store) List() ([]User, error)
- func (s *Store) ResetAdmin(hash string) (created bool, err error)
- func (s *Store) SetDisabled(username string, disabled bool) error
- func (s *Store) SetPassword(username, hash string, mustChange bool) error
- func (s *Store) SetRole(username string, r Role) error
- func (s *Store) TouchLogin(username string, at time.Time) error
- type User
- type ValidationError
Constants ¶
const InitialAdminUsername = "admin"
InitialAdminUsername is the fixed name of the admin account created on first start and restored by `jo-ei reset-admin-password`.
Variables ¶
Functions ¶
func CheckPassword ¶
CheckPassword reports whether password matches hash. A malformed hash never matches.
func GeneratePassword ¶
GeneratePassword returns a random 20-character password over [A-Za-z0-9], drawn from crypto/rand without modulo bias. It is the temporary password of the initial admin and of `jo-ei reset-admin-password`.
func HashPassword ¶
HashPassword returns a bcrypt hash of password at the default cost.
func ValidatePassword ¶
ValidatePassword requires at least 12 characters, at most 72 bytes, and a password different from the username (case-insensitive).
func ValidateUsername ¶
ValidateUsername accepts 1-64 characters of [A-Za-z0-9._@-].
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists users in SQLite.
func (*Store) EnsureInitialAdmin ¶
EnsureInitialAdmin creates the initial admin account when the users table is completely empty: user InitialAdminUsername, role admin, a generated password (returned so the caller can show it once) and must_change_password set. A table with any row, even only disabled users, is left alone and created is false.
The emptiness check and the insert are one INSERT ... WHERE NOT EXISTS statement, which takes the write lock up front (waiting out busy_timeout) instead of upgrading a read transaction — the upgrade fails at once with "database is locked" when two processes start on the same empty database. created is true only for the process whose statement inserted the row, so only that one ever sees (and logs) a password.
func (*Store) HasEnabledUsers ¶
HasEnabledUsers reports whether at least one non-disabled user exists.
func (*Store) ResetAdmin ¶
ResetAdmin is the recovery path behind `jo-ei reset-admin-password`. In one transaction it either restores the InitialAdminUsername account — new password hash, must_change_password set, enabled, role admin, sessions revoked by a token_version bump — or, when that account was deleted, recreates it with the same properties (created is true). Restoring or enabling an admin can only add admins, so the last-admin guard never applies.
func (*Store) SetDisabled ¶
SetDisabled enables or disables the user and revokes their sessions. Setting the current value is a no-op.
func (*Store) SetPassword ¶
SetPassword replaces the password hash and revokes the user's sessions.
type User ¶
type User struct {
Username string
PasswordHash string
Role Role
Disabled bool
MustChangePassword bool
TokenVersion int64
CreatedAt time.Time
LastLoginAt *time.Time
}
User is a console account. Username keeps the case it was created with; lookups are case-insensitive.
type ValidationError ¶
type ValidationError struct{ Field, Message string }
ValidationError describes an invalid user field.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string