UNPKG

@sphereon/ssi-sdk.oid4vci-issuer-rest-api

Version:

253 lines (250 loc) • 11.6 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, { OID4VCIRestAPI: () => OID4VCIRestAPI }); module.exports = __toCommonJS(index_exports); // ../../node_modules/.pnpm/tsup@8.5.1_@microsoft+api-extractor@7.55.0_@types+node@20.19.25__@swc+core@1.15.2_@swc+_119fedc18e19d1717452bb9e989205b3/node_modules/tsup/assets/cjs_shims.js var getImportMetaUrl = /* @__PURE__ */ __name(() => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href, "getImportMetaUrl"); var importMetaUrl = /* @__PURE__ */ getImportMetaUrl(); // src/OID4VCIRestAPI.ts var import_oid4vci_issuer_server = require("@sphereon/oid4vci-issuer-server"); var import_ssi_sdk_ext = require("@sphereon/ssi-sdk-ext.identifier-resolution"); var import_ssi_sdk = require("@sphereon/ssi-sdk.data-store-types"); var import_ssi_sdk2 = require("@sphereon/ssi-sdk.credential-store"); var import_ssi_sdk3 = require("@sphereon/ssi-sdk.oid4vci-issuer"); var import_ssi_types = require("@sphereon/ssi-types"); var import_express = __toESM(require("express"), 1); var import_fs = __toESM(require("fs"), 1); var import_path = __toESM(require("path"), 1); var import_url = require("url"); var import_swagger_ui_express = __toESM(require("swagger-ui-express"), 1); var __filename2 = (0, import_url.fileURLToPath)(importMetaUrl); var __dirname = import_path.default.dirname(__filename2); var OID4VCIRestAPI = class _OID4VCIRestAPI { static { __name(this, "OID4VCIRestAPI"); } _expressSupport; _context; _opts; _restApi; _instance; _issuer; _router; _baseUrl; _basePath; static async init(args) { const { issuerInstanceArgs, context } = args; const opts = args.opts ?? {}; const expressSupport = args.expressSupport; const instance = await context.agent.oid4vciGetInstance(args.issuerInstanceArgs); const wrapCredentialSignerCallback = opts.persistIssuedCredentials ? _OID4VCIRestAPI.buildPersistenceSignerWrapper({ context, instance }) : void 0; const issuer = await instance.get({ context, credentialDataSupplier: args.credentialDataSupplier, wrapCredentialSignerCallback }); if (!opts.endpointOpts) { opts.endpointOpts = {}; } if (!opts.endpointOpts.tokenEndpointOpts) { opts.endpointOpts.tokenEndpointOpts = { accessTokenIssuer: instance.metadataOptions.credentialIssuer ?? issuer.issuerMetadata.credential_issuer }; } if (opts?.endpointOpts.tokenEndpointOpts?.tokenEndpointDisabled !== true && typeof opts?.endpointOpts.tokenEndpointOpts?.accessTokenSignerCallback !== "function") { const idOpts = instance.issuerOptions.idOpts; const tokenOpts = { iss: opts.endpointOpts.tokenEndpointOpts.accessTokenIssuer ?? instance.metadataOptions.credentialIssuer, didOpts: instance.issuerOptions.didOpts, idOpts }; opts.endpointOpts.tokenEndpointOpts.accessTokenSignerCallback = await (0, import_ssi_sdk3.getAccessTokenSignerCallback)({ ...tokenOpts }, args.context); } if (opts?.endpointOpts.authorizationChallengeOpts?.enabled === true) { if (!instance.issuerOptions.presentationDefinitionId) { throw Error(`Unable to set createAuthRequestUriCallback. No presentationDefinitionId present in issuer options`); } if (typeof opts?.endpointOpts.authorizationChallengeOpts.createAuthRequestUriCallback !== "function") { if (!opts.endpointOpts.authorizationChallengeOpts?.createAuthRequestUriEndpointPath) { throw Error(`Unable to set createAuthRequestUriCallback. No createAuthRequestUriEndpointPath present in options`); } opts.endpointOpts.authorizationChallengeOpts.createAuthRequestUriCallback = await (0, import_ssi_sdk3.createAuthRequestUriCallback)({ path: opts.endpointOpts.authorizationChallengeOpts.createAuthRequestUriEndpointPath, presentationDefinitionId: instance.issuerOptions.presentationDefinitionId }); } if (typeof opts?.endpointOpts.authorizationChallengeOpts?.verifyAuthResponseCallback !== "function") { if (!opts.endpointOpts.authorizationChallengeOpts?.verifyAuthResponseEndpointPath) { throw Error(`Unable to set verifyAuthResponseCallback. No createAuthRequestUriEndpointPath present in options`); } opts.endpointOpts.authorizationChallengeOpts.verifyAuthResponseCallback = await (0, import_ssi_sdk3.createVerifyAuthResponseCallback)({ path: opts.endpointOpts.authorizationChallengeOpts.verifyAuthResponseEndpointPath, presentationDefinitionId: instance.issuerOptions.presentationDefinitionId }); } } return new _OID4VCIRestAPI({ context, issuerInstanceArgs, expressSupport, opts, instance, issuer }); } /** * Builds a {@link CredentialSignerCallback} wrapper that persists each issued credential as a * {@link CredentialRole.ISSUER} `DigitalCredential` row after delegating the actual signing to * the underlying signer. Issuer identity is resolved from the *signed* credential (so any * issuer normalization performed by the signer is preserved in the persisted row). The * correlation type is derived from the resolved issuer id (`did:` prefix -> DID, otherwise URL), * matching the pattern used by the holder-side persistence in `OID4VCIHolder`. * * Persistence errors propagate to the caller (fail-fast). Callers that opt into * `persistIssuedCredentials` are explicitly requesting the row; a silent drop here would * reproduce the DEV-35 defect. */ static buildPersistenceSignerWrapper(args) { const { context, instance } = args; return (originalSigner) => { return async (signerArgs) => { const signed = await originalSigner(signerArgs); const rawDocument = (0, import_ssi_sdk.ensureRawDocument)(signed); const uniform = import_ssi_types.CredentialMapper.toUniformCredential(signed); let issuerId = import_ssi_types.CredentialMapper.issuerCorrelationIdFromIssuerType(uniform.issuer); if (!issuerId) { const fromIdOpts = instance.issuerOptions.idOpts?.identifier ?? instance.issuerOptions.didOpts?.idOpts?.identifier; if (typeof fromIdOpts === "string") { issuerId = fromIdOpts; } } if (!issuerId) { return Promise.reject(Error("Cannot persist issued credential: unable to determine issuer identifier from signed credential")); } const identifier = await context.agent.identifierManagedGet({ identifier: issuerId, issuer: issuerId, vmRelationship: "assertionMethod" }); let issuerCorrelationId = identifier.issuer; if (!issuerCorrelationId && (0, import_ssi_sdk_ext.isDidIdentifier)(identifier.identifier)) { if ((0, import_ssi_sdk_ext.isIIdentifier)(identifier.identifier)) { issuerCorrelationId = identifier.identifier.did; } else if (typeof identifier.identifier === "string") { issuerCorrelationId = identifier.identifier; } } if (!issuerCorrelationId) { issuerCorrelationId = issuerId; } const dc = { credential: { credentialRole: import_ssi_types.CredentialRole.ISSUER, kmsKeyRef: identifier.kmsKeyRef, identifierMethod: identifier.method, issuerCorrelationId, issuerCorrelationType: issuerCorrelationId.startsWith("did:") ? import_ssi_sdk2.CredentialCorrelationType.DID : import_ssi_sdk2.CredentialCorrelationType.URL, rawDocument } }; await context.agent.crsAddCredential(dc); return signed; }; }; } OID4VCI_OPENAPI_FILE = import_path.default.join(__dirname, "..", "oid4vci-openapi.yml"); constructor(args) { const { context, opts, issuerInstanceArgs } = args; this._baseUrl = new URL(opts?.baseUrl ?? process.env.BASE_URL ?? opts?.issuer?.issuerMetadata?.credential_issuer ?? issuerInstanceArgs.credentialIssuer ?? "http://localhost"); this._basePath = (0, import_oid4vci_issuer_server.getBasePath)(this._baseUrl); this._context = context; this._opts = opts ?? {}; this._expressSupport = args.expressSupport; this._issuer = args.issuer; this._instance = args.instance; this._restApi = new import_oid4vci_issuer_server.OID4VCIServer(args.expressSupport, { ...opts, issuer: this._issuer }); this._router = import_express.default.Router(); this.express.use(this._basePath, this._router); this.setupSwaggerUi(); } setupSwaggerUi() { const apiDocsPath = `/api-docs`; const specPath = `/api-docs/spec.yaml`; const fullSpecPath = `${this._basePath}${specPath}`; if (!import_fs.default.existsSync(this.OID4VCI_OPENAPI_FILE)) { console.log(`[OID4VCI] OpenAPI spec not found at ${this.OID4VCI_OPENAPI_FILE}. Swagger UI disabled.`); return; } console.log(`[OID4VCI] API docs available at ${this._baseUrl.origin}${this._basePath}${apiDocsPath}`); this.express.set("trust proxy", this.opts?.endpointOpts?.trustProxy ?? true); this._router.get(specPath, (req, res) => { res.type("text/yaml").sendFile(this.OID4VCI_OPENAPI_FILE); }); this._router.use(apiDocsPath, import_swagger_ui_express.default.serve, import_swagger_ui_express.default.setup(void 0, { swaggerOptions: { url: fullSpecPath } })); } get express() { return this._expressSupport.express; } get context() { return this._context; } get opts() { return this._opts; } get restApi() { return this._restApi; } get instance() { return this._instance; } get issuer() { return this._issuer; } async stop() { return this._expressSupport.stop(); } }; //# sourceMappingURL=index.cjs.map