OAuth2 Provider

Act as an OAuth2 + OpenID Connect provider - Authorization Code + PKCE, refresh tokens, and ID tokens.

thunder-core can act as an OAuth2 + OIDC provider (Authorization Code + PKCE, refresh tokens, ID tokens). The core logic is the OAuth2 class in lib/oauth2.ts.

Key Methods

Read lib/oauth2.ts for the full method set. The important ones:

MethodPurpose
resolveJWKsLazily generates/persists an RS256 keypair in the jwks collection.
createOAuthConfigBuilds the discovery metadata.
validateOAuthParams / validateConfidentialClientValidate incoming authorization requests and confidential clients.
signJWT / verifyJWTHS256, SECRET_KEY-based - for access/refresh/authorization codes.
signIdTokenRS256, JWKS-based ID tokens.
generateAuthorizationCode / exchangeToken / revoke / verifyAccessTokenThe token lifecycle (S256 PKCE only).

Concepts Not Obvious From Method Names

  • Consent versioning: each consent has a version. exchangeToken increments it, which revokes previously issued tokens carrying an older version.
  • Refresh tokens are only issued when the consent includes offline_access.
  • ID tokens are only issued when the consent includes openid.

Routes

Read each file for exact request/response shapes:

Route fileResponsibility
routes/.well-known.tsOAuth2 / OpenID discovery metadata.
routes/oauth.tsauthorize, consent, token, revoke, and JWKS endpoints.
routes/oauthClients.tsCRUD over registered OAuth clients.

The client secret is hidden in responses. Confidential clients authenticate via HTTP Basic at the token/revoke endpoints. See schemas/oauthClient.ts and schemas/oauthConsent.ts for the stored fields.

resourceGrantSchema (exported from schemas/oauthConsent.ts) maps a tenant id to granted scopes, and is reused by both OAuth consents and API keys to scope access per tenant.


On this page