Documentation
¶
Overview ¶
Package signinflow establishes the session of an Auth sign-in over a storage driver: it resolves the identity's user, decides the sign-in and inserts the session, once for every driver, generic over the driver's transaction type. The drivers keep the SQL: each supplies the primitives, every one taking the transaction the flow opened.
Index ¶
- Constants
- func AcceptInvitation[Tx any](ctx context.Context, d Driver[Tx], userID impulse.UUID, username, email string, ...) error
- func InvitedAddressMessage(inv *sessioninfo.Invitation) string
- func LinkConfirmed[Tx any](ctx context.Context, d Driver[Tx], userID impulse.UUID, username string, ...) error
- func Refusal(code, fallback sessioninfo.LoginRefusalCode, cause error, message string) error
- func SignIn[Tx any](ctx context.Context, d Driver[Tx], id impulse.UUID, ...) error
- func SignUp[Tx any](ctx context.Context, d Driver[Tx], method, audience string, ...) (impulse.UUID, error)
- func SignUpClosed() error
- type DirectorySeater
- type Driver
- type InvitationAcceptor
- type OnlyInTenant
- type Rules
- type SeededAccount
- type SignUpSeater
- type TrustedDeviceCheck
Constants ¶
const NotInvitedMessage = "this sign-in is by invitation: ask an administrator to invite you"
NotInvitedMessage is the sentence a person reads when a sign-in method that is invite only refuses them: they hold no account, and carried no invitation.
const OrganizationAccountMessage = "This invitation is accepted with your organization's account, not with a password or a personal account. " +
"Sign in with your organization's account (Google Workspace or Microsoft Entra)."
OrganizationAccountMessage is the sentence shown for an acceptance refused because the invitation is accepted through an organization's directory alone: what the person does instead.
const OrganizationHasTenantMessage = "Your organization is already set up in this application. Ask its administrator to invite you."
OrganizationHasTenantMessage is the sentence shown to a signed-in person whose acceptance by hand was refused because the organization they signed in through already has a tenant: it says what to do and nothing of that tenant.
Variables ¶
This section is empty.
Functions ¶
func AcceptInvitation ¶
func AcceptInvitation[Tx any](ctx context.Context, d Driver[Tx], userID impulse.UUID, username, email string, inv *sessioninfo.Invitation, accept InvitationAcceptor[Tx]) error
AcceptInvitation accepts inv for userID, whose address is email ("" for none), in one transaction: the invitation is consumed, the user's address is set from it when the user holds no verified address and no other user holds it, and accept, nil for none, gives what the invitation names, handed the transaction; what accept asks to have run after the commit runs once it has landed. inv.Accepted is set to the outcome. The user accepts only an invitation to their own address: one mailed to another address is refused (a *sessioninfo.LoginRefusal, invitation_address) with nothing written, the invitation left standing (addressRefusal); a user with no address accepts by the link and takes the address. An invitation that is no longer live (consumed, expired or revoked) is NotFound with signin.ErrInvitationExpired as its cause, and nothing is written; a grant that fails is the acceptor's error, whatever it is, with nothing written either: the invitation stands.
The caller notes on inv how the person's session was established (inv.SignInMethod, inv.SignInConnection: the session's first auth event), which accept reads, and the acceptance is held to it: an invitation that makes a tenant entered through its directory only is refused (organization_account_required) for a session that came through no organization's directory, with nothing written (organizationAccountRefusal).
func InvitedAddressMessage ¶
func InvitedAddressMessage(inv *sessioninfo.Invitation) string
InvitedAddressMessage is the sentence shown for an acceptance refused for another address: the invited address, and that the person signs in with that account.
func LinkConfirmed ¶ added in v0.5.0
func LinkConfirmed[Tx any]( ctx context.Context, d Driver[Tx], userID impulse.UUID, username string, identity *sessioninfo.Identity, tenant string, directories signin.DirectoryReader[Tx], seat DirectorySeater[Tx], proven dbtype.PasswordProof, ) error
LinkConfirmed links identity to userID once the person has confirmed the link with the user's password, in one transaction, and gives the person the default seat of the tenant whose directory the identity came through, when directories reads one (nil for none): the confirmed link is the person's first sign-in through the directory, as a link the resolution makes is, so the seat is given with it or the link is not made (seatDeclared). What seat asks to have run after the commit runs once it has landed, before the caller establishes the session. An identity that was linked meanwhile is a Conflict with nothing written, which the caller reads the link by; a seater's error is the seater's, whatever it is, with nothing written either.
proven is the proof of the password the person confirmed with: the link is made only while the user still holds that password, read in the transaction that makes it, so a password changed, reset or removed since the confirmation links nothing (dbtype.ErrPasswordChanged). The nil proof holds the link to nothing.
func Refusal ¶
func Refusal(code, fallback sessioninfo.LoginRefusalCode, cause error, message string) error
Refusal returns a refused sign-in's error. It carries code (or fallback when code is empty) for the login page, cause for errors.Is, and an Unauthorized client message.
func SignIn ¶
func SignIn[Tx any]( ctx context.Context, d Driver[Tx], id impulse.UUID, insertSession *dbtype.InsertSession, req *sessioninfo.NewSessionRequest, rules Rules[Tx], ) error
SignIn establishes session id for a request that carries a verified identity, in two transactions (see the README's "Storage and schema"). The first resolves an external identity's user: a linked identity is its user's, and an unlinked one goes to the account rules: the registration's custom rule when it has one, and the standard rule when it has none or the custom rule answers signin.Standard. The standard rule reads what an administrator chose for the method, in that transaction (standardRule). The second reads the user, asks policy and inserts the session. Each transaction commits unless a hook or the driver fails, so a refusal or a pending outcome keeps the hooks' own writes and writes no session. The resolution commits first because Spanner hides buffered writes from reads in the same transaction: the policy and the custom session data resolver must see a provisioned user and the rows OnProvisioned wrote.
A password identity names its user in req.UserID and skips the resolution. Once the user is known, req.UserID, req.Username and req.User are set to it, whatever the outcome. A refusal is a *sessioninfo.LoginRefusal and a sign-in that must wait a *signin.PendingSignInError.
A sign-in that carries an invitation (req.Invitation) accepts it once the user is known and before the policy decides, so the policy sees what the acceptance granted. It is accepted only as the invited address: a sign-in whose address is not the one the invitation was mailed to, the directory's for an external identity and the account's own for a password one, is refused before anything is read or written (invitation_address), the invitation left standing for the invited account. An external identity that asserts no address cannot match; a password account that holds no address accepts by the link, since the link is what the person proved, and takes the address. The invitation is told how the person signed in (Invitation.SignInMethod, SignInConnection: the identity's), which the acceptor reads, and an invitation that makes a tenant entered through its directory only (Invitation.NewTenant) is accepted through an organization's directory alone: a sign-in by password or with a personal account is refused before anything is read or written (organization_account_required), the invitation left standing (organizationAccountRefusal). Accepting it consumes the invitation and sets the user's address from it when the user holds no verified address, in the resolution transaction for an external identity (so a user the invitation provisioned and the consumption commit together) and in a transaction of its own for a password identity; rules.Accept gives what the invitation names inside that same transaction, which it is handed, so the invitation is spent and its roles given together or neither is. The invitation is consumed only once the resolution has admitted the sign-in: the user found, linked or provisioned, and enabled. A sign-in the account rules refuse or hold for a confirmation, or a disabled user's, leaves the invitation standing, nothing granted, so the person may sign in another way; a sign-in the policy then denies leaves it accepted, and what the acceptance granted stands. An invitation that is no longer live is skipped and the sign-in goes on, unless the invitation alone admitted the person (a method that is invite only), which is then a refusal (invitation_expired) and provisions nothing; req.Invitation.Accepted reports whether it was accepted. An acceptor that fails ends the sign-in with its error and fails the transaction: the invitation stands, a user the resolution would have provisioned or linked is not, and Accepted is false.
A person's first sign-in through a directory some tenant declares is decided by the tenant's row when rules.Directories is set (standardRule): the default seat is given by rules.Seat inside the resolution's transaction, the one that links the identity, before the invitation's grant, whether the account is made in that transaction or stood before, so the link is made and the person seated together or neither is. A linked identity gives nothing more: a seat an administrator removed is not given back by signing in, and a link that is deleted and made again is a first sign-in again.
What the two hooks ask to have run once their transaction has committed (their committed functions: the word to a permission engine that its rows changed) runs right after the commit and before the policy decides, so the policy sees the memberships. Only the functions of the run of the transaction that committed are called, each once.
func SignUp ¶ added in v0.5.0
func SignUp[Tx any]( ctx context.Context, d Driver[Tx], method, audience string, user *dbtype.InsertSessionUser, linkID impulse.UUID, seat SignUpSeater[Tx], ) (impulse.UUID, error)
SignUp makes the account of a person who signs up with a password, in one transaction, and returns its id. method is the name the password method is registered under and audience whom its registration admits (signin.Audience.Key), and the transaction reads what an administrator chose for it: a method that is not open to sign-up for that audience refuses (SignUpClosed), so a sign-up link mailed while the method was open creates nothing once it is closed. The mail link linkID, which carried the person here, is consumed, the user row written, and the method's default role given by seat, handed the transaction (nil for none; a role with no hook fails the transaction, as seatSignedUp says): the link is spent, the account made and the role held together, or none of it. What seat asks to have run after the commit runs once it has landed, before the caller signs the person in.
A link that is no longer live is NotFound. An address another user holds, verified or not, and a username that is taken are a Conflict. Nothing is written by a refusal.
func SignUpClosed ¶ added in v0.5.0
func SignUpClosed() error
SignUpClosed is the answer to a sign-up through a method that is not open to it: the sentence the sign-up routes say when the method is invite only.
Types ¶
type DirectorySeater ¶
type DirectorySeater[Tx any] func(ctx context.Context, tx Tx, userID impulse.UUID, username string, seats []signin.DirectorySeat) (committed func(ctx context.Context), err error)
DirectorySeater is the application's half of a directory's default seat: it seats userID, named by id and by username, with seats, the default seat of the tenant whose directory the identity came through, inside the transaction that links the identity to the user, which it is handed: the link is made and the person seated together, or neither. The link is made at a person's first sign-in through the directory, whether the account is made in that transaction or stood before, so the seater is called once for a link; it gives a seat only where the person holds none in the tenant, which it alone can read. It is held to what InvitationAcceptor is: tx alone for reads and writes, a committed function (nil for none) the flow runs once the transaction has committed and before the sign-in policy decides, and an error that fails the transaction, so no account is left holding nothing and no link is left that gave nothing.
type Driver ¶
type Driver[Tx any] interface { // Transact runs fn in a read-write transaction and commits when fn returns nil. // Spanner retries an aborted transaction, so fn may run more than once and starts // from its inputs on every run. Transact(ctx context.Context, fn func(ctx context.Context, tx Tx) error) error // IdentityIn returns the link for (method, connection, subject), or nil when the // identity is not linked. IdentityIn(ctx context.Context, tx Tx, method sessioninfo.AuthMethod, connection, subject string) (*dbtype.SessionIdentity, error) // TouchIdentityIn moves a link's LastUsedAt to the database's time at commit. TouchIdentityIn(ctx context.Context, tx Tx, linkID impulse.UUID) error // LinkIdentityIn writes a new link of identity to userID, created and last used at // the database's time at commit. A link of the same identity made meanwhile ends the // transaction with an IdentityConflict. LinkIdentityIn(ctx context.Context, tx Tx, userID impulse.UUID, identity *sessioninfo.Identity, tenant string) error // IdentitiesByUserIn lists userID's links inside tx, oldest first. IdentitiesByUserIn(ctx context.Context, tx Tx, userID impulse.UUID) ([]*dbtype.SessionIdentity, error) // UserIn reads the part of userID's user the flow decides on. A missing user is // NotFound. UserIn(ctx context.Context, tx Tx, userID impulse.UUID) (*dbtype.User, error) // LockUserIn reads the user as UserIn does and holds the user against another link // decision until tx ends, so the check of the user's links and the new link are // not raced; a session insert for the user, and an account rule's own reads of the // user, are not held. A missing user is NotFound. LockUserIn(ctx context.Context, tx Tx, userID impulse.UUID) (*dbtype.User, error) // UserByUsernameIn reads the user whose username is username, matched without // regard to case as the users table's normalized username is, or nil when there is // none. UserByUsernameIn(ctx context.Context, tx Tx, username string) (*dbtype.User, error) // UserByVerifiedEmailIn reads the user who holds email as a verified address, // matched without regard to case as the users table's normalized address is, or // nil when there is none; an address a user holds unverified matches no one. UserByVerifiedEmailIn(ctx context.Context, tx Tx, email string) (*dbtype.User, error) // InsertUserIn writes a new user row and returns its ID. A taken username ends the // transaction with a UsernameConflict. InsertUserIn(ctx context.Context, tx Tx, user *dbtype.InsertSessionUser) (impulse.UUID, error) // InsertSessionIn writes session row id for req with its auth events and its custom // session data: req.CustomData, or what the configured resolver returns. InsertSessionIn(ctx context.Context, tx Tx, id impulse.UUID, insertSession *dbtype.InsertSession, req *sessioninfo.NewSessionRequest) error // SignInConflict classifies the error a transaction ended with. SignInConflict(err error) dbtype.SignInConflict // ConsumeMailLinkIn consumes the live mail link id inside tx, at the database's // time at commit. A link that is not live is NotFound, and the transaction may go // on. ConsumeMailLinkIn(ctx context.Context, tx Tx, id impulse.UUID, now time.Time) error // ClaimVerifiedEmailIn writes email on the user as verified inside tx when the // user holds no verified address yet and no other user holds email, and reports // whether it wrote. ClaimVerifiedEmailIn(ctx context.Context, tx Tx, userID impulse.UUID, email string) (bool, error) // ClaimUserNameIn writes name on the user inside tx when the user holds none, and // reports whether it wrote. A user inserted earlier in tx is not claimed for: its // insert carries the name. ClaimUserNameIn(ctx context.Context, tx Tx, userID impulse.UUID, name string) (bool, error) // UserByEmailIn reads the user holding email inside tx, verified or not, matched // without regard to case, or nil when there is none. UserByEmailIn(ctx context.Context, tx Tx, email string) (*dbtype.User, error) // UsernameIsEmail reports whether the store's usernames are the addresses. UsernameIsEmail() bool // SignInMethodIn reads the row of the sign-in method registered under name inside // tx: what an administrator chose for the method. It answers nil when the method // has no row, which is a method that is invite only. SignInMethodIn(ctx context.Context, tx Tx, name string) (*dbtype.SignInMethod, error) }
Driver is what the flow needs from a storage driver. Every primitive runs inside a transaction Transact opened and receives it.
type InvitationAcceptor ¶
type InvitationAcceptor[Tx any] func(ctx context.Context, tx Tx, userID impulse.UUID, username string, inv *sessioninfo.Invitation) (committed func(ctx context.Context), err error)
InvitationAcceptor is the application's half of an invitation's acceptance: it gives userID what the invitation names (its roles, in its tenant), inside the transaction that consumes the invitation, which it is handed: the invitation is spent and its roles given together, or neither. It reads and writes through tx alone: the Spanner emulator allows one read-write transaction at a time, so a second transaction or a read through another client inside the hook waits on the one it runs in. A user the same transaction provisioned is buffered and not readable in it (Spanner applies a buffered row at the commit), so the user is named by id, which is what the hook writes under, and by username, for what it reports.
committed, nil for none, is work the flow runs once that transaction has committed and before the sign-in policy decides: telling a permission engine that its rows changed, so the policy sees the membership. A transaction's closure may run more than once (Spanner retries an aborted transaction), so the hook is called once per run and only the committed function of the run that committed is called, once. An error fails the transaction: the invitation stands, a user the transaction would have provisioned is not created, and the sign-in ends with the error.
type OnlyInTenant ¶ added in v0.5.0
type OnlyInTenant[Tx any] func(ctx context.Context, tx Tx, userID impulse.UUID, tenant accesstypes.Domain) (bool, error)
OnlyInTenant is the application's half of the rule on a first link made on a name alone (linkUser): it reports whether tenant is the only place userID holds anything, read through tx, the transaction that would make the link. The user holds a role, or a place in a group, in tenant itself, and nothing in another tenant, in every tenant or outside the tenants: a tenant's directory vouches for the people who are the tenant's alone, since the link hands its directory everything the account holds. It is held to what InvitationAcceptor is for its reads: tx alone.
type Rules ¶
type Rules[Tx any] struct { // Account is the registration's own account rule, nil for none; see SignIn. Account signin.AccountRule[Tx] // Method is the name the sign-in's method is registered under, by which the // standard rule reads what an administrator chose for it (the method's row of the // sign-in methods table). A sign-in that names none is invite only. Method string // Audience is whom the method's registration admits now, as it spells itself // (signin.Audience.Key): the method's row opens it only where the row was opened for // that audience (dbtype.SignInMethod.OpenFor). Audience string // Policy is the sign-in policy, nil for none. Policy signin.Policy[Tx] // Accept is the application's invitation acceptor, nil for none. Accept InvitationAcceptor[Tx] // SignUp gives a person who signed up through an open method the method's default // role, nil for none; see standardRule. SignUp SignUpSeater[Tx] // Directories reads the declaration of the directory an identity came through at a // person's first sign-in through it, nil for none, and Seat seats the person with // the default seat the declaration names, nil for none; see standardRule. OnlyIn // reports whether a tenant is the only place a person holds anything, nil for // none: with Directories it decides a first link made on a name alone; see // linkUser. Directories signin.DirectoryReader[Tx] Seat DirectorySeater[Tx] OnlyIn OnlyInTenant[Tx] // Seeded reports whether an account is the deployment's own seed for the // registration the sign-in came through, nil for none, and OneOrganization says // that registration admits the people of exactly one organization and nobody else // (signin.Audience.OneOrganization): with Directories the two admit the seed's // first link on the name; see firstLink. Seeded SeededAccount OneOrganization bool // SeededUsers says the registration declared its provider's names the // application's own (signin.Registration.SeededUsers): the standard rule's first // link of a user created ahead is then made on the name alone; see firstLink. SeededUsers bool // TrustedDevice reports whether the browser is a trusted device of the user, nil // for none; see decideSignIn. TrustedDevice TrustedDeviceCheck // Proven is the proof of the password the sign-in rests on, nil when no password // was proved: the session is inserted only while the user still holds that password // (decideAndInsert). Proven dbtype.PasswordProof }
Rules are the application's rules a sign-in runs under.
type SeededAccount ¶ added in v0.5.0
SeededAccount is the application's word on an account the deployment's own seed created for a person who signs in through a directory: whether the account with this id and username is the one a seed made for the registration named (session.SeedUserIDThrough). It reads nothing. See firstLink.
type SignUpSeater ¶ added in v0.5.0
type SignUpSeater[Tx any] func(ctx context.Context, tx Tx, userID impulse.UUID, username, method, role string) (committed func(ctx context.Context), err error)
SignUpSeater is the application's half of a sign-up through a method an administrator opened: it gives userID, named by id and by username, the default role the method's row names, inside the transaction that makes the account, which it is handed: the account is made and holds the role together, or neither. It is called for a person the open method admitted, never for one whose account stood before, and at a directory's method not for one who came with a live invitation, whose roles are the invitation's. A person who signs up with a password from a mailed link is given the role, and an invitation the browser also carries is then accepted only where it was mailed to the address the sign-up verified. method is the name the method is registered under. It is held to what InvitationAcceptor is: tx alone for reads and writes, a committed function (nil for none) the flow runs once the transaction has committed and before the sign-in policy decides, and an error that fails the transaction, so no account is left holding nothing.
type TrustedDeviceCheck ¶
TrustedDeviceCheck reports whether the browser signing in is a trusted device of userID: a device the person asked to be remembered after a second factor, named by the trusted device cookie and live in the trusted devices table. The Auth supplies it with the cookie's token on the context, and it answers at once, without a read, for a browser that carries none. It is asked once the user is known and before the decision's transaction opens, since it reads the store on its own (Spanner refuses a read inside another transaction), and its yes records a trusted-device event on the session in the step's place when the sign-in policy holds the sign-in for MFA and admits a trusted device.