postgres

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 26 Imported by: 1 module, 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

Overview

Package postgres provides a production-grade implementation of theauth.Storage backed by PostgreSQL through pgx and sqlc-generated query bindings.

Migrations live under migrations/ and are append-only: a renamed column ships as a new migration. Use the in-package New constructor with a pgxpool.Pool. See https://docs.theauth.dev/go/reference/stability for the rules that govern future schema and query changes.

Package postgres: migrate.go applies the embedded SQL migrations at boot, before theauth.New. Migrate is safe across replicas (transaction-scoped advisory lock) and reruns are no-ops.

Package postgres provides a Postgres-backed storage.Storage implementation built on top of sqlc-generated queries and pgx/v5.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, pool *pgxpool.Pool) error

Migrate applies embedded up migrations not yet in theauth_schema_migrations, in version order, under a transaction-scoped advisory lock. Call it before theauth.New.

Types

type MigrationStatus added in v2.12.0

type MigrationStatus struct {
	// Version is the migration id, for example "0001_init".
	Version string
	// Applied reports whether the version is recorded in theauth_schema_migrations.
	Applied bool
	// AppliedAt is the ledger timestamp; zero when not applied.
	AppliedAt time.Time
	// SQL is the embedded up migration body.
	SQL string
}

MigrationStatus describes one embedded migration and whether the ledger has it.

func Status added in v2.12.0

func Status(ctx context.Context, pool *pgxpool.Pool) ([]MigrationStatus, error)

Status lists every embedded migration in version order with its ledger state. It is read only: it never creates the ledger table and takes no lock. A missing ledger means every migration is pending. Ledger versions unknown to this build are ignored.

type Store

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

Store is the Postgres-backed storage adapter.

func New

func New(pool *pgxpool.Pool) *Store

New constructs a Store using the given pgxpool.Pool.

func (*Store) APITokenByHash

func (s *Store) APITokenByHash(ctx context.Context, hash []byte) (*theauth.APIToken, error)

APITokenByHash returns the token with the given secret hash, else ErrNotFound.

func (*Store) APITokenByID

func (s *Store) APITokenByID(ctx context.Context, id theauth.ULID) (*theauth.APIToken, error)

APITokenByID returns the token with the given ID, else ErrNotFound.

func (*Store) APITokensByAgentID added in v2.11.0

func (s *Store) APITokensByAgentID(ctx context.Context, agentID theauth.ULID) ([]theauth.APIToken, error)

APITokensByAgentID returns every token minted for the agent, newest first.

func (*Store) APITokensByOwner

func (s *Store) APITokensByOwner(ctx context.Context, ownerID theauth.ULID) ([]theauth.APIToken, error)

APITokensByOwner returns every token of the owner, newest first.

func (*Store) AccountStateByUserID added in v2.10.0

func (s *Store) AccountStateByUserID(ctx context.Context, userID theauth.ULID) (theauth.AccountState, error)

AccountStateByUserID returns the user's lifecycle flags; a user without a row yields the zero state.

func (*Store) AddGroupMembers

func (s *Store) AddGroupMembers(ctx context.Context, groupID theauth.ULID, userIDs []theauth.ULID) error

func (*Store) AdvanceTOTPStep

func (s *Store) AdvanceTOTPStep(ctx context.Context, userID theauth.ULID, step int64) (bool, error)

AdvanceTOTPStep records step as the user's last used TOTP step, returning false when it is not strictly newer.

func (*Store) AgentByClientID

func (s *Store) AgentByClientID(ctx context.Context, clientID string) (*theauth.Agent, error)

func (*Store) AgentByID

func (s *Store) AgentByID(ctx context.Context, id theauth.ULID) (*theauth.Agent, error)

func (*Store) AgentCredentialsByAgentID

func (s *Store) AgentCredentialsByAgentID(ctx context.Context, agentID theauth.ULID) ([]theauth.AgentCredential, error)

func (*Store) AgentsByOwner

func (s *Store) AgentsByOwner(ctx context.Context, owner theauth.AgentOwner) ([]theauth.Agent, error)

func (*Store) AppendSessionAuthMethods added in v2.11.0

func (s *Store) AppendSessionAuthMethods(ctx context.Context, id theauth.ULID, methods []string, at time.Time) ([]string, time.Time, error)

AppendSessionAuthMethods merges methods and advances auth_time under a FOR UPDATE row lock, so concurrent appends never lose methods.

func (*Store) ApproveToolApproval added in v2.11.0

func (s *Store) ApproveToolApproval(ctx context.Context, id, approver string, now time.Time) (bool, error)

func (*Store) AtomicRotateJWKS

func (s *Store) AtomicRotateJWKS(ctx context.Context, retireKIDs []string, demoteKID, promoteKID string, freshKey theauth.JWKSKey, now time.Time) error

AtomicRotateJWKS implements as.JWKSAtomicRotator in one serializable transaction, so concurrent callers cannot leave two rows with state = current. The theauth_jwks_one_current partial unique index (migration 0015) enforces the same invariant in the database.

func (*Store) AuditOrderSupported added in v2.12.0

func (s *Store) AuditOrderSupported() bool

AuditOrderSupported declares write order support (storagetest.AuditOrderCapable).

func (*Store) BackchannelRequestByID

func (s *Store) BackchannelRequestByID(ctx context.Context, authReqID string) (theauth.BackchannelRequest, error)

func (*Store) ChainSupported added in v2.11.0

func (s *Store) ChainSupported() bool

ChainSupported reports that this backend keeps the tamper-evident audit hash chain.

