access

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package access is the PostgreSQL repository of the access context: members, API tokens and sessions.

Index

Constants

View Source
const AuthSettingsChannel = "auth_settings"

AuthSettingsChannel is the notification channel a change of the auth settings is announced on, so every replica drops what it cached.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthSettingsCache

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

AuthSettingsCache holds a replica's copy of the auth settings, which every request judging a session reads. It trusts its copy only while its listener on AuthSettingsChannel is up: a change announced while it is down may be missed, so until it is listening again every read goes to PostgreSQL.

func NewAuthSettingsCache

func NewAuthSettingsCache(pool *pgxpool.Pool) *AuthSettingsCache

NewAuthSettingsCache returns a cache of the settings on pool, which reads through until its listener reports itself up.

func (*AuthSettingsCache) AuthSettings

func (c *AuthSettingsCache) AuthSettings(ctx context.Context) (access.AuthSettings, error)

AuthSettings returns the auth settings, from the copy while it can be trusted.

func (*AuthSettingsCache) Forget

func (c *AuthSettingsCache) Forget()

Forget drops the copy, as a change made by this replica does once it is committed, so the replica's next read sees it without waiting for the notification.

func (*AuthSettingsCache) Listening

func (c *AuthSettingsCache) Listening(up bool)

Listening is told when the listener starts or stops listening.

func (*AuthSettingsCache) Received

func (c *AuthSettingsCache) Received(string)

Received is told of each change announced.

type Store

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

Store is the repository of members and API tokens.

func NewStore

func NewStore(pool *pgxpool.Pool) *Store

NewStore returns the repository on pool.

func (*Store) AnyInstanceAdmin

func (s *Store) AnyInstanceAdmin(ctx context.Context) (bool, error)

AnyInstanceAdmin reports whether the instance has an instance admin, whatever their status.

func (*Store) AuthSettings

func (s *Store) AuthSettings(ctx context.Context) (access.AuthSettings, error)

AuthSettings returns the instance's auth settings as stored.

func (*Store) ClearAttempt

func (s *Store) ClearAttempt(ctx context.Context, r access.Reservation) error

ClearAttempt takes back the failure r reserved once its password proved right: the source's window, when it is still the one counted in, holds one less, and the consecutive failures r was counted under end. A right password from a known source leaves the email's own failures, which every other source shares, as they are: the member signing in from home does not reset a guesser's count elsewhere.

func (*Store) ConsumeInvitations

func (s *Store) ConsumeInvitations(ctx context.Context, member access.MemberID, at time.Time) (int, error)

ConsumeInvitations ends every open invitation of member at at, and returns how many it ended.

func (*Store) CreateGrant

func (s *Store) CreateGrant(ctx context.Context, g access.Grant) error

CreateGrant stores g; the same member, role and source in the project twice is refused already_granted.

func (*Store) CreateInvitation

func (s *Store) CreateInvitation(ctx context.Context, inv access.Invitation) error

CreateInvitation stores inv.

func (*Store) CreateMember

func (s *Store) CreateMember(ctx context.Context, m access.Member, passwordHash string) error

CreateMember stores m with its password hash ("" for none).

func (*Store) CreateSession

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

CreateSession stores sess.

func (*Store) CreateToken

func (s *Store) CreateToken(ctx context.Context, t access.APIToken) error

CreateToken stores t.

func (*Store) DeleteGrant

func (s *Store) DeleteGrant(ctx context.Context, project workspace.ProjectID, grant access.GrantID) (access.Grant, error)

DeleteGrant removes the grant of project with the identifier and returns it; ErrNotFound when the project holds none.

func (*Store) GrantedProjects

func (s *Store) GrantedProjects(ctx context.Context, member access.MemberID) ([]workspace.ProjectID, error)

GrantedProjects returns the projects member holds a grant in.

func (*Store) InvitationByHash

func (s *Store) InvitationByHash(ctx context.Context, hash access.InvitationHash) (access.Invitation, access.Member, error)

InvitationByHash returns the invitation whose secret hashes to hash, with its member; ErrNotFound when there is none.

