Documentation
¶
Overview ¶
Package oidc is an OpenID Connect relying party for browser login: Start redirects to the provider's hosted login, Callback exchanges the authorization code and hands the caller's claims to the product.
Any provider publishing an OpenID discovery document whose issuer equals the configured issuer, give or take a trailing slash, works. The product owns accounts and sessions through the Provision and Login hooks; this package stores nothing. State, the PKCE verifier and the nonce travel in a short-lived SameSite=Lax cookie rather than a session, because the callback arrives as a cross-site top-level navigation that a SameSite=Strict session cookie does not accompany.
Index ¶
Constants ¶
const ( CodeIDPError = "idp_error" CodeFlowExpired = "flow_expired" CodeStateMismatch = "state_mismatch" CodeMissingCode = "missing_code" CodeExchangeFailed = "exchange_failed" CodeInvalidIdentity = "invalid_identity" CodeLoginFailed = "login_failed" CodeInvalidReturnTo = "invalid_return_to" )
Codes of the failures this package creates.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Claims ¶
type Claims struct {
// Issuer is the configured issuer the login went through; a verified id_token's iss equals it or differs from it only by a trailing slash.
Issuer string
Subject string
Email string
PreferredUsername string
// SessionID is the provider's session id (the sid claim), which a back-channel logout names.
SessionID string
// Groups holds the string values of Config.GroupsClaim.
Groups []string
// contains filtered or unexported fields
}
Claims is the identity a login produced.
An id_token is verified per OIDC Core 3.1.3.7 before any claim is read: signature against the provider's JWKS (asymmetric algorithms only), iss, aud and azp, exp, iat and the nonce sent with the authorization request. A response with no id_token but an inline profile is trusted on the server-side code exchange alone: TLS, client authentication, state and PKCE. Bearer tokens presented by callers are a different surface.
func (Claims) Claim ¶ added in v0.4.0
Claim returns the named claim of the verified id_token as JSON decoded it (string, bool, float64, []any, map[string]any), or nil when absent. A login with no id_token has no verified claims, so it reads nil for every name.
func (Claims) StringClaim ¶ added in v0.2.0
StringClaim returns the named claim of the verified id_token when its value is a string, else "". A login with no id_token has no verified claims, so it reads "" for every name.
type Config ¶
type Config struct {
// Issuer is the provider's issuer URL; endpoints and signing keys are discovered from its metadata. It must equal the metadata's issuer or differ from it only by one trailing slash; id_tokens must carry the metadata's issuer exactly.
Issuer string
ClientID string
// ClientSecret is sent in the token request's form parameters; empty for a public client, which relies on PKCE alone.
ClientSecret string
RedirectURL string
Scopes []string
// AuthParams are extra authorization-URL query parameters some providers require.
AuthParams map[string]string
// GroupsClaim names the id_token claim holding the user's groups as a string array; empty reads none.
GroupsClaim string
// PostLoginURL is where the browser goes once Login succeeds; empty means "/".
PostLoginURL string
}
Config is one provider registration. A RelyingParty reads it on every request, so a changed setting applies to the next login.
type Failure ¶
type Failure struct {
Status int
Code string
Message string
Name string
// Return is the return target of this login once the callback has read and re-validated it from the flow cookie, "" otherwise. The callback sets it, overwriting any value a hook gave.
Return string
Err error
}
Failure is a refused login: the callback answers Status with Message. Code is a stable machine-readable reason, Name who was refused as far as known ("" until the provider has named anyone), Err the cause. Provision and Login return one to choose the response.
type RelyingParty ¶
type RelyingParty[U any] struct { // Config returns the current registration, or nil when login is disabled, which both handlers answer with 404. Config func() *Config // FlowCookie names the cookie carrying state and the PKCE verifier from Start to Callback; it defaults to "oidc_flow". FlowCookie string // CookieSecure marks the flow cookie HTTPS-only. CookieSecure bool // ReturnParam names the Start query parameter holding where the browser goes after this login instead of PostLoginURL; it defaults to "rd". Without ValidateReturn only a path on this origin is honoured, anything else is ignored. ReturnParam string // ValidateReturn, when set, decides which ReturnParam values are honoured and returns the target to store. Start refuses a login whose value it rejects with CodeInvalidReturnTo, and the callback checks the stored target with it again, because the flow cookie is not signed. ValidateReturn func(r *http.Request, raw string) (target string, ok bool) // Provision returns the product's user for c, creating it if the product's rules allow. Provision func(ctx context.Context, c Claims) (U, error) // Login starts the product's session for u. Login func(w http.ResponseWriter, r *http.Request, u U, c Claims) error // OnDenied observes each callback that refuses a login attempt, before the error response is written. A provider that cannot be discovered refuses no attempt and is not reported. OnDenied func(r *http.Request, f *Failure) // Render, when set, writes the response for each Failure: the callback's refusals, its discovery failure and a return target Start refuses. Unset, the response is f.Message as plain text with f.Status. Render func(w http.ResponseWriter, r *http.Request, f *Failure) // contains filtered or unexported fields }
RelyingParty serves Start and Callback for one product user type U. Config, Provision and Login are required.
func (*RelyingParty[U]) Callback ¶
func (rp *RelyingParty[U]) Callback(w http.ResponseWriter, r *http.Request)
Callback completes the login the provider redirected back: it checks state, exchanges the code, reads the claims, then runs Provision and Login and redirects to the return target Start accepted, checked again, or PostLoginURL without one.
func (*RelyingParty[U]) Start ¶
func (rp *RelyingParty[U]) Start(w http.ResponseWriter, r *http.Request)
Start redirects the browser to the provider's authorization endpoint and sets the flow cookie.