func (*Store) ClaimDeviceCode

func (s *Store) ClaimDeviceCode(ctx context.Context, hash []byte, now time.Time) (*theauth.DeviceCode, error)

ClaimDeviceCode atomically moves an approved, unexpired request to redeemed; one concurrent caller wins.

func (*Store) ConfirmTOTPSecret

func (s *Store) ConfirmTOTPSecret(ctx context.Context, userID theauth.ULID, at time.Time) error

func (*Store) ConsumeAuthorizationCode

func (s *Store) ConsumeAuthorizationCode(ctx context.Context, code string) (*theauth.AuthorizationCode, error)

ConsumeAuthorizationCode uses DELETE ... RETURNING for atomicity: exactly one of N concurrent callers wins per code.

func (*Store) ConsumeDeviceAuthorization added in v2.8.0

func (s *Store) ConsumeDeviceAuthorization(ctx context.Context, id theauth.ULID, now time.Time) (bool, error)
func (s *Store) ConsumeMagicLink(ctx context.Context, tokenHash []byte) (*theauth.MagicLink, error)

func (*Store) ConsumePasswordResetToken

func (s *Store) ConsumePasswordResetToken(ctx context.Context, tokenHash []byte) (*theauth.PasswordResetToken, error)

func (*Store) ConsumePushedRequest

func (s *Store) ConsumePushedRequest(ctx context.Context, requestURI string) ([]byte, error)

ConsumePushedRequest atomically marks the row as consumed and returns the payload. Returns ErrNotFound when:

  • the request_uri does not exist
  • the row has already been consumed (consumed_at IS NOT NULL)
  • the row is expired (expires_at <= now)

func (*Store) ConsumeRecoveryCode

func (s *Store) ConsumeRecoveryCode(ctx context.Context, userID theauth.ULID, code string, at time.Time) error
func (s *Store) ConsumeSessionLink(ctx context.Context, tokenHash []byte, now time.Time) (*theauth.SessionLink, error)

ConsumeSessionLink atomically marks an unused, unexpired link used and returns it, else ErrNotFound.

func (*Store) ConsumeToolApproval added in v2.11.0

func (s *Store) ConsumeToolApproval(ctx context.Context, id, agentID, action, resource string, now time.Time) (bool, error)

ConsumeToolApproval implements theauth.ToolApprovalStorage. The conditional UPDATE is the single winner check: only one caller sees one affected row.

func (*Store) CountUnusedRecoveryCodes

func (s *Store) CountUnusedRecoveryCodes(ctx context.Context, userID theauth.ULID) (int, error)

func (*Store) CountUsers

func (s *Store) CountUsers(ctx context.Context) (int, error)

CountUsers returns the number of user rows.

func (*Store) CountUsersWithPermissionInOrg

func (s *Store) CountUsersWithPermissionInOrg(ctx context.Context, orgID theauth.ULID, perm string) (int, error)
func (s *Store) CreateMagicLink(ctx context.Context, ml theauth.MagicLink) error

func (*Store) CreatePasswordResetToken

func (s *Store) CreatePasswordResetToken(ctx context.Context, t theauth.PasswordResetToken) error

func (*Store) CreateSession

func (s *Store) CreateSession(ctx context.Context, sess theauth.Session) (theauth.Session, error)

CreateSession inserts a session with the default full auth level.

func (s *Store) CreateSessionLink(ctx context.Context, l theauth.SessionLink) error

CreateSessionLink stores a single-use session link.

func (*Store) CreateSessionWithAuthLevel

func (s *Store) CreateSessionWithAuthLevel(ctx context.Context, sess theauth.Session) (theauth.Session, error)

CreateSessionWithAuthLevel inserts a session with its own auth level.

func (*Store) CreateUser

func (s *Store) CreateUser(ctx context.Context, u theauth.User) (theauth.User, error)

func (*Store) DecideDeviceAuthorization added in v2.8.0

func (s *Store) DecideDeviceAuthorization(ctx context.Context, id theauth.ULID, status string, userID theauth.ULID, now time.Time) (bool, error)

func (*Store) DecideDeviceCode

func (s *Store) DecideDeviceCode(ctx context.Context, userCode string, d theauth.DeviceDecision, now time.Time) error

DecideDeviceCode moves a pending, unexpired request to approved or denied in one statement.

func (*Store) DelegationGrantByID

func (s *Store) DelegationGrantByID(ctx context.Context, id theauth.ULID) (*theauth.DelegationGrant, error)

func (*Store) DelegationGrantByUserAgentResource

func (s *Store) DelegationGrantByUserAgentResource(ctx context.Context, userID, agentID theauth.ULID, resource string) (*theauth.DelegationGrant, error)

func (*Store) DelegationGrantsByAgentID

func (s *Store) DelegationGrantsByAgentID(ctx context.Context, agentID theauth.ULID) ([]theauth.DelegationGrant, error)

func (*Store) DelegationGrantsByUserID

func (s *Store) DelegationGrantsByUserID(ctx context.Context, userID theauth.ULID) ([]theauth.DelegationGrant, error)

func (*Store) DeleteBackchannelRequest

func (s *Store) DeleteBackchannelRequest(ctx context.Context, authReqID string) error

func (*Store) DeleteExpiredDeviceAuthorizations added in v2.8.0

func (s *Store) DeleteExpiredDeviceAuthorizations(ctx context.Context, before time.Time) (int64, error)

func (*Store) DeleteExpiredDeviceCodes

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

DeleteExpiredDeviceCodes removes requests that expired before the cutoff.

