sqlitestore

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 15 Imported by: ? 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.

README

sqlitestore

Package sqlitestore is an auth.Store backed by SQLite, for local and small installs. It uses the cgo-free ncruces/go-sqlite3 driver through database/sql.

The package documentation is in sqlitestore.go. To read it with the API, run go doc ./sqlitestore.

Contents

Name What it holds
sqlitestore.go Store, Open, New, DSN, Migrate, Migrations, sessions, and agents; the package documentation
factors.go Passkey credentials, ceremonies, recovery codes, and TOTP factors
emails.go Email addresses and passwords
identities.go Federated identities
transfer.go Export and import of every sign-in record (auth.TransferStore)
devices.go Device authorization codes
signup.go Agent signups and claims
signup_settings.go Signup settings
migrations/ The SQL migrations

The tests are in the *_test.go files next to the code.

See also

Documentation

Overview

Package sqlitestore is an auth.Store backed by SQLite, for local and small installs. It uses the cgo-free ncruces/go-sqlite3 driver through database/sql.

SQLite allows one writer at a time. Every write runs in a transaction that starts with BEGIN IMMEDIATE, so operations that check and then write are atomic, and concurrent writers wait up to the busy timeout instead of failing.

Open a store with Open, or pass a *sql.DB opened with DSN to New:

st, err := sqlitestore.Open(ctx, "auth.db")
if err != nil {
	return err
}
defer st.Close()
svc, err := auth.New(st)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSN

func DSN(path string) string

DSN returns the data source name to open path with: immediate transactions, a 5-second busy timeout, WAL mode, synchronous=NORMAL, and foreign keys on.

func Migrate

func Migrate(ctx context.Context, db *sql.DB) (err error)

Migrate applies pending migrations in one transaction. It is safe to call on every start.

Migrations run with foreign keys off, so a migration can rebuild a table as SQLite's ALTER TABLE documentation describes, and Migrate checks every foreign key with PRAGMA foreign_key_check before it commits.

func Migrations

func Migrations() fs.FS

Migrations returns the SQL migrations, for apps that run them with their own migration tool instead of Migrate.

Types

type Store

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

Store is safe for concurrent use.

func New

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

New returns a store that uses db, which must be opened with DSN and migrated with Migrate or Migrations. New checks that foreign keys are on. The caller keeps ownership of db.

func Open

func Open(ctx context.Context, path string) (*Store, error)

Open opens or creates the database at path, applies any pending migrations, and returns a store that owns the database. Call Close when you're done.

func (*Store) AdvanceTOTP

func (s *Store) AdvanceTOTP(ctx context.Context, u auth.UserID, step int64, at time.Time, sealed []byte, ev *auth.Event) error

AdvanceTOTP implements auth.FactorStore.

func (*Store) Agent

func (s *Store) Agent(ctx context.Context, id auth.AgentID) (auth.Agent, error)

Agent implements auth.Store.

func (*Store) AgentByClaimCode

func (s *Store) AgentByClaimCode(ctx context.Context, code [32]byte) (auth.Agent, error)

AgentByClaimCode implements auth.SignupStore.

func (*Store) CeremonyByID

func (s *Store) CeremonyByID(ctx context.Context, id [32]byte, now time.Time) (auth.Ceremony, error)

CeremonyByID implements auth.FactorStore.

func (*Store) ChainEvents

func (s *Store) ChainEvents(ctx context.Context, after int64, limit int) ([]auth.Event, error)

ChainEvents implements auth.Store.

func (*Store) ClaimAgent

func (s *Store) ClaimAgent(ctx context.Context, code [32]byte, c auth.Claim, rec auth.SessionRecord, ev auth.Event) (auth.Agent, int, error)

ClaimAgent implements auth.SignupStore.

func (*Store) ClaimEmail

func (s *Store) ClaimEmail(ctx context.Context, c auth.EmailClaim) error

ClaimEmail implements auth.EmailStore.

func (*Store) Close

func (s *Store) Close() error

Close closes the database if the store opened it with Open.

func (*Store) ConsumeCeremony

func (s *Store) ConsumeCeremony(ctx context.Context, id [32]byte, now time.Time, kinds ...string) (auth.Ceremony, error)

