UNPKG

@sphereon/oid4vci-client

Version:

OpenID for Verifiable Credential Issuance (OpenID4VCI) client

50 lines 3.59 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUriEncoded = isUriEncoded; exports.handleCredentialOfferUri = handleCredentialOfferUri; exports.constructBaseResponse = constructBaseResponse; const oid4vci_common_1 = require("@sphereon/oid4vci-common"); const cross_fetch_1 = require("cross-fetch"); function isUriEncoded(str) { const pattern = /%[0-9A-F]{2}/i; return pattern.test(str); } function handleCredentialOfferUri(uri) { return __awaiter(this, void 0, void 0, function* () { var _a; const uriObj = (0, oid4vci_common_1.getURIComponentsAsArray)(uri); const credentialOfferUri = decodeURIComponent(uriObj['credential_offer_uri']); const decodedUri = isUriEncoded(credentialOfferUri) ? decodeURIComponent(credentialOfferUri) : credentialOfferUri; const response = yield (0, cross_fetch_1.fetch)(decodedUri); if (!(response && response.status >= 200 && response.status < 400)) { return Promise.reject(`the credential offer URI endpoint call was not successful. http code ${response.status} - reason ${response.statusText}`); } if (((_a = response.headers.get('Content-Type')) === null || _a === void 0 ? void 0 : _a.startsWith('application/json')) === false) { return Promise.reject('the credential offer URI endpoint did not return content type application/json'); } return { credential_offer: (0, oid4vci_common_1.decodeJsonProperties)(yield response.json()), }; }); } function constructBaseResponse(request, scheme, baseUrl) { var _a, _b, _c, _d, _e, _f; const clientId = (0, oid4vci_common_1.getClientIdFromCredentialOfferPayload)(request.credential_offer); const grants = (_a = request.credential_offer) === null || _a === void 0 ? void 0 : _a.grants; return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ scheme, baseUrl }, (clientId && { clientId })), request), (((_b = grants === null || grants === void 0 ? void 0 : grants.authorization_code) === null || _b === void 0 ? void 0 : _b.issuer_state) && { issuerState: grants.authorization_code.issuer_state })), (((_c = grants === null || grants === void 0 ? void 0 : grants[oid4vci_common_1.PRE_AUTH_GRANT_LITERAL]) === null || _c === void 0 ? void 0 : _c[oid4vci_common_1.PRE_AUTH_CODE_LITERAL]) && { preAuthorizedCode: grants[oid4vci_common_1.PRE_AUTH_GRANT_LITERAL][oid4vci_common_1.PRE_AUTH_CODE_LITERAL], })), (((_f = (_e = (_d = request.credential_offer) === null || _d === void 0 ? void 0 : _d.grants) === null || _e === void 0 ? void 0 : _e[oid4vci_common_1.PRE_AUTH_GRANT_LITERAL]) === null || _f === void 0 ? void 0 : _f.tx_code) && { txCode: request.credential_offer.grants[oid4vci_common_1.PRE_AUTH_GRANT_LITERAL].tx_code, })); } //# sourceMappingURL=CredentialOfferCommons.js.map