Documentation
¶
Overview ¶
Package provider is Portiko's identity provider: passkey ceremonies, a set of signed-in accounts per browser, and an OpenID Connect provider built on zitadel/oidc that issues audience-scoped tokens to every site.
Package handlers holds the HTTP surface: discovery, the passkey ceremonies, and the OAuth endpoints.
Index ¶
- Constants
- Variables
- func ClearSSOCookie(w http.ResponseWriter, path string, secure bool)
- func Migrate(ctx context.Context, databaseURL string) (int, error)
- func NegotiatedVersion(r *http.Request) protocol.Version
- func Router(d Deps) http.Handler
- func SetSSOCookie(w http.ResponseWriter, path, token string, expires time.Time, secure bool)
- func VersionMiddleware(next http.Handler) http.Handler
- type Auth
- func (h *Auth) AddEmail(w http.ResponseWriter, r *http.Request)
- func (h *Auth) FinishLogin(w http.ResponseWriter, r *http.Request)
- func (h *Auth) FinishRegistration(w http.ResponseWriter, r *http.Request)
- func (h *Auth) GetPassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) ListEmails(w http.ResponseWriter, r *http.Request)
- func (h *Auth) ListPasskeys(w http.ResponseWriter, r *http.Request)
- func (h *Auth) LoginPassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Logout(w http.ResponseWriter, r *http.Request)
- func (h *Auth) LogoutAll(w http.ResponseWriter, r *http.Request)
- func (h *Auth) MakePrimaryEmail(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RegisterPassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RemoveEmail(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RemovePassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RenamePasskey(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RevokePasskey(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Session(w http.ResponseWriter, r *http.Request)
- func (h *Auth) SetDefault(w http.ResponseWriter, r *http.Request)
- func (h *Auth) SetPassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) SignInEvent(w http.ResponseWriter, r *http.Request)
- func (h *Auth) StartLogin(w http.ResponseWriter, r *http.Request)
- func (h *Auth) StartRegistration(w http.ResponseWriter, r *http.Request)
- func (h *Auth) UpdateProfile(w http.ResponseWriter, r *http.Request)
- func (h *Auth) VerifyCode(w http.ResponseWriter, r *http.Request)
- func (h *Auth) VerifyEmail(w http.ResponseWriter, r *http.Request)
- type Client
- type Config
- type Deps
- type Discovery
- type EmailChange
- type EmailNotice
- type Login
- type Mailer
- type Options
- type Pages
- func (p *Pages) Emails(w http.ResponseWriter, r *http.Request)
- func (p *Pages) EmailsRedirect(w http.ResponseWriter, r *http.Request)
- func (p *Pages) Home(w http.ResponseWriter, r *http.Request)
- func (p *Pages) Passkeys(w http.ResponseWriter, r *http.Request)
- func (p *Pages) PasskeysRedirect(w http.ResponseWriter, r *http.Request)
- func (p *Pages) Profile(w http.ResponseWriter, r *http.Request)
- func (p *Pages) ProfileRedirect(w http.ResponseWriter, r *http.Request)
- func (p *Pages) Root(w http.ResponseWriter, r *http.Request)
- func (p *Pages) SignIn(w http.ResponseWriter, r *http.Request)
- type PasskeyNotice
- type PasswordChange
- type PasswordNotice
- type Provider
- type RegistrationPolicy
- type SignInEvent
- type SignInOutcome
- type SignInStage
Constants ¶
const ( PasswordAdded = mailer.PasswordAdded PasswordReplaced = mailer.PasswordReplaced PasswordRemoved = mailer.PasswordRemoved )
The changes a PasswordNotice reports.
const ( EmailAdded = mailer.EmailAdded EmailRemoved = mailer.EmailRemoved EmailPrimaryChanged = mailer.EmailPrimaryChanged )
The changes an EmailNotice reports.
const AccessTokenTTL = 15 * time.Minute
AccessTokenTTL bounds how long a revoked account keeps working access at a site. With server-side refresh the renewal is invisible to the person.
const ErrRateLimited = "rate_limited"
ErrRateLimited is the error code a limited request is answered with.
It is the same body for every cause and for every address: whether the budget that ran out was the caller's IP or the address it named, and whether that address has an account, are all invisible from the outside.
const ErrRegistrationRefused = "registration_refused"
ErrRegistrationRefused is the error code a RegistrationPolicy refusal is answered with.
const IDTokenTTL = 5 * time.Minute
IDTokenTTL is short because an ID token is consumed at sign-in and never presented to an API.
const ReauthWindow = 10 * time.Minute
ReauthWindow is how recently an account must have signed in in this browser, with a passkey or (PASSWORDS=allow) its password, to add an address, remove one or change the primary, or to change its passkeys or password.
A session cookie alone is not enough for these. Each one decides which inboxes can recover the account, so a borrowed laptop or a stolen cookie that could do them would be a way to keep the account for good. Signing in again as the same account resets the clock.
const SSOCookieName = "accounts_sso"
SSOCookieName is the provider's own cookie. Its value names a browser; the accounts signed in on that browser are rows keyed by the value's hash.
Variables ¶
var Minimum = protocol.V1_0
Minimum is what a client is assumed to speak when it sends no version header.
var Preferred = protocol.V1_0
Preferred is the version advertised as best.
var Supported = []protocol.Version{protocol.V1_0}
Supported is the set of protocol versions this build serves. A new minor is appended here; a new major is served at its own path prefix alongside the old.
Functions ¶
func ClearSSOCookie ¶
func ClearSSOCookie(w http.ResponseWriter, path string, secure bool)
ClearSSOCookie expires the browser cookie set for the same path.
func Migrate ¶
Migrate brings the database at databaseURL up to the schema this version of the provider needs, and returns the version it ends at. Call it before New.
It is safe to call on every start and from several replicas at once, and it keeps the same schema_migrations table as the golang-migrate CLI, so either can be used on the same database.
func NegotiatedVersion ¶
NegotiatedVersion returns the version chosen for this request.
func SetSSOCookie ¶
SetSSOCookie writes the browser cookie, limited to path (the provider's base path, "" at the root of a host).
SameSite=Lax is what makes single sign-on work: the cookie must be readable when the browser arrives on a top-level redirect from a different site. Strict would suppress it on exactly that navigation. Lax still withholds it from cross-site POSTs, which is the case that matters for CSRF.
func VersionMiddleware ¶
VersionMiddleware negotiates the protocol version for every request.
A client that asks for a major we do not serve gets a 400 naming what we do serve — not a 404, which would look like a routing mistake and send the reader looking in the wrong place.
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth serves the passkey ceremonies, the browser's accounts and signing out.
func (*Auth) AddEmail ¶
func (h *Auth) AddEmail(w http.ResponseWriter, r *http.Request)
AddEmail: POST /accounts/v1/emails/add {"account","email"} — sends a code to the address. Always 202 for an address that is not refused outright: one already on an account is sent nothing, and the answer is the same.
func (*Auth) FinishLogin ¶
func (h *Auth) FinishLogin(w http.ResponseWriter, r *http.Request)
FinishLogin: POST /accounts/v1/login/finish[?authRequestID=]
func (*Auth) FinishRegistration ¶
func (h *Auth) FinishRegistration(w http.ResponseWriter, r *http.Request)
FinishRegistration: POST /accounts/v1/register/finish[?authRequestID=]
func (*Auth) GetPassword ¶
func (h *Auth) GetPassword(w http.ResponseWriter, r *http.Request)
GetPassword: GET /accounts/v1/password?account=n
{"account", "allowed", "password": null | {"created_at", "updated_at", "last_used_at"}, "reauth_until", "held_until"}. allowed says whether this deployment lets a password be set; an account may still hold one after it stopped, and may remove it.
func (*Auth) ListEmails ¶
func (h *Auth) ListEmails(w http.ResponseWriter, r *http.Request)
ListEmails: GET /accounts/v1/emails?account=n
func (*Auth) ListPasskeys ¶
func (h *Auth) ListPasskeys(w http.ResponseWriter, r *http.Request)
ListPasskeys: GET /accounts/v1/passkeys?account=n
func (*Auth) LoginPassword ¶
func (h *Auth) LoginPassword(w http.ResponseWriter, r *http.Request)
LoginPassword: POST /accounts/v1/login/password[?authRequestID=] {"email", "password"}
Signs in with any verified address on the account and its password. Every failure is the generic 401 (docs/opinions.md), including an account waiting out its per-account backoff. The answer is the one login/finish gives, and the session's amr is ["pwd"].
func (*Auth) Logout ¶
func (h *Auth) Logout(w http.ResponseWriter, r *http.Request)
Logout: POST /accounts/v1/logout {"account": n}
With an account, that account signs out of this browser. Without one, every account in this browser does. Either way its refresh tokens are revoked and each site that held one is told. A form post (the home page's buttons) is redirected; a script gets JSON.
func (*Auth) LogoutAll ¶
func (h *Auth) LogoutAll(w http.ResponseWriter, r *http.Request)
LogoutAll: POST /accounts/v1/logout/all {"account": n} — that account, in every browser, with every refresh token it holds.
func (*Auth) MakePrimaryEmail ¶
func (h *Auth) MakePrimaryEmail(w http.ResponseWriter, r *http.Request)
MakePrimaryEmail: POST /accounts/v1/emails/{id}/primary {"account"}
func (*Auth) RegisterPassword ¶
func (h *Auth) RegisterPassword(w http.ResponseWriter, r *http.Request)
RegisterPassword: POST /accounts/v1/register/password[?authRequestID=] {"token", "password"}
The other way to finish register/verify: token is the password_token it answered with, and instead of creating a passkey the account gets this password. A new address becomes an account; an address that has one is recovery, which sets (or replaces) the account's password under the recovery hold. The answer is the one register/finish gives.
A password the policy refuses is 400 weak_password, with the reason, and the token stays usable. Any other failure is the generic 401.
func (*Auth) RemoveEmail ¶
func (h *Auth) RemoveEmail(w http.ResponseWriter, r *http.Request)
RemoveEmail: POST /accounts/v1/emails/{id}/remove {"account"}
func (*Auth) RemovePassword ¶
func (h *Auth) RemovePassword(w http.ResponseWriter, r *http.Request)
RemovePassword: POST /accounts/v1/password/remove {"account"}
Takes the password off the account, whether or not passwords are still allowed. Held like revoking a passkey (guardHeld). 404 not_found when the account has none, and 409 cannot_remove_last_credential when it has no passkey: the account would have no way to sign in.
func (*Auth) RenamePasskey ¶
func (h *Auth) RenamePasskey(w http.ResponseWriter, r *http.Request)
RenamePasskey: POST /accounts/v1/passkeys/{id}/rename {"account", "name"}
The name is Portiko's own label for the credential. WebAuthn has no signal that renames one credential — signalCurrentUserDetails names the account's user, which the profile page already sends — so nothing is signaled here.
func (*Auth) RevokePasskey ¶
func (h *Auth) RevokePasskey(w http.ResponseWriter, r *http.Request)
RevokePasskey: POST /accounts/v1/passkeys/{id}/revoke {"account"}
Needs a passkey ceremony within ReauthWindow, not with a passkey in its recovery hold (403 reauthentication_required or recovery_hold): a stolen cookie, or a passkey added from someone else's inbox, must not be able to remove the holder's own. Refused with 409 when it is the account's last passkey. The response carries a "signal" for signalAllAcceptedCredentials so the passkey provider drops the credential too.
func (*Auth) Session ¶
func (h *Auth) Session(w http.ResponseWriter, r *http.Request)
Session: GET /accounts/v1/session[?account=n] — whether an account is signed in on this browser.
Pages call it when they come back into view, because signing in and out usually happens in another tab and nothing else tells an open page. An account is named by its public sid, so a page can tell "signed in again" from "still signed in"; neither the cookie nor the subject is returned.
func (*Auth) SetDefault ¶
func (h *Auth) SetDefault(w http.ResponseWriter, r *http.Request)
SetDefault: POST /accounts/v1/default {"account": n}
func (*Auth) SetPassword ¶
func (h *Auth) SetPassword(w http.ResponseWriter, r *http.Request)
SetPassword: POST /accounts/v1/password {"account", "password"}
Adds a password to the account, or replaces the one it has. Adding needs a sign-in within ReauthWindow, as adding an address does; replacing takes the old password away, so it is held like removing one (guardHeld). A password set by a session in its recovery hold carries the same hold.
func (*Auth) SignInEvent ¶
func (h *Auth) SignInEvent(w http.ResponseWriter, r *http.Request)
SignInEvent: POST /signin/event[?authRequestID=|?flow=] {"stage","error"}
The sign-in page reports a passkey dialog that failed or was dismissed, which the server cannot see. Always 204: the page never waits on or learns from it.
func (*Auth) StartLogin ¶
func (h *Auth) StartLogin(w http.ResponseWriter, r *http.Request)
StartLogin: GET /accounts/v1/login/start → assertion options.
func (*Auth) StartRegistration ¶
func (h *Auth) StartRegistration(w http.ResponseWriter, r *http.Request)
StartRegistration: POST /accounts/v1/register/start {"email", "bootstrap_code"?}
Always 202, always the same body. Whether the address is new, already registered, malformed, or refused for want of the bootstrap code is not disclosed. The one exception is an embedder's RegistrationPolicy: when it turns a new address away the answer is 403 registration_refused with the policy's message in error_description (docs/registration.md).
func (*Auth) UpdateProfile ¶
func (h *Auth) UpdateProfile(w http.ResponseWriter, r *http.Request)
UpdateProfile: POST /accounts/v1/profile {"account", "display_name", "avatar"}
Requiring a JSON content type means a cross-site page cannot post here without a CORS preflight, and the cookie is SameSite=Lax besides. The account is named explicitly: with several signed in, the server never guesses which one a request means.
func (*Auth) VerifyCode ¶
func (h *Auth) VerifyCode(w http.ResponseWriter, r *http.Request)
VerifyCode: POST /accounts/v1/register/verify {"email","code"} → creation options, and with PASSWORDS=allow a password_token for register/password.
func (*Auth) VerifyEmail ¶
func (h *Auth) VerifyEmail(w http.ResponseWriter, r *http.Request)
VerifyEmail: POST /accounts/v1/emails/verify {"account","email","code"} — adds the address. A wrong code and an address taken by any account get the same 401 authentication_failed.
type Client ¶
type Client struct {
// ID is the OAuth client_id.
ID string
// Secret authenticates a confidential client. Leave it empty for a native
// app, which authenticates with PKCE alone; Native must then be set.
Secret string
// Audience is the site's origin, e.g. https://example.com. It is the
// audience of the client's access tokens and is shown on the home page.
Audience string
// DisplayName is what people see; it defaults to ID.
DisplayName string
// RedirectURIs are the exact callback URLs the client may use. Required.
RedirectURIs []string
// PostLogoutRedirectURIs are where RP-initiated logout may return to.
PostLogoutRedirectURIs []string
// Scopes the client may be granted; empty means openid, profile and
// offline_access.
Scopes []string
// FirstParty marks a client this deployment operates: it skips consent and
// is listed on the signed-in home page.
FirstParty bool
// Native marks a public app (RFC 8252) rather than a web site.
Native bool
// BackchannelLogoutURI receives a logout token when someone signs out.
BackchannelLogoutURI string
// EventsURI receives pushed security events, such as a profile change.
EventsURI string
}
Client is a site or app that signs people in with this provider.
type Config ¶
Config is the provider's runtime configuration.
func LoadConfig ¶
LoadConfig reads configuration from the environment, reporting every problem at once.
type Deps ¶
type Deps struct {
OpenID http.Handler
Discovery *Discovery
Auth *Auth
Login *Login
Pages *Pages
Health http.HandlerFunc
Limiter *limiter
}
Deps is everything the router needs.
type Discovery ¶
type Discovery struct {
// contains filtered or unexported fields
}
Discovery serves the Portiko configuration document.
func (*Discovery) Configuration ¶
func (d *Discovery) Configuration(w http.ResponseWriter, _ *http.Request)
Configuration serves the protocol version range. OpenID Connect discovery covers everything else and comes from zitadel/oidc.
type EmailChange ¶
type EmailChange = mailer.EmailChange
EmailChange names what an EmailNotice reports.
type EmailNotice ¶
type EmailNotice = mailer.EmailNotice
EmailNotice describes a change to an account's addresses: one added, one removed, or another made primary. It is sent to every address on the account and to the one removed.
type Login ¶
type Login struct {
// contains filtered or unexported fields
}
Login is the handoff between zitadel/oidc and the people signing in: the provider sends every authorization request here by id, and this decides whether an account already signed in can complete it, whether the person must choose, or whether a passkey ceremony is needed.
type Mailer ¶
Mailer delivers the provider's messages: the one-time code that starts a ceremony or confirms an address, the notice that a passkey was added to an existing account, the notice that an account's addresses changed, and the notice that its password changed. Implement it to send through something other than SMTP; every method is required, because a deployment that changes an account silently gives the holder no way to notice a compromised mailbox.
type Options ¶
type Options struct {
// Profiles supplies how an account is presented to sites. Nil uses the
// built-in display name and emoji avatar.
Profiles profile.Source
// Mailer delivers one-time codes and new-passkey notices. Nil uses SMTP
// when configured, otherwise the log.
Mailer Mailer
// RegistrationPolicy decides at runtime whether an address with no account
// may create one. It is called at the start of registration with the
// normalized email, before any code is mailed or row stored, and never for
// an address that already has an account (recovery is not gated). Deny with
// a message to have it shown on the sign-in page; an empty message becomes
// "Registration is currently closed.". An error fails closed with a generic
// message. It is asked again when the emailed code is verified, so closing
// registration also stops sign-ups already in flight. When set it replaces the REGISTRATION / BOOTSTRAP_CODE decision;
// nil leaves that setting in charge.
RegistrationPolicy RegistrationPolicy
// OnSignInEvent, when set, is called once for each stage of each sign-in
// attempt on the sign-in page: shown, email submitted, code sent and
// verified, passkey created or used, and the redirect back to the site
// (docs/sign-in-events.md). It is called synchronously on the request
// goroutine, so it must return fast; hand the event off rather than doing
// I/O inline. An event never carries an email address, IP address or user
// ID, and Portiko stores none of them: this hook is the only sink. Nil, the
// default, turns the events off.
OnSignInEvent func(context.Context, SignInEvent)
}
Options are the extension points a deployment may fill.
type Pages ¶
type Pages struct {
// contains filtered or unexported fields
}
Pages serves the provider's own pages.
func NewPages ¶
NewPages loads the templates, preferring an operator's override directory, so a second deployment can be rebranded without a fork.
func (*Pages) Emails ¶
func (p *Pages) Emails(w http.ResponseWriter, r *http.Request)
Emails: GET /u/{n}/emails — list, add, make primary and remove the account's addresses. docs/emails.md lists the template data.
func (*Pages) EmailsRedirect ¶
func (p *Pages) EmailsRedirect(w http.ResponseWriter, r *http.Request)
EmailsRedirect: GET /emails — sites, and the address-change notices, link here without knowing numbers.
func (*Pages) Home ¶
func (p *Pages) Home(w http.ResponseWriter, r *http.Request)
Home: GET /u/{n}/ — one account's page, with every account in this browser in the switcher.
func (*Pages) Passkeys ¶
func (p *Pages) Passkeys(w http.ResponseWriter, r *http.Request)
Passkeys: GET /u/{n}/passkeys — list, rename and revoke the account's passkeys.
func (*Pages) PasskeysRedirect ¶
func (p *Pages) PasskeysRedirect(w http.ResponseWriter, r *http.Request)
PasskeysRedirect: GET /passkeys — sites link here without knowing numbers.
func (*Pages) Profile ¶
func (p *Pages) Profile(w http.ResponseWriter, r *http.Request)
Profile: GET /u/{n}/profile
func (*Pages) ProfileRedirect ¶
func (p *Pages) ProfileRedirect(w http.ResponseWriter, r *http.Request)
ProfileRedirect: GET /profile — sites link here without knowing numbers.
type PasskeyNotice ¶
type PasskeyNotice = mailer.PasskeyNotice
PasskeyNotice describes a passkey just added to an account that already existed. Authenticator is empty when the passkey's provider is unknown, which is the common case, so a message must read sensibly without it.
type PasswordChange ¶
type PasswordChange = mailer.PasswordChange
PasswordChange names what a PasswordNotice reports.
type PasswordNotice ¶
type PasswordNotice = mailer.PasswordNotice
PasswordNotice describes a change to an account's password: one added, replaced or removed (PASSWORDS=allow). It is sent to the primary address.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is a running identity provider.
func New ¶
New connects to the database, loads or creates the signing key, and builds the HTTP surface. The schema must be current: call Migrate first. Configuration mistakes fail here rather than at a person's first sign-in.
func (*Provider) Close ¶
func (p *Provider) Close()
Close releases the database and the in-process rate limiters.
func (*Provider) Handler ¶
Handler is the provider's complete HTTP surface.
Where it must be mounted follows from the issuer. With no path in the issuer (https://accounts.example.com) mount it at the root: it owns every path of its host. With a path (https://go.example.com/id) the provider lives under that path and Handler already expects it: mount it, unmodified, at the issuer's path with a trailing slash, and do not wrap it in http.StripPrefix.
mux.Handle("/id/", p.Handler()) // ISSUER=https://go.example.com/id
Requests outside the prefix get 404, so the host's own routes stay the host's. Cfg.BasePath() reports the prefix. Every URL the provider emits (discovery, endpoints, redirects, links, form actions and the pages' script fetches) carries it, and its cookie is limited to it.
func (*Provider) RegisterClient ¶
RegisterClient creates the client, or replaces it with exactly this declaration, so a site can register itself on every start. It does not reactivate a client an operator has switched off.
type RegistrationPolicy ¶
type RegistrationPolicy = accounts.RegistrationPolicy
RegistrationPolicy is the shape of Options.RegistrationPolicy.
type SignInEvent ¶
type SignInEvent struct {
// FlowID ties the stages of one attempt together: the authorization request
// ID when a site sent the person, otherwise a random ID minted per page view.
FlowID string
// ClientID is the site being signed in to, when there is one.
ClientID string
Stage SignInStage
Outcome SignInOutcome
Reason string
UserAgent string
// Source is "server" for stages the provider observed itself, "browser" for
// those the sign-in page reported.
Source string
Time time.Time
}
SignInEvent is one stage of one sign-in attempt. It never carries an email address, an IP address or a user ID.
type SignInOutcome ¶
type SignInOutcome string
SignInOutcome says how a stage ended.
const ( // OutcomeOK: the stage succeeded. OutcomeOK SignInOutcome = "ok" // OutcomeFail: the stage failed; Reason says why. OutcomeFail SignInOutcome = "fail" // OutcomeAbandon: the person dismissed the browser's passkey dialog. OutcomeAbandon SignInOutcome = "abandon" )
type SignInStage ¶
type SignInStage string
SignInStage names one step of the sign-in page's funnel. docs/sign-in-events.md lists every stage and when it fires.
const ( StagePageShown SignInStage = "page_shown" StageEmailSubmitted SignInStage = "email_submitted" StageCodeSent SignInStage = "code_sent" StageCodeVerified SignInStage = "code_verified" StagePasskeyCreateStarted SignInStage = "passkey_create_started" StagePasskeyCreateDone SignInStage = "passkey_create_finished" StagePasskeySignInStarted SignInStage = "passkey_signin_started" StagePasskeySignInDone SignInStage = "passkey_signin_finished" // The password credential (PASSWORDS=allow): a password chosen instead of // a passkey after the code, and a sign-in with one. StagePasswordSet SignInStage = "password_set" StagePasswordSignInDone SignInStage = "password_signin_finished" StageRedirected SignInStage = "redirected" )
The stages, in the order a person meets them.
const ( StageEmailAdded SignInStage = "email_added" StageEmailRemoved SignInStage = "email_removed" StageEmailPrimaryChanged SignInStage = "email_primary_changed" )
Account stages: changes a signed-in person makes to their account's email addresses. They carry no flow or client, and never the address.