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 ¶
- func DSN(path string) string
- func Migrate(ctx context.Context, db *sql.DB) (err error)
- func Migrations() fs.FS
- type Store
- func (s *Store) AdvanceTOTP(ctx context.Context, u auth.UserID, step int64, at time.Time, sealed []byte, ...) error
- func (s *Store) Agent(ctx context.Context, id auth.AgentID) (auth.Agent, error)
- func (s *Store) AgentByClaimCode(ctx context.Context, code [32]byte) (auth.Agent, error)
- func (s *Store) CeremonyByID(ctx context.Context, id [32]byte, now time.Time) (auth.Ceremony, error)
- func (s *Store) ChainEvents(ctx context.Context, after int64, limit int) ([]auth.Event, error)
- func (s *Store) ClaimAgent(ctx context.Context, code [32]byte, c auth.Claim, rec auth.SessionRecord, ...) (auth.Agent, int, error)
- func (s *Store) ClaimEmail(ctx context.Context, c auth.EmailClaim) error
- func (s *Store) Close() error
- func (s *Store) ConsumeCeremony(ctx context.Context, id [32]byte, now time.Time, kinds ...string) (auth.Ceremony, error)
- func (s *Store) ConsumeRecoveryCode(ctx context.Context, h [32]byte, ev auth.Event) (auth.UserID, int, error)
- func (s *Store) CountRecoveryCodes(ctx context.Context, u auth.UserID) (int, error)
- func (s *Store) Counts(ctx context.Context, now, since time.Time) (auth.Counts, error)
- func (s *Store) CredentialByID(ctx context.Context, id []byte) (auth.CredentialRecord, error)
- func (s *Store) DecideDeviceCode(ctx context.Context, userHash [32]byte, d auth.DeviceDecision, ev auth.Event) error
- func (s *Store) DeleteCredential(ctx context.Context, u auth.UserID, id []byte, ev auth.Event) error
- func (s *Store) DeleteEmail(ctx context.Context, u auth.UserID, ev auth.Event) error
- func (s *Store) DeleteExpired(ctx context.Context, before time.Time) (int, error)
- func (s *Store) DeleteExpiredCeremonies(ctx context.Context, before time.Time) (int, error)
- func (s *Store) DeleteExpiredDeviceCodes(ctx context.Context, before time.Time) (int, error)
- func (s *Store) DeleteIdentity(ctx context.Context, u auth.UserID, issuer, subject string, ev auth.Event) error
- func (s *Store) DeletePassword(ctx context.Context, u auth.UserID, ev auth.Event) error
- func (s *Store) DeleteSignup(ctx context.Context, code [32]byte, ev auth.Event) (auth.Agent, int, error)
- func (s *Store) DeleteTOTP(ctx context.Context, u auth.UserID, ev auth.Event) error
- func (s *Store) DeleteUserFactors(ctx context.Context, u auth.UserID, ev auth.Event) error
- func (s *Store) DeviceByUserCode(ctx context.Context, userHash [32]byte, now time.Time) (auth.DeviceCode, error)
- func (s *Store) EmailByUser(ctx context.Context, u auth.UserID) (auth.EmailRecord, error)
- func (s *Store) ExpireUnclaimed(ctx context.Context, now time.Time, limit int, ev auth.Event) ([]auth.Event, error)
- func (s *Store) ExportAgents(ctx context.Context, fn func(auth.Agent) error) error
- func (s *Store) ExportUsers(ctx context.Context, fn func(auth.UserExport) error) error
- func (s *Store) FailTOTP(ctx context.Context, u auth.UserID, limit int, at time.Time, lockEv auth.Event) (failures int, locked bool, err error)
- func (s *Store) IdentityByKey(ctx context.Context, issuer, subject string) (auth.IdentityRecord, error)
- func (s *Store) ImportUsers(ctx context.Context, in auth.Import, ev auth.Event) (auth.ImportResult, error)
- func (s *Store) InsertAgent(ctx context.Context, a auth.Agent, maxOwned int, ev auth.Event) error
- func (s *Store) InsertCredential(ctx context.Context, c auth.CredentialRecord, first bool, ev auth.Event) error
- func (s *Store) InsertDeviceCode(ctx context.Context, d auth.DeviceCode) error
- func (s *Store) InsertIdentity(ctx context.Context, r auth.IdentityRecord, first bool, ev auth.Event) error
- func (s *Store) InsertSession(ctx context.Context, r auth.SessionRecord, ev auth.Event) error
- func (s *Store) InsertSignup(ctx context.Context, a auth.Agent, c auth.SignupClaim, rec auth.SessionRecord, ...) error
- func (s *Store) ListAgents(ctx context.Context, f auth.AgentFilter) ([]auth.Agent, error)
- func (s *Store) ListCredentials(ctx context.Context, u auth.UserID) ([]auth.CredentialRecord, error)
- func (s *Store) ListEvents(ctx context.Context, f auth.EventFilter) ([]auth.Event, error)
- func (s *Store) ListIdentities(ctx context.Context, u auth.UserID) ([]auth.IdentityRecord, error)
- func (s *Store) ListSessions(ctx context.Context, u auth.UserID) ([]auth.SessionRecord, error)
- func (s *Store) ListUsers(ctx context.Context, f auth.UserFilter, now time.Time) ([]auth.UserSummary, error)
- func (s *Store) Password(ctx context.Context, u auth.UserID) (auth.PasswordRecord, error)
- func (s *Store) PasswordByEmail(ctx context.Context, key string) (auth.EmailRecord, auth.PasswordRecord, error)
- func (s *Store) PollDeviceCode(ctx context.Context, deviceHash [32]byte, now time.Time) (auth.DeviceCode, error)
- func (s *Store) PutCeremony(ctx context.Context, c auth.Ceremony) error
- func (s *Store) PutSignupOverride(ctx context.Context, o auth.SignupOverride, version int64, ev auth.Event) (int64, error)
- func (s *Store) PutTOTP(ctx context.Context, r auth.TOTPRecord) error
- func (s *Store) RecordEvent(ctx context.Context, ev auth.Event) error
- func (s *Store) RecoveryCodeOwner(ctx context.Context, h [32]byte) (auth.UserID, error)
- func (s *Store) RehashPassword(ctx context.Context, u auth.UserID, old, hash string) error
- func (s *Store) RenameCredential(ctx context.Context, u auth.UserID, id []byte, name string, ev auth.Event) error
- func (s *Store) ReplaceRecoveryCodes(ctx context.Context, u auth.UserID, hashes [][32]byte, ev auth.Event) error
- func (s *Store) RevokeAgent(ctx context.Context, id auth.AgentID, ev auth.Event) (int, error)
- func (s *Store) RevokeTree(ctx context.Context, root auth.SessionID, ev auth.Event) (int, error)
- func (s *Store) RevokeUser(ctx context.Context, u auth.UserID, ev auth.Event) (int, error)
- func (s *Store) SealEvents(ctx context.Context, limit int, ...) (int, error)
- func (s *Store) SessionByHash(ctx context.Context, h [32]byte) (auth.SessionRecord, auth.HashMatch, error)
- func (s *Store) SetPassword(ctx context.Context, set auth.PasswordSet, ev auth.Event) error
- func (s *Store) SignupOverride(ctx context.Context) (auth.SignupOverride, int64, error)
- func (s *Store) SwapToken(ctx context.Context, id auth.SessionID, sw auth.Swap, ev auth.Event) error
- func (s *Store) TOTP(ctx context.Context, u auth.UserID) (auth.TOTPRecord, error)
- func (s *Store) Touch(ctx context.Context, id auth.SessionID, seen time.Time) error
- func (s *Store) TouchIdentity(ctx context.Context, issuer, subject string, at time.Time) error
- func (s *Store) UpdateCredentialUse(ctx context.Context, id []byte, prevCount uint32, c auth.CredentialRecord, ...) error
- func (s *Store) UserByHandle(ctx context.Context, h []byte) (auth.UserID, error)
- func (s *Store) UserHandle(ctx context.Context, u auth.UserID, fresh []byte) ([]byte, error)
- func (s *Store) VerifyEmail(ctx context.Context, u auth.UserID, key string, at time.Time, ev auth.Event) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Agent implements auth.Store.
func (*Store) AgentByClaimCode ¶
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 ¶
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 ¶
ClaimEmail implements auth.EmailStore.
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 ¶
CountRecoveryCodes implements auth.FactorStore.
func (*Store) Counts ¶
Counts implements auth.Store.
func (*Store) CredentialByID ¶
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 ¶
DeleteEmail implements auth.EmailStore.
func (*Store) DeleteExpired ¶
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 ¶
DeleteExpiredCeremonies implements auth.FactorStore.
func (*Store) DeleteExpiredDeviceCodes ¶
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 ¶
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 ¶
DeleteTOTP implements auth.FactorStore.
func (*Store) DeleteUserFactors ¶
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 ¶
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 ¶
ExportAgents implements auth.TransferStore.
func (*Store) ExportUsers ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ListEvents implements auth.Store.
func (*Store) ListIdentities ¶
ListIdentities implements auth.IdentityStore.
func (*Store) ListSessions ¶
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 ¶
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 ¶
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 ¶
PutTOTP implements auth.FactorStore.
func (*Store) RecordEvent ¶
RecordEvent implements auth.Store.
func (*Store) RecoveryCodeOwner ¶
RecoveryCodeOwner implements auth.FactorStore.
func (*Store) RehashPassword ¶
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 ¶
RevokeAgent implements auth.Store.
func (*Store) RevokeTree ¶
RevokeTree implements auth.Store.
func (*Store) RevokeUser ¶
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 ¶
SetPassword implements auth.EmailStore.
func (*Store) SignupOverride ¶
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 ¶
TOTP implements auth.FactorStore.
func (*Store) Touch ¶
Touch implements auth.Store.
func (*Store) TouchIdentity ¶
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 ¶
UserByHandle implements auth.FactorStore.
func (*Store) UserHandle ¶
UserHandle implements auth.FactorStore.