http.oidc

index · http

Overview

An OpenID Connect relying party for the authorization code flow with PKCE S256: discovery, the authorization URL, the code-for-token exchange, ID token verification through jose.jwt against a cached JWKS, and userinfo. The client is confidential and authenticates at the token endpoint with client_secret_basic. The ID token allowlist is EdDSA unless the config says otherwise.

Every method that reaches the IdP runs inside lev.run(), the constraint http.request carries. No redirect is followed from any IdP endpoint. The discovery document is cached for the process lifetime; the JWKS is refetched on unknown kid, at most once a minute.

Functions

NameSignature
pkce_verifierpkce_verifier() -> verifier
pkce_challengepkce_challenge(verifier) -> challenge
discoverdiscover(self) -> doc, err
auth_urlauth_url(self, state, nonce, verifier) -> url, err
exchangeexchange(self, code, verifier) -> tokens, err
verify_id_tokenverify_id_token(self, id_token, nonce) -> claims, err
userinfouserinfo(self, access_token, sub) -> claims, err
newnew(cfg) -> client, err

pkce_verifier() -> verifier

Make a PKCE code verifier

base64url of 32 bytes from the kernel CSPRNG: 43 characters of the unreserved set, inside RFC 7636's 43 to 128. The same string serves as a state or a nonce; there is no second generator.

pkce_challenge(verifier) -> challenge

Derive the S256 code challenge of a verifier

base64url of SHA-256 over the verifier's bytes (RFC 7636 §4.2). Raises on a verifier that is not a string.

discover(self) -> doc, err

Fetch the provider's discovery document

GET <issuer>/.well-known/openid-configuration. The document must be a JSON object whose issuer equals the configured one byte for byte (OIDC Discovery §4.3), with string authorization_endpoint, token_endpoint and jwks_uri; userinfo_endpoint is a string when present. The first good document is kept for the process lifetime; a failed fetch keeps nothing.

auth_url(self, state, nonce, verifier) -> url, err

Build the URL the user agent is sent to

The authorization endpoint with response_type=code, the client id, the redirect URI, the scopes joined by a space, state, nonce, the S256 challenge of verifier and code_challenge_method=S256, in that order, joined to a query the endpoint already carries with &. The caller keeps state, nonce and verifier in its transaction state until the callback. Runs discover.

exchange(self, code, verifier) -> tokens, err

Exchange an authorization code for the tokens

POST at the token endpoint with grant_type=authorization_code, the code, the redirect URI and the verifier, under client_secret_basic (RFC 6749 §2.3.1): the id and the secret form-escaped, joined by a colon and base64-encoded. The response must be a JSON object with string access_token and id_token and a token_type of Bearer in any case; it comes back whole, expires_in and scope included. An OAuth error reads token: <error>: <error_description>. The ID token is not verified here: verify_id_token takes it with the nonce.

verify_id_token(self, id_token, nonce) -> claims, err

Verify an ID token and return its claims

jose.jwt.verify against the cached JWKS with the configured allowlist, the issuer as iss, the client id as aud, exp, iat and sub required and the configured leeway. On the exact error unknown kid the JWKS is fetched again and the token tried once more, unless the last fetch is younger than a minute: then the error stands. After the signature: nonce must equal the token's, and azp, when present, must be the client id (OIDC Core §3.1.3.7). Runs discover.

userinfo(self, access_token, sub) -> claims, err

Fetch the userinfo claims of an access token

GET at the userinfo endpoint with the token as a Bearer credential. The body must be a JSON object with a string sub; given sub, the ID token's, the two must match (OIDC Core §5.3.2). Refused when the discovery document names no userinfo endpoint. Runs discover.

new(cfg) -> client, err

Create a relying-party client

keyrequiredmeaning
issueryeshttps, or http on a loopback host; no query, no fragment
client_idyesthe registered id
client_secretyesthe registered secret; the client is confidential
redirect_uriyesthe callback the IdP sends the code to
scopesnolist, default { "openid" }; openid is added when absent
algsnothe ID token allowlist, default { "EdDSA" }
leewaynoseconds of slack on the time claims, default 30
timeoutnoseconds per request at the IdP, default 5
httpnohttp.request options every call carries: the TLS ones
clocknofunction() -> seconds, default os.time

The config is data and a bad one returns nil, err. http on a host that is not loopback is refused: the issuer is the trust anchor of every check that follows.