func (*Store) DeleteGroup

func (s *Store) DeleteGroup(ctx context.Context, id theauth.ULID) error

func (*Store) DeleteOAuthAccountByProvider

func (s *Store) DeleteOAuthAccountByProvider(ctx context.Context, userID theauth.ULID, provider string) error

DeleteOAuthAccountByProvider removes the row for (userID, provider).

func (*Store) DeleteOAuthClient

func (s *Store) DeleteOAuthClient(ctx context.Context, clientID string) error

func (*Store) DeleteOrganization

func (s *Store) DeleteOrganization(ctx context.Context, id theauth.ULID) error

func (*Store) DeleteOrganizationMember

func (s *Store) DeleteOrganizationMember(ctx context.Context, orgID, userID theauth.ULID) error

func (*Store) DeleteRole

func (s *Store) DeleteRole(ctx context.Context, id theauth.ULID) error

func (*Store) DeleteSAMLConnection

func (s *Store) DeleteSAMLConnection(ctx context.Context, id theauth.ULID) error

func (*Store) DeleteTOTPSecret

func (s *Store) DeleteTOTPSecret(ctx context.Context, userID theauth.ULID) error

func (*Store) DeleteUserCascade added in v2.10.0

func (s *Store) DeleteUserCascade(ctx context.Context, userID theauth.ULID) error

DeleteUserCascade removes the user and its rows in one transaction. Foreign keys cascade everything except API tokens and magic links, which have no user foreign key.

func (*Store) DeleteWebAuthnCredential

func (s *Store) DeleteWebAuthnCredential(ctx context.Context, id theauth.ULID, userID theauth.ULID) error

func (*Store) DenyAccessToken added in v2.8.0

func (s *Store) DenyAccessToken(ctx context.Context, jti string, expiresAt time.Time) error

DenyAccessToken records jti as revoked until expiresAt (opt-in access token revocation).

func (*Store) DeviceAuthorizationByDeviceCodeHash added in v2.8.0

func (s *Store) DeviceAuthorizationByDeviceCodeHash(ctx context.Context, hash []byte) (*theauth.DeviceAuthorization, error)

func (*Store) DeviceAuthorizationByUserCodeHash added in v2.8.0

func (s *Store) DeviceAuthorizationByUserCodeHash(ctx context.Context, hash []byte) (*theauth.DeviceAuthorization, error)

func (*Store) DeviceByID added in v2.10.0

func (s *Store) DeviceByID(ctx context.Context, id theauth.ULID) (*theauth.Device, error)

func (*Store) DeviceCodeByHash

func (s *Store) DeviceCodeByHash(ctx context.Context, hash []byte) (*theauth.DeviceCode, error)

DeviceCodeByHash returns the request with the given device code hash, else ErrNotFound.

func (*Store) DeviceCodeByUserCode

func (s *Store) DeviceCodeByUserCode(ctx context.Context, userCode string) (*theauth.DeviceCode, error)

DeviceCodeByUserCode returns the request with the given user code, else ErrNotFound.

func (*Store) DeviceJoinTokenByHash added in v2.10.0

func (s *Store) DeviceJoinTokenByHash(ctx context.Context, hash []byte) (*theauth.DeviceJoinToken, error)

func (*Store) DeviceJoinTokenByID added in v2.10.0

func (s *Store) DeviceJoinTokenByID(ctx context.Context, id theauth.ULID) (*theauth.DeviceJoinToken, error)

func (*Store) GrantUserRole

func (s *Store) GrantUserRole(ctx context.Context, ur theauth.UserRole) error

func (*Store) GroupByExternalIDInOrg

func (s *Store) GroupByExternalIDInOrg(ctx context.Context, orgID theauth.ULID, externalID string) (*theauth.Group, error)

func (*Store) GroupByID

func (s *Store) GroupByID(ctx context.Context, id theauth.ULID) (*theauth.Group, error)

func (*Store) GroupMembers

func (s *Store) GroupMembers(ctx context.Context, groupID theauth.ULID) ([]theauth.ULID, error)

func (*Store) InsertAPIToken

func (s *Store) InsertAPIToken(ctx context.Context, t theauth.APIToken) (theauth.APIToken, error)

InsertAPIToken stores a token and returns the stored row.

func (*Store) InsertAgent

func (s *Store) InsertAgent(ctx context.Context, a theauth.Agent) (theauth.Agent, error)

func (*Store) InsertAgentCredential

func (s *Store) InsertAgentCredential(ctx context.Context, c theauth.AgentCredential) error

func (*Store) InsertAuditEvents

func (s *Store) InsertAuditEvents(ctx context.Context, events []theauth.AuditEvent) error

InsertAuditEvents appends a batch in one transaction. Rows are linked into a per-organization hash chain (see VerifyAuditChain); concurrent writers are serialized per chain by a row lock on the chain head (see lockChainHeads).

func (*Store) InsertAuthorizationCode

func (s *Store) InsertAuthorizationCode(ctx context.Context, c theauth.AuthorizationCode) error

func (*Store) InsertBackchannelRequest

func (s *Store) InsertBackchannelRequest(ctx context.Context, req theauth.BackchannelRequest) error

func (*Store) InsertDelegationGrant

func (s *Store) InsertDelegationGrant(ctx context.Context, g theauth.DelegationGrant) (theauth.DelegationGrant, error)

func (*Store) InsertDevice added in v2.10.0

func (s *Store) InsertDevice(ctx context.Context, d theauth.Device) error

func (*Store) InsertDeviceAuthorization added in v2.8.0

