admin

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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) DeleteUser added in v0.4.0

func (s *GormService) DeleteUser(ctx context.Context, username string) error

DeleteUser removes an operator account and its sessions. Sessions live in the cache file under a soft username reference, so they delete explicitly: orphaned rows would otherwise stay valid. Removing the last remaining account denies; a bootstrapped instance with no logins locks every operator out. The sessions delete runs before the user delete so a failed user delete never orphans valid sessions.

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.

func (*GormService) ListLogins added in v0.4.0

func (s *GormService) ListLogins(ctx context.Context) ([]*models.AdminUser, error)

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

func (s *GormService) ValidateSession(ctx context.Context, token string) (string, bool)

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.

Jump to

Keyboard shortcuts

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