@sphereon/oid4vci-common
Version:
OpenID 4 Verifiable Credential Issuance Common Types
80 lines • 4.93 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.acquireDeferredCredential = exports.isDeferredCredentialIssuancePending = exports.isDeferredCredentialResponse = void 0;
const HttpUtils_1 = require("./HttpUtils");
function isDeferredCredentialResponse(credentialResponse) {
const orig = credentialResponse.successBody;
// Specs mention 202, but some implementations like EBSI return 200
return credentialResponse.origResponse.status % 200 <= 2 && !!orig && !orig.credential && (!!orig.acceptance_token || !!orig.transaction_id);
}
exports.isDeferredCredentialResponse = isDeferredCredentialResponse;
function assertNonFatalError(credentialResponse) {
var _a;
if (credentialResponse.origResponse.status === 400 && ((_a = credentialResponse.errorBody) === null || _a === void 0 ? void 0 : _a.error)) {
if (credentialResponse.errorBody.error === 'invalid_transaction_id' || credentialResponse.errorBody.error.includes('acceptance_token')) {
throw Error('Invalid transaction id. Probably the deferred credential request expired');
}
}
}
function isDeferredCredentialIssuancePending(credentialResponse) {
var _a, _b, _c, _d, _e;
if (isDeferredCredentialResponse(credentialResponse)) {
return (_b = !!((_a = credentialResponse === null || credentialResponse === void 0 ? void 0 : credentialResponse.successBody) === null || _a === void 0 ? void 0 : _a.transaction_id)) !== null && _b !== void 0 ? _b : !!((_c = credentialResponse === null || credentialResponse === void 0 ? void 0 : credentialResponse.successBody) === null || _c === void 0 ? void 0 : _c.acceptance_token);
}
if (credentialResponse.origResponse.status === 400 && ((_d = credentialResponse.errorBody) === null || _d === void 0 ? void 0 : _d.error)) {
if (credentialResponse.errorBody.error === 'issuance_pending') {
return true;
}
else if ((_e = credentialResponse.errorBody.error_description) === null || _e === void 0 ? void 0 : _e.toLowerCase().includes('not available yet')) {
return true;
}
}
return false;
}
exports.isDeferredCredentialIssuancePending = isDeferredCredentialIssuancePending;
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function acquireDeferredCredential(_a) {
return __awaiter(this, arguments, void 0, function* ({ bearerToken, transactionId, deferredCredentialEndpoint, deferredCredentialIntervalInMS, deferredCredentialAwait, }) {
var _b;
let credentialResponse = yield acquireDeferredCredentialImpl({
bearerToken,
transactionId,
deferredCredentialEndpoint,
});
const DEFAULT_SLEEP_IN_MS = 5000;
while (!((_b = credentialResponse.successBody) === null || _b === void 0 ? void 0 : _b.credential) && deferredCredentialAwait) {
assertNonFatalError(credentialResponse);
const pending = isDeferredCredentialIssuancePending(credentialResponse);
console.log(`Issuance still pending?: ${pending}`);
if (!pending) {
throw Error(`Issuance isn't pending anymore: ${credentialResponse}`);
}
yield sleep(deferredCredentialIntervalInMS !== null && deferredCredentialIntervalInMS !== void 0 ? deferredCredentialIntervalInMS : DEFAULT_SLEEP_IN_MS);
credentialResponse = yield acquireDeferredCredentialImpl({ bearerToken, transactionId, deferredCredentialEndpoint });
}
return credentialResponse;
});
}
exports.acquireDeferredCredential = acquireDeferredCredential;
function acquireDeferredCredentialImpl(_a) {
return __awaiter(this, arguments, void 0, function* ({ bearerToken, transactionId, deferredCredentialEndpoint, }) {
const response = yield (0, HttpUtils_1.post)(deferredCredentialEndpoint, JSON.stringify(transactionId ? { transaction_id: transactionId } : ''), { bearerToken });
console.log(JSON.stringify(response, null, 2));
assertNonFatalError(response);
return Object.assign(Object.assign({}, response), { access_token: bearerToken });
});
}
//# sourceMappingURL=CredentialResponseUtil.js.map