func (s *Store) InsertDeviceAuthorization(ctx context.Context, d theauth.DeviceAuthorization) error

func (*Store) InsertDeviceCode

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

InsertDeviceCode stores a request, returning ErrDeviceUserCodeTaken on a user code collision.

func (*Store) InsertDeviceJoinToken added in v2.10.0

func (s *Store) InsertDeviceJoinToken(ctx context.Context, t theauth.DeviceJoinToken) error

func (*Store) InsertGroup

func (s *Store) InsertGroup(ctx context.Context, g theauth.Group) (theauth.Group, error)

func (*Store) InsertJWKSKey

func (s *Store) InsertJWKSKey(ctx context.Context, k theauth.JWKSKey) error

func (*Store) InsertOAuthClient

func (s *Store) InsertOAuthClient(ctx context.Context, c theauth.OAuthClient) (theauth.OAuthClient, error)

func (*Store) InsertOpaqueAccessToken added in v2.8.0

func (s *Store) InsertOpaqueAccessToken(ctx context.Context, t theauth.OpaqueAccessToken) error

func (*Store) InsertOrganization

func (s *Store) InsertOrganization(ctx context.Context, o theauth.Organization) (theauth.Organization, error)

func (*Store) InsertPermission

func (s *Store) InsertPermission(ctx context.Context, p theauth.Permission) (theauth.Permission, error)

func (*Store) InsertPushedRequest

func (s *Store) InsertPushedRequest(ctx context.Context, requestURI string, payload []byte, expiresAt time.Time) error

InsertPushedRequest stores a pushed authorization request. The requestURI is the full urn: string; payload is the JSON-encoded AuthorizeRequest; expiresAt is the absolute expiry timestamp.

func (*Store) InsertRecoveryCodes

func (s *Store) InsertRecoveryCodes(ctx context.Context, codes []theauth.RecoveryCode) error

func (*Store) InsertRefreshToken

func (s *Store) InsertRefreshToken(ctx context.Context, t theauth.RefreshToken) error

func (*Store) InsertRegistrationToken added in v2.8.0

func (s *Store) InsertRegistrationToken(ctx context.Context, t theauth.RegistrationToken) error

func (*Store) InsertRole

func (s *Store) InsertRole(ctx context.Context, r theauth.Role) (theauth.Role, error)

func (*Store) InsertSAMLConnection

func (s *Store) InsertSAMLConnection(ctx context.Context, c theauth.SAMLConnection) (theauth.SAMLConnection, error)

func (*Store) InsertSCIMToken

func (s *Store) InsertSCIMToken(ctx context.Context, t theauth.SCIMToken) (theauth.SCIMToken, error)

func (*Store) InsertTrustedDevice added in v2.10.0

func (s *Store) InsertTrustedDevice(ctx context.Context, d theauth.TrustedDevice) error

InsertTrustedDevice stores a remember-this-device record.

func (*Store) InsertWebAuthnCredential

func (s *Store) InsertWebAuthnCredential(ctx context.Context, c theauth.WebAuthnCredential) (theauth.WebAuthnCredential, error)

func (*Store) IsAccessTokenDenied added in v2.8.0

func (s *Store) IsAccessTokenDenied(ctx context.Context, jti string) (bool, error)

IsAccessTokenDenied reports whether jti is on the unexpired denylist.

func (*Store) JWKSKeyByKID

func (s *Store) JWKSKeyByKID(ctx context.Context, kid string) (*theauth.JWKSKey, error)

func (*Store) JWKSKeysAll

func (s *Store) JWKSKeysAll(ctx context.Context) ([]theauth.JWKSKey, error)

func (*Store) ListAPITokens

func (s *Store) ListAPITokens(ctx context.Context) ([]theauth.APIToken, error)

ListAPITokens returns every token, newest first.

func (*Store) ListDevices added in v2.10.0

func (s *Store) ListDevices(ctx context.Context, orgID *theauth.ULID) ([]theauth.Device, error)

func (*Store) ListGroupsByOrganization

func (s *Store) ListGroupsByOrganization(ctx context.Context, orgID theauth.ULID, offset, limit int, filter theauth.SCIMGroupFilter) ([]theauth.Group, int, error)

func (*Store) ListPendingDeviceCodes

func (s *Store) ListPendingDeviceCodes(ctx context.Context, f theauth.DevicePendingFilter) ([]theauth.DeviceCode, error)

ListPendingDeviceCodes returns pending, unexpired requests newest first without device code hashes.

func (*Store) ListPermissions

func (s *Store) ListPermissions(ctx context.Context) ([]theauth.Permission, error)

func (*Store) ListRegistrationTokens added in v2.8.0

func (s *Store) ListRegistrationTokens(ctx context.Context, orgID *theauth.ULID) ([]theauth.RegistrationToken, error)

func (*Store) ListTrustedDevices added in v2.10.0

func (s *Store) ListTrustedDevices(ctx context.Context, userID theauth.ULID) ([]theauth.TrustedDevice, error)

ListTrustedDevices returns the user's unrevoked, unexpired devices, newest first.

func (*Store) ListUserSessions

func (s *Store) ListUserSessions(ctx context.Context, userID theauth.ULID) ([]theauth.Session, error)

ListUserSessions returns the user's live sessions, newest first.

func (*Store) ListUsersByOrganization

func (s *Store) ListUsersByOrganization(ctx context.Context, orgID theauth.ULID, offset, limit int, filter theauth.SCIMUserFilter) ([]theauth.User, int, error)

func (*Store) MarkEmailVerified

