Table of Contents

If you or your business use openid-client, please consider becoming a sponsor so I can continue maintaining it and adding new features carefree.
Encapsulates a discovered or instantiated OpenID Connect Issuer (Issuer), Identity Provider (IdP), Authorization Server (AS) and its metadata.
import { Issuer } from 'openid-client';Creates a new Issuer with the provided metadata
metadata:<Object>issuer:<string>Issuer identifierauthorization_endpoint:<string>token_endpoint:<string>jwks_uri:<string>userinfo_endpoint:<string>revocation_endpoint:<string>introspection_endpoint:<string>end_session_endpoint:<string>registration_endpoint:<string>token_endpoint_auth_methods_supported:<string>token_endpoint_auth_signing_alg_values_supported:<string>introspection_endpoint_auth_methods_supported:<string>introspection_endpoint_auth_signing_alg_values_supported:<string>revocation_endpoint_auth_methods_supported:<string>revocation_endpoint_auth_signing_alg_values_supported:<string>request_object_signing_alg_values_supported:<string>mtls_endpoint_aliases:<Object>token_endpoint:<string>userinfo_endpoint:<string>revocation_endpoint:<string>introspection_endpoint:<string>
- other metadata may be present but currently doesn't have any special handling
- Returns:
<Issuer>
Returns the <Client> class tied to this issuer.
- Returns:
<Client>
Returns the <FAPI1Client> class tied to this issuer. <FAPI1Client> inherits from <Client> and
adds necessary Financial-grade API Security Profile 1.0 - Part 2: Advanced behaviours:
- Returns:
<FAPI1Client>
The behaviours are:
s_hashpresence and value checks in authorization endpoint response ID Tokens- authorization endpoint response ID Tokens
iatmust not be too far in the past (fixed to be 1 hour) - Request Objects include
nbf(with the same value asiat)
Returns metadata from the issuer's discovery document.
- Returns:
<Object>
Loads OpenID Connect 1.0 and/or OAuth 2.0 Authorization Server Metadata documents. When the
issuer argument contains '.well-known' only that document is loaded, otherwise performs both
openid-configuration and oauth-authorization-server requests.
This is the recommended method of getting yourself an Issuer instance.
issuer:<string>Issuer Identifier or metadata URL- Returns:
Promise<Issuer>
Performs OpenID Provider Issuer Discovery based on End-User input.
input:<string>EMAIL, URL, Hostname and Port, acct or syntax input- Returns:
Promise<Issuer>
- Class: <Client>
- new Client(metadata[, jwks[, options]])
- client.authorizationUrl(parameters)
- client.callback(redirectUri, parameters[, checks[, extras]])
- client.callbackParams(input)
- client.deviceAuthorization(parameters[, extras])
- client.endSessionUrl(parameters)
- client.grant(body[, extras])
- client.introspect(token[, tokenTypeHint[, extras]])
- client.metadata
- client.refresh(refreshToken[, extras])
- client.requestObject(payload)
- client.requestResource(resourceUrl, accessToken, [, options])
- client.revoke(token[, tokenTypeHint[, extras]])
- client.userinfo(accessToken[, options])
- client.pushedAuthorizationRequest(parameters[, extras])
- Client Authentication Methods
- Client.fromUri(registrationClientUri, registrationAccessToken[, jwks[, clientOptions]])
- Client.register(metadata[, other])
Encapsulates a dynamically registered, discovered or instantiated OpenID Connect Client (Client), Relying Party (RP), and its metadata, its instances hold the methods for getting an authorization URL, consuming callbacks, triggering token endpoint grants, revoking and introspecting tokens.
import { Issuer } from 'openid-client';
const issuer = await Issuer.discover('https://accounts.google.com');
const { Client } = issuer;Creates a new Client with the provided metadata
metadata:<Object>client_id:<string>client_secret:<string>id_token_signed_response_alg:<string>Default: 'RS256'id_token_encrypted_response_alg:<string>id_token_encrypted_response_enc:<string>userinfo_signed_response_alg:<string>userinfo_encrypted_response_alg:<string>userinfo_encrypted_response_enc:<string>redirect_uris:string[]response_types:string[]Default: '["code"]'post_logout_redirect_uris:string[]default_max_age:<number>require_auth_time:<boolean>Default: 'false'request_object_signing_alg:<string>request_object_encryption_alg:<string>request_object_encryption_enc:<string>token_endpoint_auth_method:<string>Default: 'client_secret_basic'introspection_endpoint_auth_method:<string>Default: same as token_endpoint_auth_methodrevocation_endpoint_auth_method:<string>Default: same as token_endpoint_auth_methodtoken_endpoint_auth_signing_alg:<string>introspection_endpoint_auth_signing_alg:<string>revocation_endpoint_auth_signing_alg:<string>tls_client_certificate_bound_access_tokens:<boolean>- other metadata may be present but currently doesn't have any special handling
jwks:<Object>JWK Set formatted object with private keys used for signing client assertions or decrypting responses.options:<Object>additional options for the clientadditionalAuthorizedParties:<string>|string[]additional accepted values for the Authorized Party (azp) claim. Default: only the client's client_id value is accepted.
- Returns:
<Client>
Returns the client's metadata.
- Returns:
<Object>
Returns the target authorization redirect URI to redirect End-Users to using the provided parameters.
parameters:<Object>redirect_uri:<string>Default: If only a singleclient.redirect_urismember is present that one will be used automatically.response_type:<string>Default: If only a singleclient.response_typesmember is present that one will be used automatically.scope:<string>Default: 'openid'- any other authorization parameters may be provided (e.g.
nonce,state,login_hint, ...)
- Returns:
<string>
Returns the target logout redirect URI to redirect End-Users to using the provided parameters.
parameters:<Object>id_token_hint:<string>|<TokenSet>client_id:<string>Default: client's client_idpost_logout_redirect_uri:<string>Default: If only a singleclient.post_logout_redirect_urismember is present that one will be used automatically.state:<string>logout_hint:<string>- any other end session parameters may be provided
- Returns:
<string>
Returns recognized callback parameters from a provided input.
input:<string>|<http.IncomingMessage>|<http2.Http2ServerRequest>- When input is of type string it will be parsed using
url.parseand its query component will be returned - When input is a GET http/http2 request object its
urlproperty will be parsed usingurl.parseand its query component will be returned - When input is a POST http/http2 request object its
bodyproperty will be parsed or returned if it is already an object. Note: the request read stream will not be parsed, it is expected that you will have a body parser prior to calling this method. This parser would set thereq.bodyproperty
- When input is of type string it will be parsed using
- Returns:
<Object>
Performs the callback for Authorization Server's authorization response.
-
redirectUri:<string>redirect_uri used for the authorization request -
parameters:<Object>returned authorization response, seeclient.callbackParamsif you need help getting them. -
checks:<Object>response_type:<string>When provided the authorization response will be checked for presence of required parameters for a given response_type. Use of this check is recommended.state:<string>When provided the authorization response's state parameter will be checked to be the this expected one. Use of this check is required if you sent a state parameter into an authorization request.jarm:<boolean>When provided the authorization response must be a JARM one.nonce:<string>When provided the authorization response's ID Token nonce parameter will be checked to be the this expected one. Use of this check is required if you sent a nonce parameter into an authorization request.code_verifier:<string>PKCE code_verifier to be sent to the token endpoint during code exchange. Use of this check is required if you sent a code_challenge parameter into an authorization request.max_age:<number>When provided the authorization response's ID Token auth_time parameter will be checked to be conform to the max_age value. Use of this check is required if you sent a max_age parameter into an authorization request. Default: uses client'sdefault_max_age.
-
extras:<Object>exchangeBody:<Object>extra request body properties to be sent to the AS during code exchange.clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Token Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
-
Returns:
Promise<TokenSet>Parsed token endpoint response as a TokenSet.
Tip: If you're using pure
OAuth 2.0 then client.oauthCallback(redirectUri, parameters[, checks[, extras]]) is the OAuth 2.0
variant of this method, it has the same signature with the exception of checks only supporting
code_verifier, state, response_type and jarm.
Performs refresh_token grant type exchange.
refreshToken:<string>|<TokenSet>Refresh Token value. When TokenSet instance is provided itsrefresh_tokenproperty will be used automatically.extras:<Object>exchangeBody:<Object>extra request body properties to be sent to the AS during refresh token exchange.clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Token Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
- Returns:
Promise<TokenSet>Parsed token endpoint response as a TokenSet.
Fetches the OIDC userinfo response with the provided Access Token. Also handles signed and/or
encrypted userinfo responses. When TokenSet is provided as an argument the userinfo sub property
will also be checked to match the on in the TokenSet's ID Token.
accessToken:<string>|<TokenSet>Access Token value. When TokenSet instance is provided itsaccess_tokenproperty will be used automatically.options:<Object>method:<string>The HTTP method to use for the request 'GET' or 'POST'. Default: 'GET'via:<string>The mechanism to use to attach the Access Token to the request. Valid values areheaderorbody. Default: 'header'.tokenType:<string>The token type as the Authorization Header scheme. Default: 'Bearer' or thetoken_typeproperty from a passed in TokenSet.params:<Object>additional parameters to send with the userinfo request (as query string when GET, as x-www-form-urlencoded body when POST).DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Userinfo Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
- Returns:
Promise<Object>Parsed userinfo response.
Fetches an arbitrary resource with the provided Access Token in an Authorization header.
resourceUrl:<URL>|<string>Resource URL to request a response from.accessToken:<string>|<TokenSet>Access Token value. When TokenSet instance is provided itsaccess_tokenproperty will be used automatically.options:<Object>headers:<Object>HTTP Headers to include in the request.body:<string>|<Buffer>HTTP Body to include in the request.method:<string>The HTTP method to use for the request. Default: 'GET'tokenType:<string>The token type as the Authorization Header scheme. Default: 'Bearer' or thetoken_typeproperty from a passed in TokenSet.DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Userinfo Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
- Returns:
Promise<Response>Response is a Got Response with thebodyproperty being a<Buffer>
Performs an arbitrary grant_type exchange at the token_endpoint.
body:<Object>grant_type:<string>- other properties may be provided depending on the grant in question
extras:<Object>clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Token Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
- Returns:
Promise<TokenSet>
Introspects a token at the Authorization Server's introspection_endpoint.
token:<string>tokenTypeHint:<string>extras:<Object>introspectBody:<Object>extra request body properties to be sent to the introspection endpoint.clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.
- Returns:
Promise<Object>Parsed introspection response.
Revokes a token at the Authorization Server's revocation_endpoint.
token:<string>tokenTypeHint:<string>extras:<Object>revokeBody:<Object>extra request body properties to be sent to the revocation endpoint.clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.
- Returns:
Promise<undefined>Revocation responses are not parsed as per the specification.
Creates a signed and optionally encrypted Request Object to send to the AS. Uses the client's
request_object_signing_alg, request_object_encryption_alg, request_object_encryption_enc
metadata for determining the algorithms to use.
payload:<Object>Authorization request parameters and any other JWT parameters to be included in the Request Object.client_id:<string>Default: client's client_idiss:<string>Default: client's client_idaud:<string>Default: issuer's Issuer Identifieriat:<number>Default: now()exp:<number>Default: now() + 300 (5 minutes from now)jti:<string>Default: 32 random base64url encoded bytes- any other authorization request parameters may be included
- any other JWT parameters like
nbfmay also be included - any custom request object payload properties may also be included
- Returns:
Promise<string>
RFC8628 - OAuth 2.0 Device Authorization Grant (Device Flow)
Starts a Device Authorization Request at the issuer's device_authorization_endpoint and returns
a handle for subsequent Device Access Token Request polling.
parameters:<Object>client_id:<string>Default: client's client_idscope:<string>Default: 'openid'- any Device Authorization Request parameters may also be included
extras:<Object>exchangeBody:<Object>extra request body properties to be sent to the AS during the Device Access Token RequestclientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.DPoP:<KeyObject>or<CryptoKey>When provided the client will send a DPoP Proof JWT to the Token Endpoint. The DPoP Proof JWT's algorithm is determined1 automatically based on the type of key and the issuer metadata.
- Returns:
Promise<DeviceFlowHandle>
OAuth 2.0 Pushed Authorization Requests (PAR) - draft 06
Performs a Pushed Authorization Request at the issuer's pushed_authorization_request_endpoint
with the provided parameters. The resolved object contains a request_uri that you will
afterwards pass to client.authorizationUrl(parameters) as the request_uri parameter.
The parameters sent to pushed_authorization_request_endpoint default to the same values
as client.authorizationUrl(parameters) unless
request (a Request Object) parameter e.g. from client.requestObject(payload) is present.
The client will use it's token_endpoint_auth_method to authenticate at the pushed_authorization_request_endpoint.
parameters:<Object>client_id:<string>Default: client's client_id- any other request parameters may also be included
extras:<Object>clientAssertionPayload:<Object>extra client assertion payload parameters to be sent as part of a client JWT assertion. This is only used when the client'stoken_endpoint_auth_methodis eitherclient_secret_jwtorprivate_key_jwt.
- Returns:
Promise<Object>Parsed Pushed Authorization Request Response withrequest_uriandexpires_inproperties validated to be present and correct types.
Defined in Core 1.0 and RFC 8705
the following are valid values for token_endpoint_auth_method.
none- only client_id is sent in the request bodyclient_secret_basic(default) - client_id and client_secret is sent using theAuthorizationheader as described in RFC6749client_secret_post- client_id and client_secret is sent in the request body as described in RFC6749client_secret_jwt- usingclient_secretas a shared symmetric secret aclient_assertionis sent in the request bodyprivate_key_jwt- using the asymmetric keys provided viajwksaclient_assertionis sent in the request bodytls_client_authandself_signed_tls_client_auth- sends client_id in the request body combined with client certificate and key configured via settingcertandkeyon a per-request basis usingdocs#customizing-http-requests
Note: *_jwt methods resolve their signature algorithm either via the client's configured alg
(token_endpoint_auth_signing_alg) or any of the issuer's supported algs
(token_endpoint_auth_signing_alg_values_supported).
Performs Dynamic Client Registration with the provided metadata at the issuer's
registration_endpoint.
metadata:<Object>Client Metadata to register the new client with.other:<Object>jwks:<Object>JWK Set formatted object with private keys used for signing client assertions or decrypting responses. When neitherjwks_uriorjwksis present inmetadatathe key's public parts will be registered asjwks.initialAccessToken:<string>Initial Access Token to use as a Bearer token during the registration call.additionalAuthorizedParties:<string>|string[]additional accepted values for the Authorized Party (azp) claim. Default: only the client's client_id value is accepted.
Performs Dynamic Client Read Request to retrieve a Client instance.
registrationClientUri:<string>Location of the Client Configuration EndpointregistrationAccessToken:<string>Registration Access Token to use as a Bearer token during the Client Read Requestjwks:<Object>JWK Set formatted object with private keys used for signing client assertions or decrypting responses.clientOptions:<Object>additional options passed to theClientconstructoradditionalAuthorizedParties:<string>|string[]additional accepted values for the Authorized Party (azp) claim. Default: only the client's client_id value is accepted.
The following are default http request options that openid-client sets for all requests.
const DEFAULT_HTTP_OPTIONS = {
headers: {
'User-Agent': `${pkg.name}/${pkg.version} (${pkg.homepage})`,
'Accept-Encoding': 'identity',
},
timeout: 3500,
};You may change these global options like so:
import { custom } from 'openid-client';
custom.setHttpOptionsDefaults({
timeout: 5000,
});This is meant to change global request options such as timeout or the default User-Agent header.
You change options on a per-request basis by assigning a function to
Issuerconstructor to override the following request's options- discovery
- webfinger
Issuerinstance to override fetching issuer's jwks_uriissuer.Clientconstructor to override the following request's options- dynamic client registration through Client Registration Endpoint
- discovering client info through Client Read Request
issuer.Clientinstance to override the following request's options- userinfo requests
- token endpoint requests
- introspection endpoint requests
- revocation endpoint requests
This function will then be called before executing each and every request on the instance or constructor.
import { custom } from 'openid-client';
// you can also set this on Issuer constructor, Issuer instance, or Client constructor
client[custom.http_options] = (url, options) => {
// console.log(url);
// console.log(options);
return { timeout: 5000 };
}The following options can be provided agent, ca, cert, crl, headers, key, lookup, passphrase,
pfx, timeout. These are all relayed to https://nodejs.org/api/https.html#httpsrequesturl-options-callback
Example (Click to expand) providing mutual-TLS client certificate and key
import { custom } from 'openid-client';
client[custom.http_options] = function (url, options) {
// https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
const result = {};
result.cert = cert; // <string> | <string[]> | <Buffer> | <Buffer[]>
result.key = key; // <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]>
// custom CA
// result.ca = ca; // <string> | <string[]> | <Buffer> | <Buffer[]>
// use with .p12/.pfx files
// result.pfx = pfx; // <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]>
// result.passphrase = passphrase; // <string>
// use HTTP(S)_PROXY
// https://nodejs.org/api/http.html#httprequesturl-options-callback
// e.g. using https://www.npmjs.com/package/proxy-agent
// result.agent = agent;
return result;
}It is possible the RP or OP environment has a system clock skew, which can result in the error "JWT not active yet". To set a clock tolerance (in seconds)
import { custom } from 'openid-client';
client[custom.clock_tolerance] = 5; // to allow a 5 second skewRepresents a set of tokens retrieved from either authorization callback or successful token endpoint grant call.
Creates a new TokenSet from the provided response. E.g. parsed token endpoint response, parsed callback parameters. You only need to instantiate a TokenSet yourself if you recall it from e.g. distributed cache storage or a database. Note: manually constructed TokenSet instances do not undergo any validations.
input:<Object>access_token:<string>token_type:<string>id_token:<string>refresh_token:<string>expires_in:<number>expires_at:<number>Access token expiration timestamp, represented as the number of seconds since the epoch (January 1, 1970 00:00:00 UTC).session_state:<string>- other properties may be present and they'll be passthrough available on the TokenSet instance
- Returns:
<TokenSet>
Given that the instance has expires_at / expires_in this function returns true / false when the access token (which expires properties are for) is beyond its lifetime.
- Returns:
<boolean>
Given that the instance has an id_token this function returns its parsed payload object. Does not perform any validations as these were done prior to openid-client returning the tokenset in the first place.
- Returns:
<Object>
The handle represents a Device Authorization Response with the verification_uri, user_code and
other defined response properties. A handle is instantiated by calling
client.deviceAuthorization()
This will continuously poll the token_endpoint and resolve with a TokenSet once one is received.
This will handle the defined authorization_pending and slow_down "soft" errors and continue
polling but upon any other error it will reject.
options:<Object>signal:<AbortSignal>An optional AbortSignal that can be used to abort polling. When if the signal is aborted the next interval in the poll will make the returned promise reject.
- Returns:
Promise<TokenSet>
This will abort ongoing polling. The next interval in the poll will result in a rejection.
Returns the user_code Device Authorization Response parameter.
- Returns:
<string>
Returns the verification_uri Device Authorization Response parameter.
- Returns:
<string>
Returns the verification_uri_complete Device Authorization Response parameter.
- Returns:
<string>
Returns true/false depending on whether the handle is expired or not.
- Returns:
<boolean>
Returns the number of seconds until the handle expires.
- Returns:
<number>
Returns the device_code Device Authorization Response parameter.
- Returns:
<string>
Generic OpenID Connect Passport authentication middleware strategy.
import { Strategy } from 'openid-client';Creates a new Strategy
options:<Object>client:<Client>Client instance. The strategy will use it.params:<Object>Authorization Request parameters. The strategy will use these for every authorization request.passReqToCallback:<boolean>Boolean specifying whether the verify function should get the request object as first argument instead. Default: 'false'usePKCE:<boolean>|<string>The PKCE method to use. When 'true' it will resolve based on the issuer metadata, when 'false' no PKCE will be used. Default: 'true'sessionKey:<string>Define the property in your session which is used for storing information for the purpose of consuming the authentication response. Default: 'oidc:${Issuer Identifier}'
verify:<Function>Your regular Passport Verify Callback function in which you verify the user from based on the data received from the AS.tokenset:<TokenSet>Successful callback result TokenSet[userinfo]:<Object>Optional argument, omit it when you don't want to load userinfo and are fine using 'tokenset.claims()' alone.done:<Function>
- Returns:
<Strategy>
Note: You can also set authorization request parameters dynamically using the options argument in passport.authenticate([options]):
app.get('/protected-route', function(req, res, next) {
if (shouldReConsent(req)) {
passport.authenticate('oidc', { prompt: 'consent' })(req, res, next);
}
});- generators.random([bytes])
- generators.state([bytes])
- generators.nonce([bytes])
- generators.codeVerifier([bytes])
- generators.codeChallenge(codeVerifier)
Generates random bytes and encodes them in url safe base64. This method is also aliased as
generators.nonce, generators.state and generators.codeVerifier
bytes:<number>Number indicating the number of bytes to generate. Default: 32- Returns:
<string>
Calculates the S256 PKCE code challenge for an arbitrary code verifier.
verifier:<string>Code verifier to calculate the S256 code challenge for.- Returns:
<string>
The following errors are expected to be thrown by openid-client runtime and have their prototypes exported.
import { errors } from 'openid-client';
// { OPError: [Function: OPError],
// RPError: [Function: RPError] }Thrown when unexpected argument types or their format is encountered. This is the standard built-in
TypeError.
Error class thrown when client-side response expectations/validations fail to pass. Depending on the
context it may or may not have additional context-based properties like checks, jwt, params or
body.
Error class thrown when a regular OAuth 2.0 / OIDC style error is returned by the AS or an unexpected response is sent by the OP.
The 'error' parameter from the AS response.
- Returns:
<string>|<undefined>
The 'error_description' parameter from the AS response.
- Returns:
<string>|<undefined>
The 'error_uri' parameter from the AS response.
- Returns:
<string>|<undefined>
The 'state' parameter from the AS response.
- Returns:
<string>|<undefined>
The 'scope' parameter from the AS response.
- Returns:
<string>|<undefined>
When the error is related to an http(s) request made to the OP this property will hold the pure node request instance.
- Returns:
<http.IncomingMessage>|<undefined>