users

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 11 Imported by: 0

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

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

View Source
var (
	ErrNotFound  = errors.New("user not found")
	ErrExists    = errors.New("user already exists")
	ErrLastAdmin = errors.New("operation would leave no enabled admin")
)

Functions

func CheckPassword

func CheckPassword(hash, password string) bool

CheckPassword reports whether password matches hash. A malformed hash never matches.

func GeneratePassword

func GeneratePassword() (string, error)

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

func HashPassword(password string) (string, error)

HashPassword returns a bcrypt hash of password at the default cost.

func ValidatePassword

func ValidatePassword(username, password string) error

ValidatePassword requires at least 12 characters, at most 72 bytes, and a password different from the username (case-insensitive).

func ValidateUsername

func ValidateUsername(name string) error

ValidateUsername accepts 1-64 characters of [A-Za-z0-9._@-].

Types

type Role

type Role string

Role is a console permission level.

const (
	RoleAdmin    Role = "admin"
	RoleOperator Role = "operator"
)

func (Role) Valid

func (r Role) Valid() bool

Valid reports whether r is one of the known roles.

type Store

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

Store persists users in SQLite.

func New

func New(db *storage.DB) (*Store, error)

New runs the users migration on db and returns a Store.

func (*Store) Create

func (s *Store) Create(u User) error

Create validates and inserts u. ErrExists on a duplicate name in any case.

func (*Store) Delete

func (s *Store) Delete(username string) error

Delete removes the user, refusing to remove the last enabled admin.

func (*Store) EnsureInitialAdmin

func (s *Store) EnsureInitialAdmin() (password string, created bool, err error)

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

func (s *Store) Get(username string) (User, error)

Get returns the user with the given (case-insensitive) name.

func (*Store) HasEnabledUsers

func (s *Store) HasEnabledUsers() (bool, error)

HasEnabledUsers reports whether at least one non-disabled user exists.

func (*Store) List

func (s *Store) List() ([]User, error)

List returns all users ordered by username, case-insensitively.

func (*Store) ResetAdmin

func (s *Store) ResetAdmin(hash string) (created bool, err error)

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

func (s *Store) SetDisabled(username string, disabled bool) error

SetDisabled enables or disables the user and revokes their sessions. Setting the current value is a no-op.

func (*Store) SetPassword

func (s *Store) SetPassword(username, hash string, mustChange bool) error

SetPassword replaces the password hash and revokes the user's sessions.

func (*Store) SetRole

func (s *Store) SetRole(username string, r Role) error

SetRole changes the user's role and revokes their sessions. Same role is a no-op.

func (*Store) TouchLogin

func (s *Store) TouchLogin(username string, at time.Time) error

TouchLogin records a successful login time.

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

Jump to

Keyboard shortcuts

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