func (s *Store) MarkEmailVerified(ctx context.Context, userID theauth.ULID) error

func (*Store) MoveOAuthAccount

func (s *Store) MoveOAuthAccount(ctx context.Context, provider, providerUserID string, newUserID theauth.ULID) error

MoveOAuthAccount reassigns the row keyed by (provider, providerUserID) to newUserID.

func (*Store) MovePasswordHash

func (s *Store) MovePasswordHash(ctx context.Context, primaryID, secondaryID theauth.ULID) error

MovePasswordHash copies the Argon2id hash from secondaryID to primaryID, overwriting any existing primary hash, then clears secondaryID's row. A no-op when secondaryID has no password row.

func (*Store) MoveTOTPSecret

func (s *Store) MoveTOTPSecret(ctx context.Context, primaryID, secondaryID theauth.ULID) error

MoveTOTPSecret reassigns the TOTP secret. A confirmed primary secret wins; the secondary row is dropped without overwriting it.

func (*Store) MoveWebAuthnCredentials

func (s *Store) MoveWebAuthnCredentials(ctx context.Context, primaryID, secondaryID theauth.ULID) error

MoveWebAuthnCredentials reassigns every WebAuthn credential row from secondaryID to primaryID.

func (*Store) OAuthAccountByProviderUserID

func (s *Store) OAuthAccountByProviderUserID(ctx context.Context, provider, providerUserID string) (*theauth.OAuthAccount, error)

func (*Store) OAuthAccountsByUserID

func (s *Store) OAuthAccountsByUserID(ctx context.Context, userID theauth.ULID) ([]theauth.OAuthAccount, error)

OAuthAccountsByUserID returns every oauth_accounts row linked to userID.

func (*Store) OAuthClientByClientID

func (s *Store) OAuthClientByClientID(ctx context.Context, clientID string) (*theauth.OAuthClient, error)

func (*Store) OpaqueAccessTokenByHash added in v2.8.0

func (s *Store) OpaqueAccessTokenByHash(ctx context.Context, hash []byte) (*theauth.OpaqueAccessToken, error)

func (*Store) OpenToolApproval added in v2.11.0

func (s *Store) OpenToolApproval(ctx context.Context, a theauth.ToolApproval) error

func (*Store) OrganizationByID

func (s *Store) OrganizationByID(ctx context.Context, id theauth.ULID) (*theauth.Organization, error)

func (*Store) OrganizationBySlug

func (s *Store) OrganizationBySlug(ctx context.Context, slug string) (*theauth.Organization, error)

func (*Store) OrganizationMemberRole

func (s *Store) OrganizationMemberRole(ctx context.Context, orgID, userID theauth.ULID) (string, error)

func (*Store) OrganizationMembersByOrg

func (s *Store) OrganizationMembersByOrg(ctx context.Context, orgID theauth.ULID) ([]theauth.OrganizationMember, error)

func (*Store) OrganizationsByUser

func (s *Store) OrganizationsByUser(ctx context.Context, userID theauth.ULID) ([]theauth.Organization, error)

func (*Store) PermissionByName

func (s *Store) PermissionByName(ctx context.Context, name string) (*theauth.Permission, error)

func (*Store) PermissionsByRole

func (s *Store) PermissionsByRole(ctx context.Context, roleID theauth.ULID) ([]string, error)

func (*Store) PermissionsForUser

func (s *Store) PermissionsForUser(ctx context.Context, userID theauth.ULID, orgID *theauth.ULID) ([]string, error)

func (*Store) QueryAuditEvents

func (s *Store) QueryAuditEvents(ctx context.Context, q theauth.AuditQuery) ([]theauth.AuditEvent, string, error)

QueryAuditEvents is hand-built because the filter combinations vary too much for a fixed sqlc query. Rows are ordered by (recorded_at_us, id) in q.Order (default desc) and paged with a "c1." keyset cursor. A cursor from before write order existed keeps the old (created_at DESC, id DESC) keyset, for descending reads only.

func (*Store) RecordDeviceAuthorizationPoll added in v2.8.0

func (s *Store) RecordDeviceAuthorizationPoll(ctx context.Context, id theauth.ULID, polledAt time.Time, intervalSeconds int) error

func (*Store) RecordDevicePoll

func (s *Store) RecordDevicePoll(ctx context.Context, hash []byte, at time.Time, intervalSeconds int) error

RecordDevicePoll stores the last poll time and the current polling interval.

func (*Store) RedeemBackchannelRequest added in v2.11.0

func (s *Store) RedeemBackchannelRequest(ctx context.Context, authReqID string, now time.Time) (theauth.BackchannelRequest, bool, error)

RedeemBackchannelRequest implements theauth.BackchannelRedeemer. One conditional UPDATE moves an approved, unexpired request to consumed and returns it, so exactly one concurrent caller wins. Any other state (pending, denied, expired, consumed, unknown) returns ok == false.

func (*Store) RedeemDeviceJoinToken added in v2.10.0

func (s *Store) RedeemDeviceJoinToken(ctx context.Context, id, deviceID theauth.ULID, now time.Time) (bool, error)

func (*Store) RedeemRegistrationToken added in v2.8.0

func (s *Store) RedeemRegistrationToken(ctx context.Context, id theauth.ULID, now time.Time) (bool, error)

func (*Store) RefreshTokenByHash

func (s *Store) RefreshTokenByHash(ctx context.Context, hash []byte) (*theauth.RefreshToken, error)

func (*Store) RefundDeviceJoinToken added in v2.10.0

func (s *Store) RefundDeviceJoinToken(ctx context.Context, id theauth.ULID) error

