Documentation
¶
Overview ¶
Package oauth2 implements the plain OAuth 2.0 authorization code flow with PKCE (S256) as an authentication.FederatedProvider, for providers that do not issue OpenID Connect ID tokens.
State handling matches package oidc: the raw state is never stored, the pending state is consumed atomically before the code exchange, and it expires after Options.StateTTL. Because plain OAuth 2.0 has no verifiable identity token, the required UserInfoMapper is the only identity source: it receives the access token and must establish the principal, for example by calling the provider's user-info API.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// Name identifies the provider (lower-cased and trimmed). Required.
Name string
// Config holds the client credentials and endpoints. ClientID, AuthURL
// and TokenURL are required; endpoints must use https, or http on a
// loopback host.
Config xoauth2.Config
// RedirectURIs lists the exact redirect URIs Begin accepts (https, or
// http on a loopback host). Required, without duplicates.
RedirectURIs []string
// State persists pending callback state. Required.
State authoidc.StateStore
// Mapper establishes the principal from the access token. Required.
Mapper UserInfoMapper
// Clock returns the current time used for state expiry and principal
// validation; its result is converted to UTC. Defaults to time.Now.
Clock func() time.Time
// Random supplies state and PKCE entropy. Defaults to
// crypto/rand.Reader; any replacement must be cryptographically secure.
Random io.Reader
// StateTTL bounds the time between Begin and Complete. Defaults to ten
// minutes.
StateTTL time.Duration
}
Options configures a Provider.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is an OAuth 2.0 authorization code flow provider. It is safe for concurrent use when its StateStore and Mapper are.
func New ¶
New validates options and returns a Provider. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.
func (*Provider) Begin ¶
func (p *Provider) Begin(ctx context.Context, request authentication.BeginRequest) (authentication.Redirect, error)
Begin starts a login for a registered redirect URI and returns the authorization URL. Scopes are requested as given; "openid" is not added.
func (*Provider) Complete ¶
func (p *Provider) Complete(ctx context.Context, callback authentication.Callback) (authentication.Principal, error)
Complete consumes the callback state, exchanges the code and maps the access token to a principal. It returns authentication.ErrReplay for unknown or reused state, authentication.ErrExpired after Options.StateTTL, and authentication.ErrUnauthenticated for every other failure.
type UserInfoMapper ¶
UserInfoMapper derives the principal from an access token. Its result is validated with authentication.ValidatePrincipal.