Sign in

Class: MonoCloudBackendNodeClient

Backend client for validating access tokens in Node.js server applications.

Extends the core OIDC backend client with caching support and automatic detection of JWT vs. opaque token formats.

clockSkew

protected clockSkew: number = 0

Number of seconds to adjust the current time to account for clock differences between the client and server during time-based claim validation. Defaults to 0.

clockTolerance

protected clockTolerance: number = 300

Additional time tolerance in seconds applied when validating time-based claims (exp, nbf). Defaults to 300 (5 minutes).

Constructor

new MonoCloudBackendNodeClient(options?: Partial<MonoCloudBackendNodeClientOptions>): MonoCloudBackendNodeClient

Creates a new instance of MonoCloudBackendNodeClient.

Parameters

ParameterTypeDescription
options?Partial<MonoCloudBackendNodeClientOptions>Client configuration options. When omitted, configuration is read from environment variables.

decodeJwt()

static decodeJwt(jwt: string): JwtClaims

Decodes the payload of a JSON Web Token (JWT) and returns it as an object.

Note: THIS METHOD DOES NOT VERIFY JWT TOKENS.

Parameters

ParameterTypeDescription
jwtstringJWT to decode.

Returns

JwtClaims

Decoded payload.

Throws

MonoCloudTokenError - If decoding fails

fetcher()?

protected optional fetcher: {(input: URL | RequestInfo, init?: RequestInit): Promise<Response>; (input: string | URL | Request, init?: RequestInit): Promise<Response>; }

Custom fetch implementation used for making HTTP requests. Falls back to the global fetch if not provided.

Call Signature

(input: URL | RequestInfo, init?: RequestInit): Promise<Response>

MDN Reference

Parameters

ParameterType
inputURL | RequestInfo
init?RequestInit

Returns

Promise<Response>

Call Signature

(input: string | URL | Request, init?: RequestInit): Promise<Response>

MDN Reference

Parameters

ParameterType
inputstring | URL | Request
init?RequestInit

Returns

Promise<Response>

getJwks()

getJwks(forceRefresh: boolean): Promise<Jwks>

Fetches the JSON Web Keys used to sign the ID token. The JWKS is cached for 5 minutes by default.

Parameters

ParameterTypeDescription
forceRefreshbooleanIf true, bypasses the cache and fetches fresh set of JWKS from the server.

Returns

Promise<Jwks>

The JSON Web Key Set containing the public keys for token verification.

Throws

MonoCloudHttpError - Thrown if there is a network error during the request or unexpected status code during the request or a serialization error while processing the response.

getMetadata()

getMetadata(forceRefresh: boolean): Promise<IssuerMetadata>

Fetches the authorization server metadata from the .well-known endpoint. The metadata is cached for 5 minutes by default.

Parameters

ParameterTypeDescription
forceRefreshbooleanIf true, bypasses the cache and fetches fresh metadata from the server.

Returns

Promise<IssuerMetadata>

The issuer metadata for the tenant, retrieved from the OpenID Connect discovery endpoint.

Throws

MonoCloudHttpError - Thrown if there is a network error during the request or unexpected status code during the request or a serialization error while processing the response.

introspectAccessToken()

introspectAccessToken(accessToken: string, options?: IntrospectOptions): Promise<AccessTokenClaims>

Validates an opaque access token using the OAuth 2.0 Token Introspection endpoint (RFC 7662).

Parameters

ParameterTypeDescription
accessTokenstringThe access token string to introspect.
options?IntrospectOptionsClaims validation options.

Returns

Promise<AccessTokenClaims>

Validated access token claims (without the active field).

Throws

MonoCloudTokenError - If the token is not active or claim validation fails.

Throws

MonoCloudOPError - When the introspection endpoint returns a standardized OAuth 2.0 error response.

Throws

MonoCloudHttpError - Thrown if there is a network error during the request or unexpected status code during the request or a serialization error while processing the response.

Throws

MonoCloudValidationError - When the access token is empty or the introspection endpoint is not available in the issuer metadata or claims validation fails.

jwks?

protected optional jwks: Jwks

Cached JSON Web Key Set retrieved from the issuer's JWKS endpoint.

jwksCacheDuration

protected jwksCacheDuration: number = 300

Duration (in seconds) for which the JWKS is cached. Defaults to 300 (5 minutes).

jwksCacheExpiry

protected jwksCacheExpiry: number = 0

Timestamp (in seconds) when the cached JWKS expires.

metadata?

protected optional metadata: IssuerMetadata

Cached issuer metadata retrieved from the OpenID Connect discovery endpoint.

metadataCacheDuration

protected metadataCacheDuration: number = 300

Duration (in seconds) for which the metadata is cached. Defaults to 300 (5 minutes).

metadataCacheExpiry

protected metadataCacheExpiry: number = 0

Timestamp (in seconds) when the cached metadata expires.

setClockSkew()

setClockSkew(clockSkew: number): void

Sets clock skew used for access token time-based claim validation.

Parameters

ParameterTypeDescription
clockSkewnumberNumber of seconds to adjust the current time to account for clock differences.

Returns

void

setClockTolerance()

setClockTolerance(clockTolerance: number): void

Sets clock tolerance used for access token time-based claim validation.

Parameters

ParameterTypeDescription
clockTolerancenumberAdditional time tolerance in seconds for time-based claim validation.

Returns

void

tenantDomain

protected readonly tenantDomain: string

The normalized tenant domain URL used as the base for discovery endpoints.

validateAccessToken()

validateAccessToken(accessToken: string, options?: ValidateAccessTokenOptions): Promise<AccessTokenClaims>

Validates an access token by automatically detecting its format.

Parameters

ParameterTypeDescription
accessTokenstringThe access token string to validate.
options?ValidateAccessTokenOptionsValidation options.

Returns

Promise<AccessTokenClaims>

Validated access token claims.

Throws

MonoCloudValidationError - When the access token is empty.

Throws

MonoCloudTokenError - If token validation fails.

Throws

MonoCloudOPError - When the introspection endpoint returns a standardized OAuth 2.0 error response.

Throws

MonoCloudHttpError - Thrown if there is a network error during the request or unexpected status code during the request or a serialization error while processing the response.


validateJwtAccessToken()

validateJwtAccessToken(accessToken: string, options?: ValidateJwtAccessTokenOptions): Promise<AccessTokenClaims>

Validates a JWT access token by verifying the signature and claims.

Parameters

ParameterTypeDescription
accessTokenstringThe access token JWT string to validate.
options?ValidateJwtAccessTokenOptionsValidation options.

Returns

Promise<AccessTokenClaims>

Validated access token claims.

Throws

MonoCloudTokenError - If JWT parsing, signature verification, or claim validation fails.

Throws

MonoCloudHttpError - Thrown if there is a network error during the request or unexpected status code during the request or a serialization error while processing the response.

Throws

MonoCloudValidationError - When the access token is empty or claims validation fails.

© 2024 MonoCloud. All rights reserved.