func (*Store) RefundRegistrationToken added in v2.8.0

func (s *Store) RefundRegistrationToken(ctx context.Context, id theauth.ULID) error

func (*Store) RegistrationTokenByHash added in v2.8.0

func (s *Store) RegistrationTokenByHash(ctx context.Context, hash []byte) (*theauth.RegistrationToken, error)

func (*Store) RegistrationTokenByID added in v2.8.0

func (s *Store) RegistrationTokenByID(ctx context.Context, id theauth.ULID) (*theauth.RegistrationToken, error)

func (*Store) RemoveGroupMembers

func (s *Store) RemoveGroupMembers(ctx context.Context, groupID theauth.ULID, userIDs []theauth.ULID) error

func (*Store) RenameWebAuthnCredential

func (s *Store) RenameWebAuthnCredential(ctx context.Context, id, userID theauth.ULID, name string) error

func (*Store) ReplaceRecoveryCodes

func (s *Store) ReplaceRecoveryCodes(ctx context.Context, userID theauth.ULID, codes []theauth.RecoveryCode) error

func (*Store) RevokeAPIToken

func (s *Store) RevokeAPIToken(ctx context.Context, id theauth.ULID, at time.Time) error

RevokeAPIToken revokes a token, keeping the first revocation time on repeat calls.

func (*Store) RevokeAPITokensByOwner

func (s *Store) RevokeAPITokensByOwner(ctx context.Context, ownerID theauth.ULID, at time.Time) (int, error)

RevokeAPITokensByOwner revokes the owner's live tokens and returns how many it changed.

func (*Store) RevokeAgentCredential

func (s *Store) RevokeAgentCredential(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) RevokeDelegationGrant

func (s *Store) RevokeDelegationGrant(ctx context.Context, id theauth.ULID, at time.Time, reason string) error

func (*Store) RevokeDevice added in v2.10.0

func (s *Store) RevokeDevice(ctx context.Context, id theauth.ULID, at time.Time) (bool, error)

func (*Store) RevokeDeviceJoinToken added in v2.10.0

func (s *Store) RevokeDeviceJoinToken(ctx context.Context, id theauth.ULID, at time.Time) (bool, error)

func (*Store) RevokeOpaqueAccessToken added in v2.8.0

func (s *Store) RevokeOpaqueAccessToken(ctx context.Context, hash []byte) error

func (*Store) RevokeOtherUserSessions

func (s *Store) RevokeOtherUserSessions(ctx context.Context, userID, keep theauth.ULID) (int, error)

RevokeOtherUserSessions revokes the user's live sessions except keep and returns the count.

func (*Store) RevokeRefreshToken

func (s *Store) RevokeRefreshToken(ctx context.Context, hash []byte, reason string) error

func (*Store) RevokeRefreshTokenFamily

func (s *Store) RevokeRefreshTokenFamily(ctx context.Context, familyID theauth.ULID, reason string) error

func (*Store) RevokeRefreshTokensByAuthCode added in v2.8.0

func (s *Store) RevokeRefreshTokensByAuthCode(ctx context.Context, codeHash, reason string) ([]string, error)

RevokeRefreshTokensByAuthCode revokes the rotation families of the code under codeHash and returns the revoked access-token jtis.

func (*Store) RevokeRegistrationToken added in v2.8.0

func (s *Store) RevokeRegistrationToken(ctx context.Context, id theauth.ULID, at time.Time) (bool, error)

func (*Store) RevokeSCIMTokenByID

func (s *Store) RevokeSCIMTokenByID(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) RevokeSession

func (s *Store) RevokeSession(ctx context.Context, id theauth.ULID) error

func (*Store) RevokeSessionsByCredential

func (s *Store) RevokeSessionsByCredential(ctx context.Context, credentialID string) (int, error)

RevokeSessionsByCredential revokes every live session bound to credentialID and returns the count.

func (*Store) RevokeTrustedDevice added in v2.10.0

func (s *Store) RevokeTrustedDevice(ctx context.Context, userID, id theauth.ULID) error

RevokeTrustedDevice revokes one live device owned by userID.

func (*Store) RevokeUserRole

func (s *Store) RevokeUserRole(ctx context.Context, userID, roleID theauth.ULID) error

func (*Store) RevokeUserSessions

func (s *Store) RevokeUserSessions(ctx context.Context, userID theauth.ULID) error

func (*Store) RevokeUserTrustedDevices added in v2.10.0

func (s *Store) RevokeUserTrustedDevices(ctx context.Context, userID theauth.ULID) (int, error)

RevokeUserTrustedDevices revokes every live device of userID and returns how many it revoked.

func (*Store) RoleByID

func (s *Store) RoleByID(ctx context.Context, id theauth.ULID) (*theauth.Role, error)

func (*Store) RoleByOrgAndName

func (s *Store) RoleByOrgAndName(ctx context.Context, orgID *theauth.ULID, name string) (*theauth.Role, error)

func (*Store) RolesByOrganization

func (s *Store) RolesByOrganization(ctx context.Context, orgID *theauth.ULID) ([]theauth.Role, error)

func (*Store) RolesForUser

func (s *Store) RolesForUser(ctx context.Context, userID theauth.ULID, orgID *theauth.ULID) ([]theauth.Role, error)

func (*Store) SAMLConnectionByID

func (s *Store) SAMLConnectionByID(ctx context.Context, id theauth.ULID) (*theauth.SAMLConnection, error)

func (*Store) SAMLConnectionsByOrg