func (*Store) LockAuthSettings

func (s *Store) LockAuthSettings(ctx context.Context) (access.AuthSettings, error)

LockAuthSettings returns the auth settings and locks them until the transaction ends, so two changes made at once each see the other's outcome.

func (*Store) LockBootstrap

func (s *Store) LockBootstrap(ctx context.Context) error

LockBootstrap serializes bootstraps until the transaction ends.

func (*Store) LockGrant

func (s *Store) LockGrant(ctx context.Context, project workspace.ProjectID, grant access.GrantID) (access.Grant, error)

LockGrant returns the grant of project with the identifier, locked until the transaction ends; ErrNotFound when the project holds none.

func (*Store) LockInstanceAdmins

func (s *Store) LockInstanceAdmins(ctx context.Context) error

LockInstanceAdmins takes, until the transaction ends, the lock every change that could leave the instance without an active instance admin holds. The key names the schema too, so instances sharing a database in separate schemas do not wait on each other.

func (*Store) LockInvitationByHash

func (s *Store) LockInvitationByHash(ctx context.Context, hash access.InvitationHash) (access.Invitation, access.Member, error)

LockInvitationByHash returns the invitation whose secret hashes to hash, locked until the transaction ends, with its member; ErrNotFound when there is none. Its member is to be locked first, as every change to a member's invitations locks them.

func (*Store) LockMember

func (s *Store) LockMember(ctx context.Context, member access.MemberID) (access.Member, bool, error)

LockMember returns the member, locked until the transaction ends, and whether they hold a password; ErrNotFound when none has the identifier.

func (*Store) LockMemberByEmail

func (s *Store) LockMemberByEmail(ctx context.Context, email string) (access.Member, error)

LockMemberByEmail returns the member whose email is email whatever its case, locked until the transaction ends; ErrNotFound when there is none.

func (*Store) LockMemberPassword added in v0.2.0

func (s *Store) LockMemberPassword(ctx context.Context, member access.MemberID) (access.Member, string, error)

LockMemberPassword returns the member with the identifier and their password hash ("" for none), locked until the transaction ends; ErrNotFound when there is none.

func (*Store) MemberByEmail

func (s *Store) MemberByEmail(ctx context.Context, email string) (access.Member, string, error)

MemberByEmail returns the member whose email is email whatever its case, with their password hash ("" for none); ErrNotFound when there is none.

func (*Store) MemberByID

func (s *Store) MemberByID(ctx context.Context, member access.MemberID) (access.Member, error)

MemberByID returns the member with the identifier; ErrNotFound when there is none.

func (*Store) MemberGrants

func (s *Store) MemberGrants(ctx context.Context, member access.MemberID, project workspace.ProjectID) ([]access.Grant, error)

MemberGrants returns the grants member holds in project, oldest first.

func (*Store) MemberSessions

func (s *Store) MemberSessions(ctx context.Context, member access.MemberID) ([]access.Session, error)

MemberSessions returns the sessions of member not yet revoked, the newest first.

func (*Store) MemberView

func (s *Store) MemberView(ctx context.Context, member access.MemberID) (appaccess.MemberView, error)

MemberView returns one member as an instance admin sees them; ErrNotFound when none has the identifier.

func (*Store) Members

Members returns the members f selects, ordered by email whatever its case.

func (*Store) OtherActiveInstanceAdmins

func (s *Store) OtherActiveInstanceAdmins(ctx context.Context, member access.MemberID) (int, error)

OtherActiveInstanceAdmins counts the active instance admins other than member.

func (*Store) PersonalTokens

func (s *Store) PersonalTokens(ctx context.Context, member access.MemberID) ([]access.APIToken, error)

PersonalTokens returns every personal access token of member, the newest first.

func (*Store) ProjectGrants

func (s *Store) ProjectGrants(ctx context.Context, project workspace.ProjectID) ([]access.Grant, error)

ProjectGrants returns every grant of project, oldest first.

func (*Store) ProjectMemberCounts

