@sphereon/ssi-sdk.public-key-hosting
Version:
177 lines (169 loc) • 5.42 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/index.ts
import { Loggers } from "@sphereon/ssi-types";
// src/public-key-hosting.ts
import { agentContext } from "@sphereon/ssi-sdk.core";
import express from "express";
// src/api-functions.ts
import { checkAuth, sendErrorResponse } from "@sphereon/ssi-express-support";
import { contextHasPlugin } from "@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
import { toJwk } from "@sphereon/ssi-sdk-ext.key-utils";
import { asArray } from "@veramo/utils";
var toJWKS = /* @__PURE__ */ __name((args) => {
const providedKeys = asArray(args.keys);
const keys = providedKeys.map((key) => 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, checkAuth(opts?.endpoint), async (request, response) => {
try {
if (!contextHasPlugin(context, "keyManagerListKeys")) {
return 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 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, 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 sendErrorResponse(response, 404, `DID ${did} not found`);
}
response.statusCode = 200;
return response.send(toJWKS({
keys: resolution.keys
}));
} catch (e) {
console.log(e);
return 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 = express.Router();
const context = 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 = Loggers.DEFAULT.get("sphereon:public-key-hosting");
export {
PublicKeyHosting,
getAllJWKSEndpoint,
getDIDJWKSEndpoint,
jwksURIFromIdentifier,
logger,
toJWKS
};
//# sourceMappingURL=index.js.map