func (s *Store) SAMLConnectionsByOrg(ctx context.Context, orgID theauth.ULID) ([]theauth.SAMLConnection, error)

func (*Store) SAMLIdentityByConnectionAndNameID

func (s *Store) SAMLIdentityByConnectionAndNameID(ctx context.Context, connectionID theauth.ULID, nameID string) (*theauth.SAMLIdentity, error)

func (*Store) SCIMTokenByHash

func (s *Store) SCIMTokenByHash(ctx context.Context, hash []byte) (*theauth.SCIMToken, error)

func (*Store) SCIMTokensByOrg

func (s *Store) SCIMTokensByOrg(ctx context.Context, orgID theauth.ULID) ([]theauth.SCIMToken, error)

func (*Store) SessionAndUserByTokenHash added in v2.8.0

func (s *Store) SessionAndUserByTokenHash(ctx context.Context, hash []byte) (*theauth.Session, *theauth.User, error)

SessionAndUserByTokenHash returns a session and its user in one round trip.

func (*Store) SessionByID

func (s *Store) SessionByID(ctx context.Context, id theauth.ULID) (*theauth.Session, error)

SessionByID returns the session with the given ID.

func (*Store) SessionByTokenHash

func (s *Store) SessionByTokenHash(ctx context.Context, hash []byte) (*theauth.Session, error)

SessionByTokenHash returns the session with the given token hash.

func (*Store) SetGroupMembers

func (s *Store) SetGroupMembers(ctx context.Context, groupID theauth.ULID, userIDs []theauth.ULID) error

func (*Store) SetMustChangePassword added in v2.10.0

func (s *Store) SetMustChangePassword(ctx context.Context, userID theauth.ULID, required bool) error

SetMustChangePassword sets or clears the forced password change flag.

func (*Store) SetRolePermissions

func (s *Store) SetRolePermissions(ctx context.Context, roleID theauth.ULID, permissionIDs []theauth.ULID) error

func (*Store) SetSessionActiveOrganization

func (s *Store) SetSessionActiveOrganization(ctx context.Context, sessionID theauth.ULID, orgID *theauth.ULID) error

func (*Store) SetSessionElevatedUntil

func (s *Store) SetSessionElevatedUntil(ctx context.Context, id theauth.ULID, until *time.Time) error

SetSessionElevatedUntil sets or clears the step-up window of one session.

func (*Store) SetUserDisabledAt added in v2.10.0

func (s *Store) SetUserDisabledAt(ctx context.Context, userID theauth.ULID, at *time.Time) error

SetUserDisabledAt disables the user at the given time, or re-enables it when at is nil.

func (*Store) SetUserPassword

func (s *Store) SetUserPassword(ctx context.Context, userID theauth.ULID, passwordHash string) error

func (*Store) SignalsStore added in v2.10.0

func (s *Store) SignalsStore() signals.Store

SignalsStore returns a signals.Store backed by this database. Pass it as SharedSignalsConfig.Store.

func (*Store) TOTPSecretByUserID

func (s *Store) TOTPSecretByUserID(ctx context.Context, userID theauth.ULID) (*theauth.TOTPSecret, error)

func (*Store) ThrottleStore

func (s *Store) ThrottleStore() *ThrottleStore

ThrottleStore returns a login throttle store backed by the same database.

func (*Store) TouchAPITokenLastUsed

func (s *Store) TouchAPITokenLastUsed(ctx context.Context, id theauth.ULID, at time.Time) error

TouchAPITokenLastUsed records the last use time of a token.

func (*Store) TouchBackchannelPoll

func (s *Store) TouchBackchannelPoll(ctx context.Context, authReqID string, now time.Time, newInterval int) (theauth.BackchannelRequest, error)

TouchBackchannelPoll satisfies CIBAStorage. Updates last_poll_at and poll_interval atomically and returns the updated row.

func (*Store) TouchDevice added in v2.10.0

func (s *Store) TouchDevice(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) TouchSAMLIdentityLastLogin

func (s *Store) TouchSAMLIdentityLastLogin(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) TouchSCIMTokenLastUsed

func (s *Store) TouchSCIMTokenLastUsed(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) TouchSession

func (s *Store) TouchSession(ctx context.Context, id theauth.ULID, at time.Time) error

TouchSession advances last-seen time and never moves it backwards.

func (*Store) TouchTrustedDevice added in v2.10.0

func (s *Store) TouchTrustedDevice(ctx context.Context, id theauth.ULID, at time.Time) error

TouchTrustedDevice sets LastUsedAt.

func (*Store) TrustedDeviceByTokenHash added in v2.10.0

func (s *Store) TrustedDeviceByTokenHash(ctx context.Context, userID theauth.ULID, hash []byte) (*theauth.TrustedDevice, error)

TrustedDeviceByTokenHash returns the record for userID and hash, revoked or expired rows included.

func (*Store) UpdateAgentCredentialLastUsed

func (s *Store) UpdateAgentCredentialLastUsed(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) UpdateAgentExpiry added in v2.12.0

func (s *Store) UpdateAgentExpiry(ctx context.Context, id theauth.ULID, expiresAt *time.Time) error

func (*Store) UpdateAgentLastActive

func (s *Store) UpdateAgentLastActive(ctx context.Context, id theauth.ULID, at time.Time) error

func (*Store) UpdateAgentStatus

func (s *Store) UpdateAgentStatus(ctx context.Context, id theauth.ULID, status string, _ time.Time) error

func (*Store) UpdateBackchannelRequest

func (s *Store) UpdateBackchannelRequest(ctx context.Context, req theauth.BackchannelRequest) error

