UNPKG

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

Version:

145 lines (144 loc) 5.67 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/OID4VCIRestAPI.ts import { getBasePath, OID4VCIServer } from "@sphereon/oid4vci-issuer-server"; import { createAuthRequestUriCallback, getAccessTokenSignerCallback, createVerifyAuthResponseCallback } from "@sphereon/ssi-sdk.oid4vci-issuer"; import express from "express"; import swaggerUi from "swagger-ui-express"; 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 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 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 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 = getBasePath(this._baseUrl); this._context = context; this._opts = opts ?? {}; this._expressSupport = args.expressSupport; this._issuer = args.issuer; this._instance = args.instance; this._restApi = new OID4VCIServer(args.expressSupport, { ...opts, issuer: this._issuer }); this._router = express.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(); }, swaggerUi.serveFiles(swagger, options), swaggerUi.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(); } }; export { OID4VCIRestAPI }; //# sourceMappingURL=index.js.map