UNPKG

@sphereon/oid4vci-issuer

Version:

OpenID 4 Verifiable Credential Issuance issuer REST endpoints

176 lines 8.14 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VcIssuerBuilder = void 0; const oid4vci_common_1 = require("@sphereon/oid4vci-common"); const VcIssuer_1 = require("../VcIssuer"); const functions_1 = require("../functions"); const state_manager_1 = require("../state-manager"); class VcIssuerBuilder { constructor() { this.issuerMetadata = {}; this.authorizationServerMetadata = {}; } withIssuerMetadata(issuerMetadata) { if (!issuerMetadata.credential_configurations_supported) { throw new Error('IssuerMetadata should be from type v1_0_13 or higher.'); } this.issuerMetadata = issuerMetadata; return this; } withASClientMetadata(clientMetadata) { this.asClientOpts = clientMetadata; return this; } withASClientMetadataParams(_a) { var { client_id, client_secret, redirect_uris, response_types } = _a, other = __rest(_a, ["client_id", "client_secret", "redirect_uris", "response_types"]); this.asClientOpts = Object.assign(Object.assign({}, other), { client_id, client_secret, redirect_uris, response_types }); return this; } withAuthorizationMetadata(authorizationServerMetadata) { this.authorizationServerMetadata = authorizationServerMetadata; return this; } withIssuerMetadataBuilder(builder) { this.issuerMetadataBuilder = builder; return this; } withDefaultCredentialOfferBaseUri(baseUri) { this.defaultCredentialOfferBaseUri = baseUri; return this; } withCredentialIssuer(issuer) { this.issuerMetadata.credential_issuer = issuer; return this; } withAuthorizationServers(authorizationServers) { this.issuerMetadata.authorization_servers = typeof authorizationServers === 'string' ? [authorizationServers] : authorizationServers; return this; } withCredentialEndpoint(credentialEndpoint) { this.issuerMetadata.credential_endpoint = credentialEndpoint; return this; } withBatchCredentialEndpoint(batchCredentialEndpoint) { this.issuerMetadata.batch_credential_endpoint = batchCredentialEndpoint; throw Error('Not implemented yet'); // return this } withTokenEndpoint(tokenEndpoint) { this.issuerMetadata.token_endpoint = tokenEndpoint; return this; } withIssuerDisplay(issuerDisplay) { this.issuerMetadata.display = Array.isArray(issuerDisplay) ? issuerDisplay : [issuerDisplay]; return this; } addIssuerDisplay(issuerDisplay) { var _a; this.issuerMetadata.display = [...((_a = this.issuerMetadata.display) !== null && _a !== void 0 ? _a : []), issuerDisplay]; return this; } withCredentialConfigurationsSupported(credentialConfigurationsSupported) { this.issuerMetadata.credential_configurations_supported = credentialConfigurationsSupported; return this; } addCredentialConfigurationsSupported(id, supportedCredential) { if (!this.issuerMetadata.credential_configurations_supported) { this.issuerMetadata.credential_configurations_supported = {}; } this.issuerMetadata.credential_configurations_supported[id] = supportedCredential; return this; } withTXCode(txCode) { this.txCode = txCode; return this; } withCredentialOfferURIStateManager(credentialOfferURIManager) { this.credentialOfferURIManager = credentialOfferURIManager; return this; } withInMemoryCredentialOfferURIState() { this.withCredentialOfferURIStateManager(new state_manager_1.MemoryStates()); return this; } withCredentialOfferStateManager(credentialOfferManager) { this.credentialOfferStateManager = credentialOfferManager; return this; } withInMemoryCredentialOfferState() { this.withCredentialOfferStateManager(new state_manager_1.MemoryStates()); return this; } withCNonceStateManager(cNonceManager) { this.cNonceStateManager = cNonceManager; return this; } withInMemoryCNonceState() { this.withCNonceStateManager(new state_manager_1.MemoryStates()); return this; } withCNonceExpiresIn(cNonceExpiresIn) { this.cNonceExpiresIn = cNonceExpiresIn; return this; } withCredentialSignerCallback(cb) { this.credentialSignerCallback = cb; return this; } withJWTVerifyCallback(verifyCallback) { this.jwtVerifyCallback = verifyCallback; return this; } withCredentialDataSupplier(credentialDataSupplier) { this.credentialDataSupplier = credentialDataSupplier; return this; } build() { var _a, _b, _c; if (!this.credentialOfferStateManager) { throw new Error(oid4vci_common_1.TokenErrorResponse.invalid_request); } if (!this.cNonceStateManager) { throw new Error(oid4vci_common_1.TokenErrorResponse.invalid_request); } if (Object.keys(this.issuerMetadata).length === 0) { throw new Error('issuerMetadata not set'); } if (Object.keys(this.authorizationServerMetadata).length === 0) { throw new Error('authorizationServerMetadata not set'); } const builder = (_a = this.issuerMetadataBuilder) === null || _a === void 0 ? void 0 : _a.build(); const metadata = Object.assign(Object.assign({}, this.issuerMetadata), builder); // Let's make sure these get merged correctly: metadata.credential_configurations_supported = this.issuerMetadata.credential_configurations_supported; metadata.display = [...((_b = this.issuerMetadata.display) !== null && _b !== void 0 ? _b : []), ...((_c = builder === null || builder === void 0 ? void 0 : builder.display) !== null && _c !== void 0 ? _c : [])]; if (!metadata.credential_endpoint || !metadata.credential_issuer || !this.issuerMetadata.credential_configurations_supported) { throw new Error(oid4vci_common_1.TokenErrorResponse.invalid_request); } if (this.asClientOpts && typeof this.jwtVerifyCallback !== 'function') { if (!this.issuerMetadata.credential_issuer) { throw Error('issuerMetadata.credential_issuer is required when using asClientOpts'); } else if (!this.issuerMetadata.authorization_servers) { throw Error('issuerMetadata.authorization_servers is required when using asClientOpts'); } this.jwtVerifyCallback = (0, functions_1.oidcAccessTokenVerifyCallback)({ clientMetadata: this.asClientOpts, credentialIssuer: this.issuerMetadata.credential_issuer, authorizationServer: this.issuerMetadata.authorization_servers[0], }); } return new VcIssuer_1.VcIssuer(metadata, this.authorizationServerMetadata, Object.assign(Object.assign({}, (this.txCode && { txCode: this.txCode })), { defaultCredentialOfferBaseUri: this.defaultCredentialOfferBaseUri, credentialSignerCallback: this.credentialSignerCallback, jwtVerifyCallback: this.jwtVerifyCallback, credentialDataSupplier: this.credentialDataSupplier, credentialOfferSessions: this.credentialOfferStateManager, cNonces: this.cNonceStateManager, cNonceExpiresIn: this.cNonceExpiresIn, uris: this.credentialOfferURIManager, asClientOpts: this.asClientOpts })); } } exports.VcIssuerBuilder = VcIssuerBuilder; //# sourceMappingURL=VcIssuerBuilder.js.map