func (s *Store) ProjectMemberCounts(ctx context.Context, projects []workspace.ProjectID) (map[workspace.ProjectID]int, error)

ProjectMemberCounts returns how many active members hold a grant in each of projects, in one query.

func (*Store) ProjectMembers

func (s *Store) ProjectMembers(ctx context.Context, project workspace.ProjectID) ([]appaccess.ProjectMember, error)

ProjectMembers returns the members holding grants in project, ordered by email, each with their grants oldest first, in one query.

func (*Store) ProjectTokens

func (s *Store) ProjectTokens(ctx context.Context, project workspace.ProjectID) ([]access.APIToken, error)

ProjectTokens returns every project token of project, the newest first.

func (*Store) PurgeEmailFailures

func (s *Store) PurgeEmailFailures(ctx context.Context, cut access.FailureCutoffs, limit int) (int, error)

PurgeEmailFailures deletes at most limit emails' failures that no lock holds and whose last failure is at or before the cutoff.

func (*Store) PurgeInvitations

func (s *Store) PurgeInvitations(ctx context.Context, cut access.InvitationCutoffs, limit int) (int, error)

PurgeInvitations deletes at most limit invitations consumed or expired at or before the cutoff, except each member's latest, which stays as the record of their last link.

func (*Store) PurgeKnownSourceFailures added in v0.2.0

func (s *Store) PurgeKnownSourceFailures(ctx context.Context, cut access.FailureCutoffs, limit int) (int, error)

PurgeKnownSourceFailures deletes at most limit failures of emails from known sources that no lock holds and whose last failure is at or before the cutoff, as PurgeEmailFailures does an email's own.

func (*Store) PurgeKnownSources added in v0.2.0

func (s *Store) PurgeKnownSources(ctx context.Context, cut access.FailureCutoffs, limit int) (int, error)

PurgeKnownSources deletes at most limit sources their members last signed in from at or before the cutoff: they are no longer known.

func (*Store) PurgeSessions

func (s *Store) PurgeSessions(ctx context.Context, cut access.SessionCutoffs, limit int) (int, error)

PurgeSessions deletes at most limit sessions the cutoffs end.

func (*Store) PurgeSourceFailures

func (s *Store) PurgeSourceFailures(ctx context.Context, cut access.FailureCutoffs, limit int) (int, error)

PurgeSourceFailures deletes at most limit sources' failures whose window opened at or before the cutoff.

func (*Store) RecordKnownSource added in v0.2.0

func (s *Store) RecordKnownSource(ctx context.Context, member access.MemberID, source netip.Prefix, at time.Time) error

RecordKnownSource records that member signed in from source at, which keeps the source known to them for the age the domain gives it.

func (*Store) RecordSignIn

func (s *Store) RecordSignIn(ctx context.Context, member access.MemberID, at time.Time) error

RecordSignIn records that member signed in at.

func (*Store) RecordTokenUse

func (s *Store) RecordTokenUse(ctx context.Context, token access.APITokenID, at time.Time, ip netip.Addr) error

RecordTokenUse records the use of token at at from ip unless a use less than the domain's interval before at is recorded already, so replicas recording the same token's use coalesce on one write per interval.

func (*Store) ReleaseAttempt

func (s *Store) ReleaseAttempt(ctx context.Context, r access.Reservation) error

ReleaseAttempt takes back the failure r reserved when its attempt guessed nothing: its password could not be verified, or proved right but the sign-in failed after, or it was refused before any was checked. It runs in the unit of work ctx carries, so the source and the failures r was counted under are taken back together. When no failure was counted on them since r, they are as they were before r: a lock r started is lifted, and a lock before it had ended, since r was counted. Failures fewer than r counted were cleared since, and r's failure went with them, so nothing is taken back twice.

func (*Store) ReserveAttempt

func (s *Store) ReserveAttempt(ctx context.Context, a access.Attempt) (access.Reservation, error)

ReserveAttempt counts a as a failure before its password is verified, and returns what it found: failures locked by earlier ones are not counted again, and report when they unlock. It runs in the unit of work ctx carries.

