Documentation
¶
Overview ¶
Package federation signs humans in through external identity providers: Google and any other OpenID Connect issuer, with zitadel/oidc, and GitHub, with golang.org/x/oauth2 and GitHub's REST API. It returns an auth.Proof with auth.MethodFederated that the application passes to auth.Service.StartSession.
The package links accounts safely:
- An identity is keyed by its issuer and subject, never by an email address. An identity that no account holds never attaches to an existing account because an email address matches.
- Only a signed-in human whose session is fresh can link an identity to their account (Federation.BeginLink, Federation.FinishLink). Every link and unlink is audited in the same transaction.
- The library never creates accounts. A sign-in with an unknown identity calls the application's Config.SignUp hook, which decides whether to create one, for example only for an invited address.
- A federated sign-in is one factor: the session it starts is never fresh and never multi-factor, so it can't link or unlink identities, add a passkey with passkey.Passkeys.BeginRegistration, or claim an agent until it steps up with another factor. Offer such a session passkey.Passkeys.BeginFirstRegistration only when the user has no passkey, recovery codes, or TOTP factor; otherwise the provider account alone could add a passkey and skip the user's TOTP.
Every failure of a sign-in or a link returns auth.ErrFactorRejected, whatever the cause; auth.ReasonOf gives the reason to logs.
The flows:
- Sign-in or sign-up: Federation.BeginSignIn with a browser binding, redirect to the URL, then Federation.FinishSignIn on the callback with the same binding.
- Link: Federation.BeginLink from a fresh session, then Federation.FinishLink from the same session.
- Unlink: Federation.Unlink from a fresh session.
State, nonce, and the PKCE verifier stay server-side in a ceremony that the callback consumes once, so the application needs no ceremony cookie beyond the binding.
Index ¶
- Constants
- Variables
- type Config
- type External
- type Federation
- func (f *Federation) BeginLink(ctx context.Context, who auth.Principal, provider string) (string, error)
- func (f *Federation) BeginSignIn(ctx context.Context, provider, binding string) (string, error)
- func (f *Federation) FinishLink(ctx context.Context, who auth.Principal, callback url.Values) (Identity, error)
- func (f *Federation) FinishSignIn(ctx context.Context, binding string, callback url.Values) (SignIn, error)
- func (f *Federation) List(ctx context.Context, u auth.UserID) ([]Identity, error)
- func (f *Federation) Unlink(ctx context.Context, who auth.Principal, issuer, subject string) error
- type GitHubConfig
- type Identity
- type OIDCConfig
- type OtherSignInFunc
- type Provider
- type SignIn
- type SignUpFunc
- type Store
Constants ¶
const ( // ReasonProviderError means the provider sent an error to the // callback, or the code exchange or the account lookup failed. ReasonProviderError auth.Reason = "provider_error" // ReasonSignUpDeclined means the identity is unknown and the SignUp // hook refused it, or returned an account that already has a sign-in // method. ReasonSignUpDeclined auth.Reason = "signup_declined" )
Reasons this package reports through auth.ReasonOf, besides auth.ReasonCeremonyInvalid, auth.ReasonFactorInvalid, and auth.ReasonFactorUnknown.
const ( ActionBeginSignIn = "federation.begin_sign_in" ActionFinishSignIn = "federation.finish_sign_in" ActionFinishLink = "federation.finish_link" )
Limiter actions this package reports.
const FreshFor = 10 * time.Minute
FreshFor is how recently a session must have authenticated, with a factor other than a federated sign-in, to link or unlink an identity.
const GoogleIssuer = "https://accounts.google.com"
GoogleIssuer is Google's OpenID Connect issuer. Pass it to NewOIDC to sign users in with Google.
Variables ¶
var ( // ErrLastSignInMethod means unlinking the identity would leave the // user unable to sign in. ErrLastSignInMethod = errors.New("federation: can't remove the last sign-in method") // ErrConfig means the configuration is invalid. Its message names // the field and the fix. It is a setup error: log it and stop the // server, and never pass it to auth.Describe, which turns it into a // bare internal error. ErrConfig = errors.New("federation: invalid configuration") )
Errors returned by this package, besides auth.ErrFactorRejected, auth.ErrThrottled, auth.ErrStepUpRequired, and auth.ErrFactorExists.
var ( NeedLink = auth.Need{Action: "federation.link", Assurance: auth.AssuranceSingle, Within: FreshFor, Human: true} NeedUnlink = auth.Need{Action: "federation.unlink", Assurance: auth.AssuranceSingle, Within: FreshFor, Human: true} )
Needs that linking and unlinking check. auth.WithNeeds tightens them by Action. A federated sign-in alone is never fresh, so it can't link another identity.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Providers users can sign in with. Names and issuers must be unique.
Providers []Provider
// SignUp decides whether an unknown identity creates an account. Nil
// means sign-in with an unknown identity always fails.
SignUp SignUpFunc
// OtherSignIn reports sign-in methods that the application manages.
// Nil means the user has none.
OtherSignIn OtherSignInFunc
// CeremonyTTL bounds the time between a begin and a finish step. The
// default is 10 minutes, and the most is 30 minutes.
CeremonyTTL time.Duration
}
Config configures Federation.
type External ¶
type External struct {
// Provider is the [Provider.Name] the user signed in with.
Provider string
// Issuer and Subject identify the account.
Issuer, Subject string
// Email is the address the provider reported, or empty.
Email string
// EmailVerified is true when the provider says it verified Email.
EmailVerified bool
// Name is the display name the provider reported, or empty.
Name string
// Login is the user name at GitHub, or empty for other providers. It
// can change; Subject can't.
Login string
}
External describes an account at an identity provider, as the provider reported it during one sign-in.
Only Issuer and Subject identify the account. Don't use Email to find an existing account: an email address at a provider proves nothing about who holds the account with that address in your application.
type Federation ¶
type Federation struct {
// contains filtered or unexported fields
}
Federation runs sign-in and linking ceremonies with identity providers. It is safe for concurrent use.
func New ¶
New returns a Federation that keeps ceremonies and identities in store and uses svc's clock, limiter, observer, and logger.
func (*Federation) BeginLink ¶
func (f *Federation) BeginLink(ctx context.Context, who auth.Principal, provider string) (string, error)
BeginLink starts linking an identity at the named provider to the human behind who, whose session must have authenticated within FreshFor with a factor other than a federated sign-in. Finish it with Federation.FinishLink from the same session.
func (*Federation) BeginSignIn ¶
BeginSignIn starts a sign-in, or a sign-up, with the named provider and returns the URL to redirect the browser to. binding ties the ceremony to the browser that began it, for example a random value the application stored in a short-lived cookie; the callback must present the same value, so an attacker can't sign a victim's browser in to the attacker's account.
func (*Federation) FinishLink ¶
func (f *Federation) FinishLink(ctx context.Context, who auth.Principal, callback url.Values) (Identity, error)
FinishLink completes a link begun by Federation.BeginLink for the session behind who, with the query of the provider's callback request, and returns the linked identity. The session must still be fresh. A link begun in another session, even one of the same user, is rejected.
It returns auth.ErrFactorExists when the identity is already linked, to this or another account: telling the person who just proved control of the identity reveals nothing they couldn't learn by signing in with it. Any other failure returns auth.ErrFactorRejected.
func (*Federation) FinishSignIn ¶
func (f *Federation) FinishSignIn(ctx context.Context, binding string, callback url.Values) (SignIn, error)
FinishSignIn completes a sign-in begun by Federation.BeginSignIn with the query of the provider's callback request and the binding given when it began. A known identity signs its user in. An unknown one goes to the SignUp hook; without a hook, or when it refuses, the sign-in fails. Any failure returns auth.ErrFactorRejected.
func (*Federation) Unlink ¶
Unlink removes the identity with the given issuer and subject from the human behind who, whose session must have authenticated within FreshFor with a factor other than a federated sign-in.
It returns ErrLastSignInMethod instead of removing the user's last way to sign in that the library knows about (identities, passkeys, recovery codes, and a usable password when the store is an auth.EmailStore), unless the OtherSignIn hook reports another. Two concurrent unlinks can both pass that check; it protects against mistakes, not against the account holder.
type GitHubConfig ¶
type GitHubConfig struct {
// Name identifies the provider. The default is "github".
Name string
// ClientID and ClientSecret are the OAuth app's or GitHub App's
// credentials.
ClientID, ClientSecret string
// RedirectURL is the application's callback URL, registered with the
// app.
RedirectURL string
// BaseURL is the web address, and the issuer of stored identities. The
// default is "https://github.com".
BaseURL string
// APIURL is the REST API address. The default is
// "https://api.github.com", or BaseURL followed by "/api/v3" when
// BaseURL is set.
APIURL string
// HTTPClient makes token and API requests. The default has a 30-second
// timeout.
HTTPClient *http.Client
}
GitHubConfig configures GitHub, or GitHub Enterprise Server, for NewGitHub.
type Identity ¶
type Identity struct {
// Provider is the name of the configured provider with this issuer, or
// empty when no configured provider has it.
Provider string
Issuer, Subject string
CreatedAt time.Time
LastUsedAt time.Time
}
Identity is an external identity linked to a user, for display.
type OIDCConfig ¶
type OIDCConfig struct {
// Name identifies the provider, for example "google": 1 to 32
// lowercase letters, digits, and hyphens.
Name string
// Issuer is the provider's issuer URL, for example [GoogleIssuer]. Its
// discovery document must name the same issuer.
Issuer string
// ClientID and ClientSecret are the application's credentials at the
// provider.
ClientID, ClientSecret string
// RedirectURL is the application's callback URL, registered at the
// provider.
RedirectURL string
// Scopes to ask for. The default is openid, email, and profile; openid
// is always added.
Scopes []string
// HTTPClient makes discovery, key, and token requests. The default has
// a 30-second timeout.
HTTPClient *http.Client
}
OIDCConfig configures an OpenID Connect provider for NewOIDC.
type OtherSignInFunc ¶
OtherSignInFunc reports whether u can sign in some way the library doesn't manage, such as an application's own password or enterprise sign-in. Federation.Unlink asks it before removing a user's last sign-in method that the library knows about.
type Provider ¶
type Provider interface {
// Name identifies the provider in the application, for example
// "google". It appears in URLs and in the audit log.
Name() string
// Issuer identifies the provider in stored identities, for example
// "https://accounts.google.com".
Issuer() string
// contains filtered or unexported methods
}
Provider is an identity provider that users sign in with. Create one with NewOIDC or NewGitHub. A Provider is safe for concurrent use.
func NewGitHub ¶
func NewGitHub(cfg GitHubConfig) (Provider, error)
NewGitHub returns GitHub as a provider. GitHub isn't an OpenID Connect provider, so the library signs in with OAuth 2 and PKCE (S256), and reads the account from GitHub's REST API with the access token: the subject is the numeric account ID, which never changes, not the login, which can. The access token isn't kept.
func NewOIDC ¶
func NewOIDC(ctx context.Context, cfg OIDCConfig) (Provider, error)
NewOIDC returns an OpenID Connect provider, such as Google. It fetches the issuer's discovery document, so it makes a network request.
Every sign-in uses the authorization code flow with PKCE (S256) and a nonce. The ID token must be signed with an algorithm the discovery document lists, by a key from the issuer's key set, for this client, by this issuer, and unexpired.
type SignIn ¶
type SignIn struct {
// Proof is for the user who signed in. Pass it to
// auth.Service.StartSession.
Proof auth.Proof
// External is the account as the provider reported it.
External External
// Created is true when the SignUp hook created the account in this
// sign-in.
Created bool
}
SignIn is the result of a successful Federation.FinishSignIn.
type SignUpFunc ¶
SignUpFunc decides whether a sign-in with an identity that no account holds creates an account. To create one, create it in the application and return its user ID; the library then links the identity to it. To refuse, return "" and nil; the sign-in then fails like any other.
Return only an account you created in this call. Never look up an existing account by ext.Email and return it: that hands the account to whoever controls the address at the provider. The library refuses an account that already has an identity, a passkey, recovery codes, or a TOTP factor, but it can't see sign-in methods that the application manages itself.
Any error other than a refusal is a server error, returned wrapped.
type Store ¶
type Store interface {
auth.FactorStore
auth.IdentityStore
}
Store is what Federation needs from a store: ceremonies and passkey credentials from auth.FactorStore, and identities. memstore, sqlitestore, and pgstore implement it.