UNPKG

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

Version:

176 lines (174 loc) • 7.32 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); // src/OID4VCIRestAPI.ts var import_oid4vci_issuer_server = require("@sphereon/oid4vci-issuer-server"); var import_ssi_sdk = require("@sphereon/ssi-sdk.oid4vci-issuer"); var import_express = __toESM(require("express"), 1); var import_swagger_ui_express = __toESM(require("swagger-ui-express"), 1); 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 issuer = await instance.get({ context, credentialDataSupplier: args.credentialDataSupplier }); 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_sdk.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_sdk.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_sdk.createVerifyAuthResponseCallback)({ path: opts.endpointOpts.authorizationChallengeOpts.verifyAuthResponseEndpointPath, presentationDefinitionId: instance.issuerOptions.presentationDefinitionId }); } } return new _OID4VCIRestAPI({ context, issuerInstanceArgs, expressSupport, opts, instance, issuer }); } OID4VCI_SWAGGER_URL = "https://api.swaggerhub.com/apis/SphereonInt/OID4VCI/0.1.1"; 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() { fetch(this.OID4VCI_SWAGGER_URL).then((res) => res.json()).then((swagger) => { const apiDocs = `/api-docs`; console.log(`[OID4VCI] API docs available at ${this._baseUrl.toString()}${this._basePath}${apiDocs}`); swagger.servers = [ { url: this._baseUrl.toString(), description: "This server" } ]; this.express.set("trust proxy", this.opts?.endpointOpts?.trustProxy ?? true); this._router.use(apiDocs, (req, res, next) => { req.swaggerDoc = swagger; next(); }, import_swagger_ui_express.default.serveFiles(swagger, options), import_swagger_ui_express.default.setup()); }).catch((err) => { console.log(`[OID4VCI] Unable to fetch swagger document: ${err}. Will not host api-docs on this instance`); }); const options = {}; } 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