Documentation
¶
Overview ¶
Package authdb provides SQLite-backed persistence for authentication data (users, sessions, passkeys, API keys, TOTP, OIDC state). It implements authstore.AuthStore and is embedded by the parent store.DB to satisfy that interface without coupling auth persistence to subtitle/search state.
Index ¶
- Variables
- type AuthDB
- func (a *AuthDB) BatchUpdateSessionActivity(ctx context.Context, tokenHashes []string, now time.Time) error
- func (a *AuthDB) CleanupExpiredOIDCStates(ctx context.Context, now time.Time, maxAge time.Duration) (int64, error)
- func (a *AuthDB) CleanupExpiredSessions(ctx context.Context, now time.Time, idleTimeout, absTimeout time.Duration) (int64, error)
- func (a *AuthDB) Close(_ context.Context) error
- func (a *AuthDB) ConsumeOIDCState(ctx context.Context, state string) (nonce, codeVerifier, redirectURI string, err error)
- func (a *AuthDB) CreateAPIKey(ctx context.Context, key *api.Key) error
- func (a *AuthDB) CreateOIDCState(ctx context.Context, state, nonce, codeVerifier, redirectURI string) error
- func (a *AuthDB) CreatePasskey(ctx context.Context, cred *api.PasskeyCredential) error
- func (a *AuthDB) CreateSession(ctx context.Context, sess *api.Session) error
- func (a *AuthDB) CreateUser(ctx context.Context, user *api.User) error
- func (a *AuthDB) DeleteAPIKey(ctx context.Context, id, userID int64) error
- func (a *AuthDB) DeletePasskey(ctx context.Context, id, userID int64) error
- func (a *AuthDB) DeleteSession(ctx context.Context, tokenHash string) error
- func (a *AuthDB) DeleteUser(ctx context.Context, id int64) error
- func (a *AuthDB) DeleteUserSessions(ctx context.Context, userID int64, exceptHash string) error
- func (a *AuthDB) GetAPIKeyByHash(ctx context.Context, hash string) (*api.Key, error)
- func (a *AuthDB) GetPasskeyByCredentialID(ctx context.Context, credID []byte) (*api.PasskeyCredential, error)
- func (a *AuthDB) GetPasskeysByUserID(ctx context.Context, userID int64) ([]api.PasskeyCredential, error)
- func (a *AuthDB) GetSessionByHash(ctx context.Context, tokenHash string) (*api.Session, error)
- func (a *AuthDB) GetUserByEmail(ctx context.Context, email string) (*api.User, error)
- func (a *AuthDB) GetUserByID(ctx context.Context, id int64) (*api.User, error)
- func (a *AuthDB) GetUserByOIDCSub(ctx context.Context, issuer, sub string) (*api.User, error)
- func (a *AuthDB) GetUserByUsername(ctx context.Context, username string) (*api.User, error)
- func (a *AuthDB) ListAPIKeysByUserID(ctx context.Context, userID int64) ([]api.Key, error)
- func (a *AuthDB) ListUsers(ctx context.Context) ([]api.User, error)
- func (a *AuthDB) PasskeyCountForUser(ctx context.Context, userID int64) (int, error)
- func (a *AuthDB) RenamePasskey(ctx context.Context, id, userID int64, name string) error
- func (a *AuthDB) UpdatePasskeyAfterLogin(ctx context.Context, credID []byte, signCount uint32, flags api.PasskeyFlags) error
- func (a *AuthDB) UpdateSessionActivity(ctx context.Context, tokenHash string, now time.Time) error
- func (a *AuthDB) UpdateUser(ctx context.Context, user *api.User) error
- func (a *AuthDB) UserCount(ctx context.Context) (int, error)
Constants ¶
This section is empty.
Variables ¶
var ErrOIDCStateNotFound = errors.New("oidc state not found")
ErrOIDCStateNotFound is returned when ConsumeOIDCState cannot find the requested state entry (expired or never existed).
Functions ¶
This section is empty.
Types ¶
type AuthDB ¶
type AuthDB struct {
// contains filtered or unexported fields
}
AuthDB wraps a shared *sql.DB for auth-specific persistence.
func New ¶
New creates an AuthDB using the provided database connection. It prepares frequently-used statements for auth queries.
func (*AuthDB) BatchUpdateSessionActivity ¶
func (a *AuthDB) BatchUpdateSessionActivity(ctx context.Context, tokenHashes []string, now time.Time) error
BatchUpdateSessionActivity updates the last_activity timestamp for multiple sessions in a single transaction.
func (*AuthDB) CleanupExpiredOIDCStates ¶
func (a *AuthDB) CleanupExpiredOIDCStates(ctx context.Context, now time.Time, maxAge time.Duration) (int64, error)
CleanupExpiredOIDCStates removes OIDC state entries older than maxAge.
func (*AuthDB) CleanupExpiredSessions ¶
func (a *AuthDB) CleanupExpiredSessions(ctx context.Context, now time.Time, idleTimeout, absTimeout time.Duration) (int64, error)
CleanupExpiredSessions removes sessions that have exceeded either the idle timeout or the absolute timeout. Deletes in batches of 100. Returns total deleted.
func (*AuthDB) Close ¶
Close closes prepared statements. The underlying *sql.DB is NOT closed because it is shared with the parent store.
func (*AuthDB) ConsumeOIDCState ¶
func (a *AuthDB) ConsumeOIDCState(ctx context.Context, state string) (nonce, codeVerifier, redirectURI string, err error)
ConsumeOIDCState atomically retrieves and deletes an unexpired OIDC state entry. An expired entry (older than oidcStateMaxAge) is treated as not found. The age check uses SQLite's own clock (UTC, matching created_at's CURRENT_TIMESTAMP default) to avoid Go-local vs DB-UTC timezone skew.
func (*AuthDB) CreateAPIKey ¶
CreateAPIKey inserts a new API key and sets the ID from LastInsertId.
func (*AuthDB) CreateOIDCState ¶
func (a *AuthDB) CreateOIDCState(ctx context.Context, state, nonce, codeVerifier, redirectURI string) error
CreateOIDCState inserts a new OIDC state entry for the authorization flow.
func (*AuthDB) CreatePasskey ¶
CreatePasskey inserts a new passkey credential and sets the ID from LastInsertId.
func (*AuthDB) CreateSession ¶
CreateSession inserts a new session record.
func (*AuthDB) CreateUser ¶
CreateUser inserts a new user and sets the ID on the struct from LastInsertId.
func (*AuthDB) DeleteAPIKey ¶
DeleteAPIKey removes the API key with the given ID owned by the given user.
func (*AuthDB) DeletePasskey ¶
DeletePasskey removes the passkey with the given ID owned by the given user.
func (*AuthDB) DeleteSession ¶
DeleteSession removes the session with the given token hash.
func (*AuthDB) DeleteUser ¶
DeleteUser removes the user with the given ID. CASCADE handles related rows.
func (*AuthDB) DeleteUserSessions ¶
DeleteUserSessions removes all sessions for a user except the one identified by exceptHash (the current session to keep).
func (*AuthDB) GetAPIKeyByHash ¶
GetAPIKeyByHash returns the API key with the given hash, or nil if not found.
func (*AuthDB) GetPasskeyByCredentialID ¶
func (a *AuthDB) GetPasskeyByCredentialID(ctx context.Context, credID []byte) (*api.PasskeyCredential, error)
GetPasskeyByCredentialID returns the passkey with the given credential ID, or nil if not found.
func (*AuthDB) GetPasskeysByUserID ¶
func (a *AuthDB) GetPasskeysByUserID(ctx context.Context, userID int64) ([]api.PasskeyCredential, error)
GetPasskeysByUserID returns all passkeys for a user, ordered by creation date.
func (*AuthDB) GetSessionByHash ¶
GetSessionByHash returns the session with the given token hash, or nil if not found.
func (*AuthDB) GetUserByEmail ¶
GetUserByEmail returns the user with the given email (case-insensitive), or nil if not found.
func (*AuthDB) GetUserByID ¶
GetUserByID returns the user with the given ID, or nil if not found.
func (*AuthDB) GetUserByOIDCSub ¶
GetUserByOIDCSub returns the user with the given OIDC (issuer, subject) identity, or nil if not found. Matching on subject alone is unsafe: a subject is only unique within its issuer, so a changed or second IdP could collide and resolve to the wrong account.
func (*AuthDB) GetUserByUsername ¶
GetUserByUsername returns the user with the given username (case-insensitive), or nil if not found.
func (*AuthDB) ListAPIKeysByUserID ¶
ListAPIKeysByUserID returns all API keys for a user, ordered by creation date descending (newest first).
func (*AuthDB) PasskeyCountForUser ¶
PasskeyCountForUser returns the number of passkeys registered for a user.
func (*AuthDB) RenamePasskey ¶
RenamePasskey updates the friendly name of a passkey owned by the given user.
func (*AuthDB) UpdatePasskeyAfterLogin ¶
func (a *AuthDB) UpdatePasskeyAfterLogin(ctx context.Context, credID []byte, signCount uint32, flags api.PasskeyFlags) error
UpdatePasskeyAfterLogin updates the sign count and authenticator flags for a passkey after a successful login ceremony.
func (*AuthDB) UpdateSessionActivity ¶
UpdateSessionActivity updates the last_activity timestamp for a session.
func (*AuthDB) UpdateUser ¶
UpdateUser updates all mutable fields for the given user.