store

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const InstallerConfigKey = "installer.config"

Variables

View Source
var (
	ErrNotFound       = errors.New("record not found")
	ErrAlreadyExists  = errors.New("record already exists")
	ErrSessionExpired = errors.New("session expired")
	// ErrSSOSessionEnded means the identity provider already ended the login being signed in.
	ErrSSOSessionEnded = errors.New("SSO session already ended")
	// ErrStale reports a directory event no newer than the last one applied to the account.
	ErrStale           = errors.New("directory event is not newer than the applied state")
	ErrInvalidUsername = errors.New("directory username must be 1-128 letters, digits or ._@+-")
)

Functions

This section is empty.

Types

type AuditRecord

type AuditRecord struct {
	ID        int64     `json:"id"`
	UserID    string    `json:"user_id"`
	Action    string    `json:"action"` // e.g. "auth.login", "scim.user_created"
	Resource  string    `json:"resource"`
	Details   string    `json:"details,omitempty"`
	IPAddress string    `json:"ip_address"`
	CreatedAt time.Time `json:"created_at"`
}

AuditRecord logs security and operational events with tamper-evident structure.

type AuditStore

type AuditStore interface {
	LogAudit(ctx context.Context, r *AuditRecord) error
	ListAuditRecords(ctx context.Context, offset, limit int) ([]*AuditRecord, int, error)
}

AuditStore logs security events.

type DirectoryUser

type DirectoryUser struct {
	ID          string
	Provider    string
	Subject     string
	Username    string
	Email       string
	DisplayName string
	Active      bool
	EventTime   int64
}

DirectoryUser is what a directory may assert about an account. It has no role: new accounts are "user" and application administration is an operator grant (SetDirectoryRole). ID empty creates a Provider ("scim" or "kysignon") account; otherwise it updates that directory account. A nonzero EventTime (unix seconds) applies only when strictly newer than the account's last applied event, else ErrStale.

type Group