ConsumeCeremony implements auth.FactorStore.

func (*Store) ConsumeRecoveryCode

func (s *Store) ConsumeRecoveryCode(ctx context.Context, h [32]byte, ev auth.Event) (auth.UserID, int, error)

ConsumeRecoveryCode implements auth.FactorStore.

func (*Store) CountRecoveryCodes

func (s *Store) CountRecoveryCodes(ctx context.Context, u auth.UserID) (int, error)

CountRecoveryCodes implements auth.FactorStore.

func (*Store) Counts

func (s *Store) Counts(ctx context.Context, now, since time.Time) (auth.Counts, error)

Counts implements auth.Store.

func (*Store) CredentialByID

func (s *Store) CredentialByID(ctx context.Context, id []byte) (auth.CredentialRecord, error)

CredentialByID implements auth.FactorStore.

func (*Store) DecideDeviceCode

func (s *Store) DecideDeviceCode(ctx context.Context, userHash [32]byte, d auth.DeviceDecision, ev auth.Event) error

DecideDeviceCode implements auth.DeviceStore.

func (*Store) DeleteCredential

func (s *Store) DeleteCredential(ctx context.Context, u auth.UserID, id []byte, ev auth.Event) error

DeleteCredential implements auth.FactorStore.

func (*Store) DeleteEmail

func (s *Store) DeleteEmail(ctx context.Context, u auth.UserID, ev auth.Event) error

DeleteEmail implements auth.EmailStore.

func (*Store) DeleteExpired

func (s *Store) DeleteExpired(ctx context.Context, before time.Time) (int, error)

DeleteExpired implements auth.Store. Deleting a session also deletes its token hashes and, through the parent key, its descendants, which never outlive it.

func (*Store) DeleteExpiredCeremonies

func (s *Store) DeleteExpiredCeremonies(ctx context.Context, before time.Time) (int, error)

DeleteExpiredCeremonies implements auth.FactorStore.

func (*Store) DeleteExpiredDeviceCodes

func (s *Store) DeleteExpiredDeviceCodes(ctx context.Context, before time.Time) (int, error)

DeleteExpiredDeviceCodes implements auth.DeviceStore.

func (*Store) DeleteIdentity

func (s *Store) DeleteIdentity(ctx context.Context, u auth.UserID, issuer, subject string, ev auth.Event) error

DeleteIdentity implements auth.IdentityStore.

func (*Store) DeletePassword

func (s *Store) DeletePassword(ctx context.Context, u auth.UserID, ev auth.Event) error

DeletePassword implements auth.EmailStore.

func (*Store) DeleteSignup

func (s *Store) DeleteSignup(ctx context.Context, code [32]byte, ev auth.Event) (auth.Agent, int, error)

DeleteSignup implements auth.SignupStore.

func (*Store) DeleteTOTP

func (s *Store) DeleteTOTP(ctx context.Context, u auth.UserID, ev auth.Event) error

DeleteTOTP implements auth.FactorStore.

func (*Store) DeleteUserFactors

func (s *Store) DeleteUserFactors(ctx context.Context, u auth.UserID, ev auth.Event) error

DeleteUserFactors implements auth.FactorStore.

func (*Store) DeviceByUserCode

func (s *Store) DeviceByUserCode(ctx context.Context, userHash [32]byte, now time.Time) (auth.DeviceCode, error)

DeviceByUserCode implements auth.DeviceStore.

func (*Store) EmailByUser

func (s *Store) EmailByUser(ctx context.Context, u auth.UserID) (auth.EmailRecord, error)

EmailByUser implements auth.EmailStore.

func (*Store) ExpireUnclaimed

func (s *Store) ExpireUnclaimed(ctx context.Context, now time.Time, limit int, ev auth.Event) ([]auth.Event, error)

ExpireUnclaimed implements auth.SignupStore. It deletes the whole batch in one transaction.

func (*Store) ExportAgents

func (s *Store) ExportAgents(ctx context.Context, fn func(auth.Agent) error) error

ExportAgents implements auth.TransferStore.

func (*Store) ExportUsers

func (s *Store) ExportUsers(ctx context.Context, fn func(auth.UserExport) error) error

ExportUsers implements auth.TransferStore. TEXT and BLOB compare byte by byte, so ORDER BY gives byte order.

