UNPKG

@mft/moneyhub-api-client

Version:
113 lines 5.81 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Moneyhub = void 0; const openid_client_1 = require("openid-client"); const get_auth_urls_1 = __importDefault(require("./get-auth-urls")); const tokens_1 = __importDefault(require("./tokens")); const requests_1 = __importDefault(require("./requests")); const R = __importStar(require("ramda")); const request_1 = __importDefault(require("./request")); const discovery_1 = require("./discovery"); const oidc_config_1 = require("./oidc-config"); const DEFAULT_TIMEOUT = 60000; const DEFAULT_OIDC_CACHE_TTL_MS = 3600000; // 1 hour function buildConfig(apiClientConfig) { return R.evolve({ identityServiceUrl: (val) => val.replace("/oidc", ""), gatewayIdentityServiceUrl: (val) => val == null ? val : val.replace(/\/oidc\/?$/, ""), caasResourceServerUrl: (val) => `${val.replace(/\/v\d+(\.\d+)?\b/, "")}/caas/v1`, }, { ...apiClientConfig, caasResourceServerUrl: apiClientConfig.resourceServerUrl, }); } function effectiveUrls(config) { var _a, _b, _c, _d, _e; return { identity: (_a = config.gatewayIdentityServiceUrl) !== null && _a !== void 0 ? _a : config.identityServiceUrl, resource: (_b = config.gatewayResourceServerUrl) !== null && _b !== void 0 ? _b : config.resourceServerUrl, caas: (_c = config.gatewayCaasResourceServerUrl) !== null && _c !== void 0 ? _c : config.caasResourceServerUrl, osip: (_d = config.gatewayOsipResourceServerUrl) !== null && _d !== void 0 ? _d : config.osipResourceServerUrl, accountConnect: (_e = config.gatewayAccountConnectUrl) !== null && _e !== void 0 ? _e : config.accountConnectUrl, }; } const _Moneyhub = async (apiClientConfig) => { const config = buildConfig(apiClientConfig); const urls = effectiveUrls(config); const { options = {}, client: clientCreds } = config; const { timeout = DEFAULT_TIMEOUT, apiVersioning = true, agent, openIdConfigCacheTtlMs = DEFAULT_OIDC_CACHE_TTL_MS, retry = {} } = options; const { mTLS } = clientCreds; openid_client_1.custom.setHttpOptionsDefaults({ timeout, ...mTLS ? { cert: mTLS.cert, key: mTLS.key } : {}, }); const discoveryOpts = { timeout, agent: options.agent, mTLS: mTLS !== null && mTLS !== void 0 ? mTLS : undefined }; const discoveryMetadata = config.gatewayIdentityServiceUrl ? await (0, discovery_1.getDiscoveryWithGatewayUrl)(urls.identity, discoveryOpts) : await (0, discovery_1.getDiscovery)(urls.identity, discoveryOpts); const moneyhubIssuer = new openid_client_1.Issuer(discoveryMetadata); const client = new moneyhubIssuer.Client({ ...R.pick(["client_id", "client_secret", "id_token_signed_response_alg", "redirect_uri", "token_endpoint_auth_method", "request_object_signing_alg"], clientCreds), tls_client_certificate_bound_access_tokens: (mTLS === null || mTLS === void 0 ? void 0 : mTLS.tls_client_certificate_bound_access_tokens) || false, }, { keys: clientCreds.keys }); client[openid_client_1.custom.clock_tolerance] = 10; const requestFn = (0, request_1.default)({ client, options: { timeout, apiVersioning, agent, mTLS, retry }, identityServiceUrl: urls.identity, gatewayResourceServerUrl: config.gatewayResourceServerUrl, gatewayCaasResourceServerUrl: config.gatewayCaasResourceServerUrl, gatewayOsipResourceServerUrl: config.gatewayOsipResourceServerUrl, }); const getOpenIdConfig = (0, oidc_config_1.createGetOpenIdConfig)({ identityServiceUrl: urls.identity, gatewayIdentityServiceUrl: config.gatewayIdentityServiceUrl, openIdConfigCacheTtlMs, request: requestFn, }); const configWithGetOpenIdConfig = { ...config, resourceServerUrl: urls.resource, identityServiceUrl: urls.identity, caasResourceServerUrl: urls.caas, osipResourceServerUrl: urls.osip, accountConnectUrl: urls.accountConnect, getOpenIdConfig, }; return { ...(0, get_auth_urls_1.default)({ client, config: configWithGetOpenIdConfig }), ...(0, tokens_1.default)({ client, config: configWithGetOpenIdConfig }), ...(0, requests_1.default)({ config: configWithGetOpenIdConfig, request: requestFn }), keys: () => { var _a; return (((_a = clientCreds.keys) === null || _a === void 0 ? void 0 : _a.length) ? { keys: clientCreds.keys } : null); }, generators: openid_client_1.generators, }; }; const Moneyhub = _Moneyhub; exports.Moneyhub = Moneyhub; //# sourceMappingURL=index.js.map