UpdateBackchannelRequest satisfies CIBAStorage. Writes all mutable fields.

func (*Store) UpdateGroup

func (s *Store) UpdateGroup(ctx context.Context, g theauth.Group) error

func (*Store) UpdateJWKSKeyState

func (s *Store) UpdateJWKSKeyState(ctx context.Context, kid, state string, at time.Time) error

func (*Store) UpdateOAuthClient

func (s *Store) UpdateOAuthClient(ctx context.Context, c theauth.OAuthClient) (theauth.OAuthClient, error)

func (*Store) UpdateOrganization

func (s *Store) UpdateOrganization(ctx context.Context, o theauth.Organization) error

func (*Store) UpdateRoleRow

func (s *Store) UpdateRoleRow(ctx context.Context, r theauth.Role) (theauth.Role, error)

func (*Store) UpdateSAMLConnectionRow

func (s *Store) UpdateSAMLConnectionRow(ctx context.Context, c theauth.SAMLConnection) error

func (*Store) UpdateSessionAuthLevel

func (s *Store) UpdateSessionAuthLevel(ctx context.Context, id theauth.ULID, level string) error

func (*Store) UpdateUserSCIM

func (s *Store) UpdateUserSCIM(ctx context.Context, u theauth.User) error

func (*Store) UpdateWebAuthnBackupFlags

func (s *Store) UpdateWebAuthnBackupFlags(ctx context.Context, credentialID []byte, backupEligible, backupState bool) error

UpdateWebAuthnBackupFlags reconciles BE/BS flags for legacy synced passkeys (trust-on-first-use). A missing credential returns nil: the caller treats this as best-effort.

func (*Store) UpdateWebAuthnSignCount

func (s *Store) UpdateWebAuthnSignCount(ctx context.Context, credentialID []byte, newCount uint32, usedAt time.Time) error

UpdateWebAuthnSignCount requires a strictly greater count. On a miss it runs a follow-up SELECT to tell a missing credential from a replay, keeping the happy path to one round-trip.

func (*Store) UpsertOAuthAccount

func (s *Store) UpsertOAuthAccount(ctx context.Context, a theauth.OAuthAccount) (theauth.OAuthAccount, error)

func (*Store) UpsertOrganizationMember

func (s *Store) UpsertOrganizationMember(ctx context.Context, m theauth.OrganizationMember) error

func (*Store) UpsertPendingTOTPSecret

func (s *Store) UpsertPendingTOTPSecret(ctx context.Context, sec theauth.TOTPSecret) error

func (*Store) UpsertSAMLIdentity

func (s *Store) UpsertSAMLIdentity(ctx context.Context, i theauth.SAMLIdentity) (theauth.SAMLIdentity, error)

func (*Store) UserByEmail

func (s *Store) UserByEmail(ctx context.Context, email string) (*theauth.User, error)

func (*Store) UserByEmailWithPassword

func (s *Store) UserByEmailWithPassword(ctx context.Context, email string) (*theauth.User, string, error)

func (*Store) UserByExternalIDInOrg

func (s *Store) UserByExternalIDInOrg(ctx context.Context, orgID theauth.ULID, externalID string) (*theauth.User, error)

func (*Store) UserByID

func (s *Store) UserByID(ctx context.Context, id theauth.ULID) (*theauth.User, error)

func (*Store) UserPasswordHashByID

func (s *Store) UserPasswordHashByID(ctx context.Context, userID theauth.ULID) (string, error)

UserPasswordHashByID returns the stored Argon2id PHC string for the user, or "" when the user has no password (or does not exist). Hashes live in users.password_hash.

func (*Store) VerifyAuditChain added in v2.11.0

func (s *Store) VerifyAuditChain(ctx context.Context) (theauth.AuditChainResult, error)

VerifyAuditChain walks every chain in seq order and reports the first mismatched row. Pre-0028 rows count as Unchained; erased rows verify via their pseudonyms and count as Erased.

func (*Store) WebAuthnCredentialByCredentialID

func (s *Store) WebAuthnCredentialByCredentialID(ctx context.Context, credentialID []byte) (*theauth.WebAuthnCredential, error)

func (*Store) WebAuthnCredentialsByUserID

func (s *Store) WebAuthnCredentialsByUserID(ctx context.Context, userID theauth.ULID) ([]theauth.WebAuthnCredential, error)

type ThrottleStore

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

ThrottleStore is a Postgres-backed theauth.LoginThrottleStore shared by every process using the database.

func (*ThrottleStore) CompareAndSwap

func (t *ThrottleStore) CompareAndSwap(ctx context.Context, key string, prev theauth.LoginThrottleEntry, prevExists bool, next theauth.LoginThrottleEntry) (bool, error)

CompareAndSwap writes next only when the stored entry still equals prev, or is absent when prevExists is false.

func (*ThrottleStore) Delete

func (t *ThrottleStore) Delete(ctx context.Context, key string) error

Delete removes key.

func (*ThrottleStore) DeleteLoginEntries added in v2.7.0

func (t *ThrottleStore) DeleteLoginEntries(ctx context.Context, ident string) error

DeleteLoginEntries removes every login backoff entry for ident across all client IPs.

func (*ThrottleStore) Get

Get returns the entry for key.

func (*ThrottleStore) Set

Set stores e under key unconditionally.

func (*ThrottleStore) SweepExpired

func (t *ThrottleStore) SweepExpired(ctx context.Context, now time.Time) (int, error)

SweepExpired deletes entries whose ExpiresAt is at or before now and returns how many it removed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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