UNPKG

@sphereon/ssi-sdk.public-key-hosting

Version:

206 lines (198 loc) • 7.34 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { PublicKeyHosting: () => PublicKeyHosting, getAllJWKSEndpoint: () => getAllJWKSEndpoint, getDIDJWKSEndpoint: () => getDIDJWKSEndpoint, jwksURIFromIdentifier: () => jwksURIFromIdentifier, logger: () => logger, toJWKS: () => toJWKS }); module.exports = __toCommonJS(index_exports); var import_ssi_types = require("@sphereon/ssi-types"); // src/public-key-hosting.ts var import_ssi_sdk2 = require("@sphereon/ssi-sdk.core"); var import_express = __toESM(require("express"), 1); // src/api-functions.ts var import_ssi_express_support = require("@sphereon/ssi-express-support"); var import_ssi_sdk = require("@sphereon/ssi-sdk.agent-config"); // src/environment.ts var JKWS_HOSTING_ALL_KEYS_PATH = process.env.JWKS_HOSTING_BASE_PATH ?? "/.well-known/jwks.json"; var JWKS_HOSTING_DID_KEYS_PATH = process.env.JWKS_HOSTING_DID_PATH ?? "/.well-known/jwks/dids/:did"; // src/functions.ts var import_ssi_sdk_ext = require("@sphereon/ssi-sdk-ext.key-utils"); var import_utils = require("@veramo/utils"); var toJWKS = /* @__PURE__ */ __name((args) => { const providedKeys = (0, import_utils.asArray)(args.keys); const keys = providedKeys.map((key) => (0, import_ssi_sdk_ext.toJwk)(key.publicKeyHex, key.type, { key, isPrivateKey: false, noKidThumbprint: false })); return { keys }; }, "toJWKS"); var jwksURIFromIdentifier = /* @__PURE__ */ __name((args) => { const { onlyEncodeDid, identifier, baseURL } = args; let basePath = args.basePath ?? JWKS_HOSTING_DID_KEYS_PATH; const did = encodeURIComponent(identifier.did); if (onlyEncodeDid) { return did; } if (basePath.includes(":did")) { basePath = basePath.replace(":did", did); } else { basePath += basePath.endsWith("/") ? did : `/${did}`; } if (baseURL) { return baseURL + baseURL.endsWith("/") ? basePath : `/${basePath}`; } return basePath; }, "jwksURIFromIdentifier"); // src/api-functions.ts function getAllJWKSEndpoint(router, context, opts) { if (opts?.enabled === false) { logger.info(`Get all JWKS endpoint is disabled`); return; } const path = opts?.path ?? JKWS_HOSTING_ALL_KEYS_PATH; logger.info(`All JWKS endpoint enabled, path ${path}`); router.get(path, (0, import_ssi_express_support.checkAuth)(opts?.endpoint), async (request, response) => { try { if (!(0, import_ssi_sdk.contextHasPlugin)(context, "keyManagerListKeys")) { return (0, import_ssi_express_support.sendErrorResponse)(response, 500, "Key manager plugin that can list keys is not found. Please enable the Sphereon Key Manager plugin if you want to use this endpoint"); } response.statusCode = 202; const keys = await context.agent.keyManagerListKeys(); return response.send(toJWKS({ keys })); } catch (e) { return (0, import_ssi_express_support.sendErrorResponse)(response, 500, e.message, e); } }); } __name(getAllJWKSEndpoint, "getAllJWKSEndpoint"); function getDIDJWKSEndpoint(router, context, opts) { if (opts?.enabled === false) { logger.info(`Get DID JWKS endpoint is disabled`); return; } const path = opts?.path ?? JWKS_HOSTING_DID_KEYS_PATH; console.info(`DID JWKS endpoint enabled, path ${path}`); router.get(path, (0, import_ssi_express_support.checkAuth)(opts?.endpoint), async (request, response) => { const did = request.params.did; try { console.log(`Will get JWKS for DID ${did}`); const resolution = await context.agent.identifierManagedGetByDid({ identifier: did }); if (!resolution.identifier) { return (0, import_ssi_express_support.sendErrorResponse)(response, 404, `DID ${did} not found`); } response.statusCode = 200; return response.send(toJWKS({ keys: resolution.keys })); } catch (e) { console.log(e); return (0, import_ssi_express_support.sendErrorResponse)(response, 404, `DID ${did} not found`); } }); } __name(getDIDJWKSEndpoint, "getDIDJWKSEndpoint"); // src/public-key-hosting.ts var PublicKeyHosting = class { static { __name(this, "PublicKeyHosting"); } get router() { return this._router; } _express; _agent; _opts; _router; constructor(args) { const { agent, opts } = args; this._agent = agent; if (opts?.endpointOpts?.globalAuth) { copyGlobalAuthToEndpoint(opts, "allJWKS"); copyGlobalAuthToEndpoint(opts, "DIDJWKS"); } this._opts = opts; this._express = args.expressSupport.express; this._router = import_express.default.Router(); const context = (0, import_ssi_sdk2.agentContext)(agent); const features = opts?.hostingOpts?.enableFeatures ?? [ "all-jwks", "did-jwks" ]; logger.info(`Public key hosting enabled, with features: ${JSON.stringify(features)}`); if (features.includes("all-jwks")) { getAllJWKSEndpoint(this.router, context, { ...opts?.endpointOpts?.allJWKS }); } if (features.includes("did-jwks")) { getDIDJWKSEndpoint(this.router, context, opts?.endpointOpts?.DIDJWKS); } this._express.use(opts?.endpointOpts?.basePath ?? "", this.router); } get agent() { return this._agent; } get opts() { return this._opts; } get express() { return this._express; } }; function copyGlobalAuthToEndpoint(opts, key) { if (opts?.endpointOpts?.globalAuth) { opts.endpointOpts[key] = { // @ts-ignore ...opts.endpointOpts[key], // @ts-ignore endpoint: { ...opts.endpointOpts.globalAuth, ...opts.endpointOpts[key]?.endpoint } }; } } __name(copyGlobalAuthToEndpoint, "copyGlobalAuthToEndpoint"); // src/index.ts var logger = import_ssi_types.Loggers.DEFAULT.get("sphereon:public-key-hosting"); //# sourceMappingURL=index.cjs.map