A statement sees only the rows committed before it began, so failures first counted by a statement it waited on, on the source's row, say, are invisible to its prior: the count finds them locked and skips them, but the statement cannot say until when. The skipped row is locked all the same until the unit of work ends, so a second statement, which sees it, reads the lock it was judged against.

func (*Store) ReserveSourceAttempt

func (s *Store) ReserveSourceAttempt(ctx context.Context, a access.Attempt) (access.Reservation, error)

ReserveSourceAttempt counts a as a failure of its source alone, before what it presents is checked: an attempt naming no email, such as accepting an invitation, is throttled with the source's other attempts but locks no email. The window is counted as reserveAttempt counts it.

func (*Store) RevokeMemberSessions

func (s *Store) RevokeMemberSessions(ctx context.Context, member access.MemberID, at time.Time) (int, error)

RevokeMemberSessions ends every session of member not ended yet at at, and returns how many it ended.

func (*Store) RevokePersonalToken

func (s *Store) RevokePersonalToken(ctx context.Context, member access.MemberID, token access.APITokenID, at time.Time,
) (access.APIToken, error)

RevokePersonalToken revokes member's unrevoked personal access token at at and returns it.

func (*Store) RevokePersonalTokens

func (s *Store) RevokePersonalTokens(ctx context.Context, member access.MemberID, at time.Time) (int, error)

RevokePersonalTokens revokes every personal access token of member not revoked yet at at, and returns how many it revoked.

func (*Store) RevokeProjectToken

func (s *Store) RevokeProjectToken(ctx context.Context, project workspace.ProjectID, token access.APITokenID, at time.Time,
) (access.APIToken, error)

RevokeProjectToken revokes project's unrevoked project token at at and returns it.

func (*Store) RevokeSession

func (s *Store) RevokeSession(ctx context.Context, session access.SessionID, at time.Time) error

RevokeSession ends session at, unless it has ended already.

func (*Store) RevokeSessions

func (s *Store) RevokeSessions(ctx context.Context, sessions []access.SessionID, at time.Time) ([]access.SessionID, error)

RevokeSessions ends each of sessions not ended yet at at, and returns those it ended.

func (*Store) SaveAuthSettings

func (s *Store) SaveAuthSettings(ctx context.Context, settings access.AuthSettings) error

SaveAuthSettings stores settings and announces the change on AuthSettingsChannel, which PostgreSQL delivers once the transaction commits and never if it rolls back.

func (*Store) SaveMember

func (s *Store) SaveMember(ctx context.Context, m access.Member, at time.Time) error

SaveMember stores m's email, display name, status, instance admin switch and locale, NULL when the member follows the browser.

func (*Store) SessionByHash

func (s *Store) SessionByHash(ctx context.Context, hash access.SessionHash) (access.Session, access.Member, error)

SessionByHash returns the session stored under hash with its member; ErrNotFound when there is none.

func (*Store) SetInstanceAdmin

func (s *Store) SetInstanceAdmin(ctx context.Context, member access.MemberID, instanceAdmin bool, at time.Time) error

SetInstanceAdmin makes member an instance admin or not.

func (*Store) SetPasswordHash

func (s *Store) SetPasswordHash(ctx context.Context, member access.MemberID, hash string, at time.Time) error

SetPasswordHash replaces the password hash of member.

func (*Store) TokenByHash

func (s *Store) TokenByHash(ctx context.Context, hash access.TokenHash) (access.APIToken, *access.Member, error)

TokenByHash returns the token whose value hashes to hash and, for a personal access token, its member.

func (*Store) TouchSession

func (s *Store) TouchSession(ctx context.Context, session access.SessionID, at time.Time) error

TouchSession records a request of session at, unless a later one is recorded already.

func (*Store) TryLockAuthRetention

func (s *Store) TryLockAuthRetention(ctx context.Context) (bool, error)

TryLockAuthRetention takes the purge lock until the transaction ends, without waiting; false when another replica holds it. The key names the schema too, so instances sharing a database in separate schemas purge independently.

Jump to

Keyboard shortcuts

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