func (*Store) FailTOTP

func (s *Store) FailTOTP(ctx context.Context, u auth.UserID, limit int, at time.Time, lockEv auth.Event) (failures int, locked bool, err error)

FailTOTP implements auth.FactorStore.

func (*Store) IdentityByKey

func (s *Store) IdentityByKey(ctx context.Context, issuer, subject string) (auth.IdentityRecord, error)

IdentityByKey implements auth.IdentityStore.

func (*Store) ImportUsers

func (s *Store) ImportUsers(ctx context.Context, in auth.Import, ev auth.Event) (auth.ImportResult, error)

ImportUsers implements auth.TransferStore. It runs in one immediate transaction, so no other writer runs between its checks and its inserts.

func (*Store) InsertAgent

func (s *Store) InsertAgent(ctx context.Context, a auth.Agent, maxOwned int, ev auth.Event) error

InsertAgent implements auth.Store.

func (*Store) InsertCredential

func (s *Store) InsertCredential(ctx context.Context, c auth.CredentialRecord, first bool, ev auth.Event) error

InsertCredential implements auth.FactorStore.

func (*Store) InsertDeviceCode

func (s *Store) InsertDeviceCode(ctx context.Context, d auth.DeviceCode) error

InsertDeviceCode implements auth.DeviceStore.

func (*Store) InsertIdentity

func (s *Store) InsertIdentity(ctx context.Context, r auth.IdentityRecord, first bool, ev auth.Event) error

InsertIdentity implements auth.IdentityStore.

func (*Store) InsertSession

func (s *Store) InsertSession(ctx context.Context, r auth.SessionRecord, ev auth.Event) error

InsertSession implements auth.Store.

func (*Store) InsertSignup

func (s *Store) InsertSignup(ctx context.Context, a auth.Agent, c auth.SignupClaim, rec auth.SessionRecord, maxUnclaimed int, ev auth.Event) error

InsertSignup implements auth.SignupStore. The transaction starts with BEGIN IMMEDIATE, so the quota count and the insert can't interleave with another signup.

func (*Store) ListAgents

func (s *Store) ListAgents(ctx context.Context, f auth.AgentFilter) ([]auth.Agent, error)

ListAgents implements auth.Store.

func (*Store) ListCredentials

func (s *Store) ListCredentials(ctx context.Context, u auth.UserID) ([]auth.CredentialRecord, error)

ListCredentials implements auth.FactorStore.

func (*Store) ListEvents

func (s *Store) ListEvents(ctx context.Context, f auth.EventFilter) ([]auth.Event, error)

ListEvents implements auth.Store.

func (*Store) ListIdentities

func (s *Store) ListIdentities(ctx context.Context, u auth.UserID) ([]auth.IdentityRecord, error)

ListIdentities implements auth.IdentityStore.

func (*Store) ListSessions

func (s *Store) ListSessions(ctx context.Context, u auth.UserID) ([]auth.SessionRecord, error)

ListSessions implements auth.Store. Results are ordered by creation time.

func (*Store) ListUsers

func (s *Store) ListUsers(ctx context.Context, f auth.UserFilter, now time.Time) ([]auth.UserSummary, error)

ListUsers implements auth.Store.

func (*Store) Password

func (s *Store) Password(ctx context.Context, u auth.UserID) (auth.PasswordRecord, error)

Password implements auth.EmailStore.

func (*Store) PasswordByEmail

func (s *Store) PasswordByEmail(ctx context.Context, key string) (auth.EmailRecord, auth.PasswordRecord, error)

PasswordByEmail implements auth.EmailStore.

func (*Store) PollDeviceCode

func (s *Store) PollDeviceCode(ctx context.Context, deviceHash [32]byte, now time.Time) (auth.DeviceCode, error)

PollDeviceCode implements auth.DeviceStore.

func (*Store) PutCeremony

func (s *Store) PutCeremony(ctx context.Context, c auth.Ceremony) error

PutCeremony implements auth.FactorStore.

func (*Store) PutSignupOverride

func (s *Store) PutSignupOverride(ctx context.Context, o auth.SignupOverride, version int64, ev auth.Event) (int64, error)

