UNPKG

@mft/moneyhub-api-client

Version:
44 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGetOpenIdConfig = void 0; const ttlcache_1 = require("@isaacs/ttlcache"); const discovery_1 = require("./discovery"); const OIDC_CACHE_KEY = "oidc"; /** * Builds the discovery document URL for the given identity service URL. * @param {string} identityServiceUrl - Identity service URL * @returns {string} Full URL to the OIDC discovery document */ const discoveryUrl = (identityServiceUrl) => identityServiceUrl.replace(/\/oidc\/?$/, "") + "/oidc/.well-known/openid-configuration"; /** * Creates a getOpenIdConfig function that fetches the OIDC discovery document with optional * URL rewriting for gateway use. Uses @isaacs/ttlcache for TTL-based caching when openIdConfigCacheTtlMs > 0. * * @param {GetOpenIdConfigParams} params - Configuration: identityServiceUrl, optional gatewayIdentityServiceUrl, openIdConfigCacheTtlMs, and the request function used to fetch the discovery document * @returns {function(): Promise<Record<string, unknown>>} A function that returns a promise of the discovery document (with endpoint URLs rewritten to gatewayIdentityServiceUrl only when that option is set) */ function createGetOpenIdConfig(params) { const { identityServiceUrl, gatewayIdentityServiceUrl, openIdConfigCacheTtlMs, request } = params; const useCache = openIdConfigCacheTtlMs > 0; const cache = useCache ? new ttlcache_1.TTLCache({ max: 1, ttl: openIdConfigCacheTtlMs }) : undefined; return async function getOpenIdConfig() { if (useCache && cache) { const cached = cache.get(OIDC_CACHE_KEY); if (cached) return cached; } const raw = (await request(discoveryUrl(identityServiceUrl))); const rewriteTarget = gatewayIdentityServiceUrl !== null && gatewayIdentityServiceUrl !== void 0 ? gatewayIdentityServiceUrl : identityServiceUrl; const value = gatewayIdentityServiceUrl && (raw === null || raw === void 0 ? void 0 : raw.issuer) && typeof raw.issuer === "string" ? (0, discovery_1.rewriteDiscoveryDocForIdentityUrl)(rewriteTarget, raw) : raw; if (useCache && cache) { cache.set(OIDC_CACHE_KEY, value); } return value; }; } exports.createGetOpenIdConfig = createGetOpenIdConfig; //# sourceMappingURL=oidc-config.js.map