Documentation
¶
Index ¶
- Variables
- type GormService
- func (s *GormService) Bootstrap(ctx context.Context, username, password string) error
- func (s *GormService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error
- func (s *GormService) CleanupExpiredSessions(ctx context.Context) error
- func (s *GormService) CreateLogin(ctx context.Context, username, password string) error
- func (s *GormService) IsBootstrapped(ctx context.Context) (bool, error)
- func (s *GormService) ListLogins(ctx context.Context) ([]*models.AdminUser, error)
- func (s *GormService) Login(ctx context.Context, username, password string, rememberMe bool) (string, time.Time, error)
- func (s *GormService) Logout(ctx context.Context, token string)
- func (s *GormService) ResetPassword(ctx context.Context, username, newPassword string) error
- func (s *GormService) SetLogger(l *slog.Logger)
- func (s *GormService) ValidateSession(ctx context.Context, token string) (string, bool)
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyBootstrapped = errors.New("already bootstrapped")
ErrAlreadyBootstrapped is returned when bootstrap runs on an instance that already has its first admin account.
var ErrInvalidCredentials = errors.New("invalid username or password")
ErrInvalidCredentials is returned when a login attempt fails.
Functions ¶
This section is empty.
Types ¶
type GormService ¶ added in v0.4.0
type GormService struct {
// contains filtered or unexported fields
}
GormService manages admin users and dashboard sessions on the SQLite handles with explicit converters between wire types and rows. Accounts and instance settings live in the durable database; sessions live in the cache file and reference accounts softly. Session tokens persist as SHA-256 hashes only; the plaintext token returns once at login and never lands in storage. Traditional GORM use: none; every statement runs through gorm.G with typed clauses.
func NewGorm ¶ added in v0.4.0
func NewGorm(database *db.Database) *GormService
NewGorm constructs a GORM-backed admin service on the SQLite handle. Construction performs no I/O.
func (*GormService) Bootstrap ¶ added in v0.4.0
func (s *GormService) Bootstrap(ctx context.Context, username, password string) error
Bootstrap creates the initial admin account and marks the instance bootstrapped. A set flag denies with an already-bootstrapped error. The password hash generates outside the transaction; one transaction holds the flag check, the user upsert, and the flag write on the transaction handle only.
func (*GormService) ChangePassword ¶ added in v0.4.0
func (s *GormService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error
ChangePassword verifies the current password and stores the new hash. A missing account or a wrong password denies with ErrInvalidCredentials. Verification and hashing run outside the transaction; one transaction holds the single-column update on the transaction handle only.
func (*GormService) CleanupExpiredSessions ¶ added in v0.4.0
func (s *GormService) CleanupExpiredSessions(ctx context.Context) error
CleanupExpiredSessions removes sessions expired before now through the expires_at index on the cache handle. Valid sessions survive.
func (*GormService) CreateLogin ¶ added in v0.4.0
func (s *GormService) CreateLogin(ctx context.Context, username, password string) error
CreateLogin adds an operator account without touching the bootstrapped flag. The instance must already be bootstrapped; first-run setup owns Bootstrap instead. A duplicate name denies with an already-exists error. Hashing runs outside the transaction; one transaction holds the flag check and the insert on the transaction handle only.
func (*GormService) IsBootstrapped ¶ added in v0.4.0
func (s *GormService) IsBootstrapped(ctx context.Context) (bool, error)
IsBootstrapped returns true once the admin account has been created. A missing settings row reads as not bootstrapped. A true answer is cached: the flag never clears.
func (*GormService) ListLogins ¶ added in v0.4.0
ListLogins returns every dashboard operator account ordered by username. Password hashes never leave storage: the returned models carry usernames and creation times only.
func (*GormService) Login ¶ added in v0.4.0
func (s *GormService) Login(ctx context.Context, username, password string, rememberMe bool) (string, time.Time, error)
Login validates credentials and persists a hashed session with expiration. The plaintext token returns once and never persists. Unknown accounts and wrong passwords deny with ErrInvalidCredentials. The credential check runs outside the transaction; one transaction holds the session insert on the transaction handle only.
func (*GormService) Logout ¶ added in v0.4.0
func (s *GormService) Logout(ctx context.Context, token string)
Logout invalidates a session token. Missing tokens are not errors, matching the previous behavior.
func (*GormService) ResetPassword ¶ added in v0.4.0
func (s *GormService) ResetPassword(ctx context.Context, username, newPassword string) error
ResetPassword stores a new hash for the named account without asking for the current password. Local database access is root-equivalent: this path serves CLI operators, never the dashboard. A missing account denies with a not-found error. Hashing runs outside the transaction; one transaction holds the single-column update on the transaction handle only. Existing sessions survive, matching ChangePassword.
func (*GormService) SetLogger ¶ added in v0.4.0
func (s *GormService) SetLogger(l *slog.Logger)
SetLogger wires structured logging.
func (*GormService) ValidateSession ¶ added in v0.4.0
ValidateSession returns the username for a valid, non-expired session token. The common path is read-only: the expiration refreshes only when less than half the session duration remains, so parallel dashboard calls never contend on one row. A failed refresh never invalidates a verified session. Expired sessions delete best-effort. Unknown or expired tokens deny with false, never an error, matching the previous behavior.