PutSignupOverride implements auth.SignupSettingsStore.

func (*Store) PutTOTP

func (s *Store) PutTOTP(ctx context.Context, r auth.TOTPRecord) error

PutTOTP implements auth.FactorStore.

func (*Store) RecordEvent

func (s *Store) RecordEvent(ctx context.Context, ev auth.Event) error

RecordEvent implements auth.Store.

func (*Store) RecoveryCodeOwner

func (s *Store) RecoveryCodeOwner(ctx context.Context, h [32]byte) (auth.UserID, error)

RecoveryCodeOwner implements auth.FactorStore.

func (*Store) RehashPassword

func (s *Store) RehashPassword(ctx context.Context, u auth.UserID, old, hash string) error

RehashPassword implements auth.EmailStore.

func (*Store) RenameCredential

func (s *Store) RenameCredential(ctx context.Context, u auth.UserID, id []byte, name string, ev auth.Event) error

RenameCredential implements auth.FactorStore.

func (*Store) ReplaceRecoveryCodes

func (s *Store) ReplaceRecoveryCodes(ctx context.Context, u auth.UserID, hashes [][32]byte, ev auth.Event) error

ReplaceRecoveryCodes implements auth.FactorStore.

func (*Store) RevokeAgent

func (s *Store) RevokeAgent(ctx context.Context, id auth.AgentID, ev auth.Event) (int, error)

RevokeAgent implements auth.Store.

func (*Store) RevokeTree

func (s *Store) RevokeTree(ctx context.Context, root auth.SessionID, ev auth.Event) (int, error)

RevokeTree implements auth.Store.

func (*Store) RevokeUser

func (s *Store) RevokeUser(ctx context.Context, u auth.UserID, ev auth.Event) (int, error)

RevokeUser implements auth.Store.

func (*Store) SealEvents

func (s *Store) SealEvents(ctx context.Context, limit int,
	seal func(head auth.ChainHead, evs []auth.Event) ([]auth.ChainLink, error),
) (int, error)

SealEvents implements auth.Store. The transaction begins IMMEDIATE, which serializes it against every other writer.

func (*Store) SessionByHash

func (s *Store) SessionByHash(ctx context.Context, h [32]byte) (auth.SessionRecord, auth.HashMatch, error)

SessionByHash implements auth.Store.

func (*Store) SetPassword

func (s *Store) SetPassword(ctx context.Context, set auth.PasswordSet, ev auth.Event) error

SetPassword implements auth.EmailStore.

func (*Store) SignupOverride

func (s *Store) SignupOverride(ctx context.Context) (auth.SignupOverride, int64, error)

SignupOverride implements auth.SignupSettingsStore.

func (*Store) SwapToken

func (s *Store) SwapToken(ctx context.Context, id auth.SessionID, sw auth.Swap, ev auth.Event) error

SwapToken implements auth.Store.

func (*Store) TOTP

func (s *Store) TOTP(ctx context.Context, u auth.UserID) (auth.TOTPRecord, error)

TOTP implements auth.FactorStore.

func (*Store) Touch

func (s *Store) Touch(ctx context.Context, id auth.SessionID, seen time.Time) error

Touch implements auth.Store.

func (*Store) TouchIdentity

func (s *Store) TouchIdentity(ctx context.Context, issuer, subject string, at time.Time) error

TouchIdentity implements auth.IdentityStore.

func (*Store) UpdateCredentialUse

func (s *Store) UpdateCredentialUse(ctx context.Context, id []byte, prevCount uint32, c auth.CredentialRecord,
	ev *auth.Event,
) error

UpdateCredentialUse implements auth.FactorStore.

func (*Store) UserByHandle

func (s *Store) UserByHandle(ctx context.Context, h []byte) (auth.UserID, error)

UserByHandle implements auth.FactorStore.

func (*Store) UserHandle

func (s *Store) UserHandle(ctx context.Context, u auth.UserID, fresh []byte) ([]byte, error)

UserHandle implements auth.FactorStore.

func (*Store) VerifyEmail

func (s *Store) VerifyEmail(ctx context.Context, u auth.UserID, key string, at time.Time, ev auth.Event) error

VerifyEmail implements auth.EmailStore.

Jump to

Keyboard shortcuts

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