Documentation
¶
Overview ¶
Package oidclient is an OIDC relying party library for web applications authenticating against an OpenID Connect provider (designed for use with infodancer/webauth but compatible with any spec-compliant IdP).
It wraps github.com/coreos/go-oidc/v3 for discovery, JWKS, and token verification, and golang.org/x/oauth2 for the authorization code flow with PKCE. Cookie helpers and a convenience API are layered on top.
Index ¶
- Constants
- Variables
- func ClearFlowCookies(w http.ResponseWriter)
- func ClearSessionCookie(w http.ResponseWriter, name string, secure bool)
- func FlowCookieValue(r *http.Request, name string) string
- func GenerateNonce() (string, error)
- func GenerateVerifier() string
- func GetOrCreateFlow(w http.ResponseWriter, r *http.Request, returnTo string) (state, verifier string, err error)
- func IsSecure(r *http.Request) bool
- func SetFlowCookie(w http.ResponseWriter, name, value string, secure bool)
- func SetSessionCookie(w http.ResponseWriter, name, token string, secure bool)
- type Claims
- type Client
- func (c *Client) AuthorizeURL(state, verifier string) string
- func (c *Client) ClientID() string
- func (c *Client) CookieName() string
- func (c *Client) ExchangeCode(ctx context.Context, code, verifier string) (accessToken string, claims *Claims, err error)
- func (c *Client) FlowConfigured() bool
- func (c *Client) LoginURL(redirectPath string) string
- func (c *Client) LogoutURL() string
- func (c *Client) RegisterURL(state, verifier string) string
- func (c *Client) Validate(ctx context.Context, tokenStr string) (*Claims, error)
- func (c *Client) ValidateCookie(r *http.Request) (*Claims, error)
- type Config
Constants ¶
const ( // Default cookie names for the OIDC authorization flow. CookieVerifier = "oauth_verifier" CookieState = "oauth_state" CookieRedirect = "oauth_redirect" )
Variables ¶
var ( ErrNoCookie = errors.New("oidclient: missing auth cookie") ErrNoIDToken = errors.New("oidclient: token response missing id_token") ErrMissingSub = errors.New("oidclient: token missing sub claim") )
Sentinel errors returned by Validate, ValidateCookie, and ExchangeCode.
Functions ¶
func ClearFlowCookies ¶
func ClearFlowCookies(w http.ResponseWriter)
ClearFlowCookies expires all OIDC flow cookies.
func ClearSessionCookie ¶
func ClearSessionCookie(w http.ResponseWriter, name string, secure bool)
ClearSessionCookie expires the session cookie.
func FlowCookieValue ¶
FlowCookieValue reads a named cookie value, returning "" if absent.
func GenerateNonce ¶
GenerateNonce returns a random base64url-encoded state nonce for CSRF protection.
func GenerateVerifier ¶
func GenerateVerifier() string
GenerateVerifier returns a random PKCE code verifier (RFC 7636). The verifier should be stored (e.g. in a cookie) and passed to both AuthorizeURL and ExchangeCode.
func GetOrCreateFlow ¶ added in v0.2.0
func GetOrCreateFlow(w http.ResponseWriter, r *http.Request, returnTo string) (state, verifier string, err error)
GetOrCreateFlow returns the state and PKCE verifier for the current authorization flow, generating them and setting the flow cookies when no flow is in progress. An existing flow (both state and verifier cookies present) is reused, so concurrent unauthenticated requests share one flow instead of overwriting the state another in-flight redirect depends on. The redirect cookie is always set to returnTo: state and verifier are per-flow, the post-login destination follows the latest request.
func SetFlowCookie ¶
func SetFlowCookie(w http.ResponseWriter, name, value string, secure bool)
SetFlowCookie writes an HttpOnly cookie used during the OIDC flow. The cookie is session-lifetime: the flow must survive however long the user leaves the IdP's login page open, and the state/verifier nonces derive no security from a fixed expiry -- the authorization code and PKCE binding do.
func SetSessionCookie ¶
func SetSessionCookie(w http.ResponseWriter, name, token string, secure bool)
SetSessionCookie writes the JWT as an HttpOnly session cookie.
Types ¶
type Claims ¶
type Claims struct {
Sub string `json:"sub"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified,omitempty"`
Name string `json:"name"`
Roles []string `json:"roles,omitempty"`
}
Claims holds the JWT claims extracted from an ID or access token.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an OIDC relying party that performs authorization code flows with PKCE and validates JWTs using keys from the provider's JWKS endpoint.
Discovery, JWKS caching, and signature verification are handled by github.com/coreos/go-oidc/v3. The authorization code flow is handled by golang.org/x/oauth2.
func New ¶
New creates a Client by performing OIDC discovery against the configured IssuerURL. Returns an error if discovery fails or the configuration is incomplete.
func (*Client) AuthorizeURL ¶
AuthorizeURL builds the OIDC authorization URL with PKCE. verifier is the PKCE code verifier (store it for the callback); the S256 challenge is computed automatically.
func (*Client) ClientID ¶
ClientID returns the OIDC client ID in effect for this Client. When the provider supports RFC 7591 dynamic registration, this is the server-assigned id (which may differ from Config.ClientID); otherwise it is the pre-registered id from Config.
func (*Client) CookieName ¶
CookieName returns the configured session cookie name.
func (*Client) ExchangeCode ¶
func (c *Client) ExchangeCode(ctx context.Context, code, verifier string) (accessToken string, claims *Claims, err error)
ExchangeCode exchanges an authorization code for tokens using the PKCE verifier. Returns the access token (for session storage) and the verified claims from the ID token.
func (*Client) FlowConfigured ¶
FlowConfigured reports whether the OIDC authorization code flow is configured (i.e., ClientID and CallbackURL are set).
func (*Client) LoginURL ¶
LoginURL returns a URL to redirect unauthenticated users to the IdP login.
func (*Client) RegisterURL ¶
RegisterURL builds the URL to the IdP's registration page with the OIDC parameters needed to complete an authorization code flow after signup. The verifier is the same PKCE code verifier used with ExchangeCode. After registration, the IdP redirects to CallbackURL with an authorization code, identical to the login flow.
type Config ¶
type Config struct {
// IssuerURL is the OIDC issuer (e.g. "https://auth.example.com/t/mytenant").
// Used for discovery and token verification. Required.
IssuerURL string
// CookieName is the name of the HttpOnly cookie storing the JWT session token.
CookieName string
// ClientID is this application's pre-registered OIDC client ID. Used as-is
// when the provider does not advertise a registration_endpoint. When the
// provider supports RFC 7591 dynamic client registration, this field is
// ignored and the server-assigned client_id is used instead — query the
// runtime value via Client.ClientID after New returns.
ClientID string
// ClientName is optional client metadata (RFC 7591 §2) sent during dynamic
// registration. Servers may use it for display in admin UIs and consent
// screens. Ignored when the provider does not advertise a registration_endpoint.
ClientName string
// ClientSecret is the static secret for a confidential client (e.g. a Google
// "Web application" credential). Set it together with a pre-registered
// ClientID for providers that issue a secret and do not support RFC 7591
// dynamic registration; it is sent in the token-exchange request body
// alongside the PKCE verifier. Leave empty for public PKCE clients. When the
// provider advertises a registration_endpoint, dynamic registration runs and
// the server-assigned secret supersedes this value.
ClientSecret string
// CallbackURL is the registered redirect URI for the authorization code flow.
CallbackURL string
// WebauthURL is the base URL of the auth server (e.g. "https://auth.example.com").
// Used for LoginURL/LogoutURL construction. Derived from IssuerURL if empty.
WebauthURL string
// HTTPClient overrides the default HTTP client for OIDC discovery, JWKS
// fetches, and token exchange requests.
HTTPClient *http.Client
}
Config configures the OIDC client.