@sphereon/oid4vci-client
Version:
OpenID for Verifiable Credential Issuance (OpenID4VCI) client
101 lines • 6.72 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialOfferClientV1_0_11 = void 0;
const oid4vci_common_1 = require("@sphereon/oid4vci-common");
const debug_1 = __importDefault(require("debug"));
const debug = (0, debug_1.default)('sphereon:oid4vci:offer');
class CredentialOfferClientV1_0_11 {
static fromURI(uri, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g;
debug(`Credential Offer URI: ${uri}`);
if (!uri.includes('?') || !uri.includes('://')) {
debug(`Invalid Credential Offer URI: ${uri}`);
throw Error(`Invalid Credential Offer Request`);
}
const scheme = uri.split('://')[0];
const baseUrl = uri.split('?')[0];
const version = (0, oid4vci_common_1.determineSpecVersionFromURI)(uri);
let credentialOffer;
let credentialOfferPayload;
if (version < oid4vci_common_1.OpenId4VCIVersion.VER_1_0_11) {
credentialOfferPayload = (0, oid4vci_common_1.convertURIToJsonObject)(uri, {
arrayTypeProperties: ['credential_type'],
requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri='] : ['issuer', 'credential_type='],
});
credentialOffer = {
credential_offer: credentialOfferPayload,
};
}
else {
credentialOffer = (0, oid4vci_common_1.convertURIToJsonObject)(uri, {
arrayTypeProperties: ['credentials'],
requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri='] : ['credential_offer='],
});
if ((credentialOffer === null || credentialOffer === void 0 ? void 0 : credentialOffer.credential_offer_uri) === undefined && !(credentialOffer === null || credentialOffer === void 0 ? void 0 : credentialOffer.credential_offer)) {
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri); // cannot be reached since convertURIToJsonObject will check the params
}
}
const request = yield (0, oid4vci_common_1.toUniformCredentialOfferRequest)(credentialOffer, Object.assign(Object.assign({}, opts), { version }));
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],
})), { userPinRequired: (_g = !!((_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.user_pin_required)) !== null && _g !== void 0 ? _g : false });
});
}
static toURI(requestWithBaseUrl, opts) {
var _a, _b;
debug(`Credential Offer Request with base URL: ${JSON.stringify(requestWithBaseUrl)}`);
const version = (_a = opts === null || opts === void 0 ? void 0 : opts.version) !== null && _a !== void 0 ? _a : requestWithBaseUrl.version;
let baseUrl = requestWithBaseUrl.baseUrl.includes(requestWithBaseUrl.scheme)
? requestWithBaseUrl.baseUrl
: `${requestWithBaseUrl.scheme.replace('://', '')}://${requestWithBaseUrl.baseUrl}`;
let param;
const isUri = requestWithBaseUrl.credential_offer_uri !== undefined;
if (version.valueOf() >= oid4vci_common_1.OpenId4VCIVersion.VER_1_0_11.valueOf()) {
// v11 changed from encoding every param to a encoded json object with a credential_offer param key
if (!baseUrl.includes('?')) {
param = isUri ? 'credential_offer_uri' : 'credential_offer';
}
else {
const split = baseUrl.split('?');
if (split.length > 1 && split[1] !== '') {
if (baseUrl.endsWith('&')) {
param = isUri ? 'credential_offer_uri' : 'credential_offer';
}
else if (!baseUrl.endsWith('=')) {
baseUrl += `&`;
param = isUri ? 'credential_offer_uri' : 'credential_offer';
}
}
}
}
return (0, oid4vci_common_1.convertJsonToURI)((_b = requestWithBaseUrl.credential_offer_uri) !== null && _b !== void 0 ? _b : requestWithBaseUrl.original_credential_offer, {
baseUrl,
arrayTypeProperties: isUri ? [] : ['credential_type'],
uriTypeProperties: isUri
? ['credential_offer_uri']
: version >= oid4vci_common_1.OpenId4VCIVersion.VER_1_0_11
? ['credential_issuer', 'credential_type']
: ['issuer', 'credential_type'],
param,
version,
});
}
}
exports.CredentialOfferClientV1_0_11 = CredentialOfferClientV1_0_11;
//# sourceMappingURL=CredentialOfferClientV1_0_11.js.map