type Group struct {
	ID          string    `json:"id"`
	DisplayName string    `json:"display_name"`
	ExternalID  string    `json:"external_id,omitempty"`
	Members     []string  `json:"members,omitempty"` // User IDs
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Group represents a SCIM/RBAC user group.

type GroupStore

type GroupStore interface {
	CreateGroup(ctx context.Context, g *Group) error
	ReplaceGroup(ctx context.Context, g *Group, create bool) error
	GetGroupByExternalID(ctx context.Context, id string) (*Group, error)
	GetGroupByID(ctx context.Context, id string) (*Group, error)
	GetGroupByName(ctx context.Context, name string) (*Group, error)
	UpdateGroup(ctx context.Context, g *Group) error
	DeleteGroup(ctx context.Context, id string) error
	ListGroups(ctx context.Context, offset, limit int) ([]*Group, int, error)
	AddGroupMember(ctx context.Context, groupID, userID string) error
	RemoveGroupMember(ctx context.Context, groupID, userID string) error
	GetUserGroups(ctx context.Context, userID string) ([]*Group, error)
}

GroupStore defines repository operations for SCIM and RBAC groups.

type MFAChallenge

type MFAChallenge struct {
	TokenHash string
	UserID    string
	ExpiresAt time.Time
}

type SQLStore

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

SQLStore implements Store on top of database/sql.

func (*SQLStore) ApplyInstaller

func (s *SQLStore) ApplyInstaller(ctx context.Context, b applysetup.Common, check bool) (applysetup.Report, error)

ApplyInstaller commits accounts, ownership and audits together. It never adopts another account, changes credentials or takes ownership of pre-existing settings.

func (*SQLStore) Audit

func (s *SQLStore) Audit() AuditStore

func (*SQLStore) Close

func (s *SQLStore) Close() error

func (*SQLStore) Drive

func (s *SQLStore) Drive() *drive.Store

func (*SQLStore) Driver

func (s *SQLStore) Driver() string

func (*SQLStore) Groups

func (s *SQLStore) Groups() GroupStore

func (*SQLStore) InstallerAdministrator

func (s *SQLStore) InstallerAdministrator(ctx context.Context, id string) (bool, error)

InstallerAdministrator marks only newly created, explicitly dedicated accounts. Existing accounts and their content are retained for an explicit migration.

func (*SQLStore) Ping

func (s *SQLStore) Ping(ctx context.Context) error

func (*SQLStore) ResetAfterRestore added in v0.4.3

func (s *SQLStore) ResetAfterRestore(ctx context.Context) error

ResetAfterRestore ends restored access grants and turns scheduled deposits off in one transaction. Passwords, MFA secrets, directory membership and content remain as backed up; operators reconcile those separately.

func (*SQLStore) Sessions

func (s *SQLStore) Sessions() SessionStore

func (*SQLStore) Settings

func (s *SQLStore) Settings() SettingsStore

func (*SQLStore) Users

func (s *SQLStore) Users() UserStore

type Session

type Session struct {
	TokenHash string    `json:"token_hash"`
	UserID    string    `json:"user_id"`
	UserAgent string    `json:"user_agent"`
	IPAddress string    `json:"ip_address"`
	CreatedAt time.Time `json:"created_at"`
	ExpiresAt time.Time `json:"expires_at"`
	// SSOSID is the identity provider's sid for an SSO login; empty for local sign-in.
	SSOSID string `json:"-"`
	// StepUpAt is when the user last re-proved who they are in this session; zero if never.
	StepUpAt time.Time `json:"-"`
}

Session represents an active authenticated user session.

type SessionStore

type SessionStore interface {
	// CreateSession refuses when the password or the MFA state changed since the caller checked them.
	CreateSession(ctx context.Context, s *Session, expectedPasswordHash string, mfaEnabled bool) error
	GetSession(ctx context.Context, tokenHash string) (*Session, error)
	// StepUp records at as the session's latest step-up; ErrNotFound when the session is gone.
	StepUp(ctx context.Context, tokenHash string, at time.Time) error
	DeleteSession(ctx context.Context, tokenHash string) error
	DeleteUserSessions(ctx context.Context, userID string) error
	// EndSSOSubject ends the provider subject's sessions whose credentials were verified
	// before cutoff and fences that subject's earlier logins until fenceUntil.
	EndSSOSubject(ctx context.Context, provider, subject string, cutoff, fenceUntil time.Time) error
	// EndSSOSession ends every session of an SSO login and fences its sid until fenceUntil.
	EndSSOSession(ctx context.Context, sid string, fenceUntil time.Time) error
	EndSession(ctx context.Context, tokenHash string) error
	CleanExpiredSessions(ctx context.Context) error
	CleanExpiredMFAChallenges(ctx context.Context) error
	CreateMFAChallenge(ctx context.Context, challenge *MFAChallenge, expectedPasswordHash string) error
	ConsumeMFAChallenge(ctx context.Context, tokenHash string) (userID, passwordHash string, err error)
}

SessionStore defines repository operations for active login sessions.

type Setting

type Setting struct {
	Key       string    `json:"key"`
	Value     string    `json:"value"`
	UpdatedAt time.Time `json:"updated_at"`
}

Setting represents a durable server-wide key-value configuration entry.

type SettingsStore

type SettingsStore interface {
	GetSetting(ctx context.Context, key string) (string, error)
	SetSetting(ctx context.Context, key, val string) error
	DeleteSetting(ctx context.Context, key string) error
	GetAllSettings(ctx context.Context) (map[string]string, error)
}

SettingsStore handles persistent key-value configuration.

type Store

type Store interface {
	Drive() *drive.Store
	Users() UserStore
	Sessions() SessionStore
	Groups() GroupStore
	Audit() AuditStore
	Settings() SettingsStore
	ApplyInstaller(context.Context, applysetup.Common, bool) (applysetup.Report, error)
	InstallerAdministrator(context.Context, string) (bool, error)

	Driver() string
	Ping(ctx context.Context) error
	Close() error
	// ResetAfterRestore invalidates restored bearer grants without changing accounts or content.
	ResetAfterRestore(context.Context) error
}

Store defines the unified storage contract implemented across SQLite, PostgreSQL, and MySQL.

func Open

func Open(ctx context.Context, cfg config.DatabaseConfig, key []byte) (Store, error)

Open initializes and returns the configured database store backend. key is the deployment key (Security.EncryptionKey); the SQLite drive store refuses to open without it.

type User

type User struct {
	ID                 string     `json:"id"`
	Username           string     `json:"username"`
	Email              string     `json:"email"`
	DisplayName        string     `json:"display_name"`
	PasswordHash       string     `json:"-"`            // Never serialized to JSON
	Role               string     `json:"role"`         // "admin", "user", "manager"
	Status             string     `json:"status"`       // "active", "suspended", "inactive"
	SSOProvider        string     `json:"sso_provider"` // "local", "kysignon", "oidc", "saml", "scim"
	SSOSubject         string     `json:"sso_subject,omitempty"`
	TOTPSecretEnc      string     `json:"-"` // AES-256-GCM encrypted
	TOTPEnabled        bool       `json:"totp_enabled"`
	TOTPLastCounter    int64      `json:"-"` // last RFC 6238 counter accepted; refuses replay inside the skew window
	RecoveryCodesHash  string     `json:"-"` // JSON array of sha256 hashes
	MustChangePassword bool       `json:"must_change_password"`
	CreatedAt          time.Time  `json:"created_at"`
	UpdatedAt          time.Time  `json:"updated_at"`
	LastLoginAt        *time.Time `json:"last_login_at,omitempty"`
}

User represents an identity within the system.

func DirectorySubject

func DirectorySubject(ctx context.Context, users UserStore, subject string) (*User, error)

DirectorySubject finds a directory account by OIDC subject / SCIM externalId: the KySignOn row first, then the SCIM row, so sign-on and webhooks resolve the same account.

type UserStore

type UserStore interface {
	CreateUser(ctx context.Context, u *User) error
	GetUserByID(ctx context.Context, id string) (*User, error)
	GetUserByUsername(ctx context.Context, username string) (*User, error)
	GetUserByEmail(ctx context.Context, email string) (*User, error)
	GetUserBySSO(ctx context.Context, provider, subject string) (*User, error)
	UpdateUser(ctx context.Context, u *User) error
	// ApplyDirectoryUser is the one directory (SCIM, KySignOn) write path; see DirectoryUser.
	ApplyDirectoryUser(ctx context.Context, d *DirectoryUser) (*User, error)
	// DeleteDirectoryUser deletes a directory account; eventTime orders it like ApplyDirectoryUser.
	DeleteDirectoryUser(ctx context.Context, id string, eventTime int64) error
	ResetAdminPassword(ctx context.Context, userID, newHash string) error
	RenameLocalAdmin(ctx context.Context, oldName, newName string) error
	SetDirectoryRole(ctx context.Context, subject, role string) error
	CompletePasswordChange(ctx context.Context, userID, oldHash, newHash, ip string) error
	DeleteUser(ctx context.Context, id string) error
	// OpenInNewTab is the user's choice to open files in a new browser tab (default true).
	// It lives outside User so profile and directory updates never overwrite it.
	OpenInNewTab(ctx context.Context, userID string) (bool, error)
	SetOpenInNewTab(ctx context.Context, userID string, v bool) error
	ListUsers(ctx context.Context, offset, limit int, search string) ([]*User, int, error)
	// ListDirectoryUsers pages SCIM-provisioned accounts only.
	ListDirectoryUsers(ctx context.Context, offset, limit int) ([]*User, int, error)
	CountUsers(ctx context.Context) (int, error)
}

UserStore defines repository operations for accounts.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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