@sphereon/ssi-sdk.siopv2-oid4vp-rp-rest-api
Version:
81 lines • 5.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SIOPv2RPApiServer = void 0;
const ssi_sdk_core_1 = require("@sphereon/ssi-sdk.core");
const ssi_express_support_1 = require("@sphereon/ssi-express-support");
const express_1 = __importDefault(require("express"));
const siop_api_functions_1 = require("./siop-api-functions");
const webapp_api_functions_1 = require("./webapp-api-functions");
const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
class SIOPv2RPApiServer {
constructor(args) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
this.OID4VP_SWAGGER_URL = 'https://api.swaggerhub.com/apis/SphereonInt/OID4VP/0.1.0';
const { agent, opts } = args;
this._agent = agent;
(0, ssi_express_support_1.copyGlobalAuthToEndpoints)({ opts, keys: ['webappCreateAuthRequest', 'webappAuthStatus', 'webappDeleteAuthRequest'] });
if ((_b = (_a = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _a === void 0 ? void 0 : _a.globalAuth) === null || _b === void 0 ? void 0 : _b.secureSiopEndpoints) {
(0, ssi_express_support_1.copyGlobalAuthToEndpoints)({ opts, keys: ['siopGetAuthRequest', 'siopVerifyAuthResponse'] });
}
this._opts = opts;
this._express = args.expressSupport.express;
this._router = express_1.default.Router();
const context = (0, ssi_sdk_core_1.agentContext)(agent);
const features = (_c = opts === null || opts === void 0 ? void 0 : opts.enableFeatures) !== null && _c !== void 0 ? _c : ['rp-status', 'siop'];
console.log(`SIOPv2 API enabled, with features: ${JSON.stringify(features)}}`);
// Webapp endpoints
if (features.includes('rp-status')) {
(0, webapp_api_functions_1.createAuthRequestWebappEndpoint)(this._router, context, (_d = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _d === void 0 ? void 0 : _d.webappCreateAuthRequest);
(0, webapp_api_functions_1.authStatusWebappEndpoint)(this._router, context, (_e = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _e === void 0 ? void 0 : _e.webappAuthStatus);
(0, webapp_api_functions_1.removeAuthRequestStateWebappEndpoint)(this._router, context, (_f = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _f === void 0 ? void 0 : _f.webappDeleteAuthRequest);
(0, webapp_api_functions_1.getDefinitionsEndpoint)(this._router, context, (_g = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _g === void 0 ? void 0 : _g.webappGetDefinitions);
}
// SIOPv2 endpoints
if (features.includes('siop')) {
(0, siop_api_functions_1.getAuthRequestSIOPv2Endpoint)(this._router, context, (_h = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _h === void 0 ? void 0 : _h.siopGetAuthRequest);
(0, siop_api_functions_1.verifyAuthResponseSIOPv2Endpoint)(this._router, context, (_j = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _j === void 0 ? void 0 : _j.siopVerifyAuthResponse);
}
this._basePath = (_l = (_k = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _k === void 0 ? void 0 : _k.basePath) !== null && _l !== void 0 ? _l : '';
this._express.use(this._basePath, this.router);
this._express.set('trust proxy', (_o = (_m = opts === null || opts === void 0 ? void 0 : opts.endpointOpts) === null || _m === void 0 ? void 0 : _m.trustProxy) !== null && _o !== void 0 ? _o : true);
this.setupSwaggerUi();
}
setupSwaggerUi() {
fetch(this.OID4VP_SWAGGER_URL)
.then((res) => res.json())
.then((swagger) => {
const apiDocs = `${this._basePath}/api-docs`;
console.log(`[OID4P] API docs available at ${apiDocs}`);
this._router.use('/api-docs', (req, res, next) => {
const regex = `${apiDocs.replace(/\//, '\/')}`.replace('/oid4vp', '').replace(/\/api-docs.*/, '');
swagger.servers = [{ url: `${req.protocol}://${req.get('host')}${regex}`, description: 'This server' }];
// @ts-ignore
req.swaggerDoc = swagger;
next();
}, swagger_ui_express_1.default.serveFiles(swagger, options), swagger_ui_express_1.default.setup());
})
.catch((err) => {
console.log(`[OID4VP] Unable to fetch swagger document: ${err}. Will not host api-docs on this instance`);
});
const options = {
// customCss: '.swagger-ui .topbar { display: none }',
};
}
get express() {
return this._express;
}
get router() {
return this._router;
}
get agent() {
return this._agent;
}
get opts() {
return this._opts;
}
}
exports.SIOPv2RPApiServer = SIOPv2RPApiServer;
//# sourceMappingURL=siopv2-rp-api-server.js.map