authdb

package
v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: GPL-2.0, GPL-3.0 Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

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

func New(ctx context.Context, db *sql.DB) (*AuthDB, error)

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

func (a *AuthDB) Close(_ context.Context) error

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

func (a *AuthDB) CreateAPIKey(ctx context.Context, key *api.Key) error

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

func (a *AuthDB) CreatePasskey(ctx context.Context, cred *api.PasskeyCredential) error

CreatePasskey inserts a new passkey credential and sets the ID from LastInsertId.

func (*AuthDB) CreateSession

func (a *AuthDB) CreateSession(ctx context.Context, sess *api.Session) error

CreateSession inserts a new session record.

func (*AuthDB) CreateUser

func (a *AuthDB) CreateUser(ctx context.Context, user *api.User) error

CreateUser inserts a new user and sets the ID on the struct from LastInsertId.

func (*AuthDB) DeleteAPIKey

func (a *AuthDB) DeleteAPIKey(ctx context.Context, id, userID int64) error

DeleteAPIKey removes the API key with the given ID owned by the given user.

func (*AuthDB) DeletePasskey

func (a *AuthDB) DeletePasskey(ctx context.Context, id, userID int64) error

DeletePasskey removes the passkey with the given ID owned by the given user.

func (*AuthDB) DeleteSession

func (a *AuthDB) DeleteSession(ctx context.Context, tokenHash string) error

DeleteSession removes the session with the given token hash.

func (*AuthDB) DeleteUser

func (a *AuthDB) DeleteUser(ctx context.Context, id int64) error

DeleteUser removes the user with the given ID. CASCADE handles related rows.

func (*AuthDB) DeleteUserSessions

func (a *AuthDB) DeleteUserSessions(ctx context.Context, userID int64, exceptHash string) error

DeleteUserSessions removes all sessions for a user except the one identified by exceptHash (the current session to keep).

func (*AuthDB) GetAPIKeyByHash

func (a *AuthDB) GetAPIKeyByHash(ctx context.Context, hash string) (*api.Key, error)

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

func (a *AuthDB) GetSessionByHash(ctx context.Context, tokenHash string) (*api.Session, error)

GetSessionByHash returns the session with the given token hash, or nil if not found.

func (*AuthDB) GetUserByEmail

func (a *AuthDB) GetUserByEmail(ctx context.Context, email string) (*api.User, error)

GetUserByEmail returns the user with the given email (case-insensitive), or nil if not found.

func (*AuthDB) GetUserByID

func (a *AuthDB) GetUserByID(ctx context.Context, id int64) (*api.User, error)

GetUserByID returns the user with the given ID, or nil if not found.

func (*AuthDB) GetUserByOIDCSub

func (a *AuthDB) GetUserByOIDCSub(ctx context.Context, issuer, sub string) (*api.User, error)

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

func (a *AuthDB) GetUserByUsername(ctx context.Context, username string) (*api.User, error)

GetUserByUsername returns the user with the given username (case-insensitive), or nil if not found.

func (*AuthDB) ListAPIKeysByUserID

func (a *AuthDB) ListAPIKeysByUserID(ctx context.Context, userID int64) ([]api.Key, error)

ListAPIKeysByUserID returns all API keys for a user, ordered by creation date descending (newest first).

func (*AuthDB) ListUsers

func (a *AuthDB) ListUsers(ctx context.Context) ([]api.User, error)

ListUsers returns all users ordered by username.

func (*AuthDB) PasskeyCountForUser

func (a *AuthDB) PasskeyCountForUser(ctx context.Context, userID int64) (int, error)

PasskeyCountForUser returns the number of passkeys registered for a user.

func (*AuthDB) RenamePasskey

func (a *AuthDB) RenamePasskey(ctx context.Context, id, userID int64, name string) error

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

func (a *AuthDB) UpdateSessionActivity(ctx context.Context, tokenHash string, now time.Time) error

UpdateSessionActivity updates the last_activity timestamp for a session.

func (*AuthDB) UpdateUser

func (a *AuthDB) UpdateUser(ctx context.Context, user *api.User) error

UpdateUser updates all mutable fields for the given user.

func (*AuthDB) UserCount

func (a *AuthDB) UserCount(ctx context.Context) (int, error)

UserCount returns the total number of user accounts.

Jump to

Keyboard shortcuts

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