@sphereon/gx-compliance-client
Version:
<!--suppress HtmlDeprecatedAttribute --> <h1 align="center"> <br> <a href="https://www.sphereon.com"><img src="https://sphereon.com/content/themes/sphereon/assets/img/logo.svg" alt="Sphereon" width="400"></a> <br>Gaia-X Compliance client (Typescript
85 lines (84 loc) • 4.08 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 _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebKey = void 0;
const jose_ld_1 = require("@transmute/jose-ld");
const web_crypto_key_pair_1 = require("@transmute/web-crypto-key-pair");
/**
* WARNING:
*
* This suite is made specifically to be interoperable with Gaia-X. Do not use this suite for other purposes, as the current Gaia-X implementation contains multiple errors and does not conform to JsonWebSignature2020.
* If you do need regular JsonWebSignature2020 support, please configure the SphereonWebSignature2020 class when setting up the agent
*/
const getKeyPairForKtyAndCrv = (kty, crv) => {
if (kty === 'RSA') {
return web_crypto_key_pair_1.WebCryptoKey;
}
throw new Error(`getKeyPairForKtyAndCrv does not support: ${kty} and ${crv}`);
};
const getKeyPairForType = (k) => {
if (k.type === 'JsonWebKey2020') {
return getKeyPairForKtyAndCrv(k.publicKeyJwk.kty, k.publicKeyJwk.crv);
}
if (k.type === 'RSAVerificationKey2018') {
return web_crypto_key_pair_1.WebCryptoKey;
}
throw new Error('getKeyPairForType does not support type: ' + k.type);
};
const getVerifier = (k, options = { detached: true }) => __awaiter(void 0, void 0, void 0, function* () {
const { publicKeyJwk } = yield k.export({ type: 'JsonWebKey2020' });
const { kty } = publicKeyJwk;
if (kty === 'RSA') {
// @ts-ignore
return jose_ld_1.JWS.createVerifier(k.verifier('RSA'), 'RS256', options);
}
throw new Error(`getVerifier does not support ${JSON.stringify(publicKeyJwk, null, 2)}`);
});
const getSigner = (k, options = { detached: true }) => __awaiter(void 0, void 0, void 0, function* () {
const { publicKeyJwk } = yield k.export({ type: 'JsonWebKey2020' });
const { kty } = publicKeyJwk;
if (kty === 'RSA') {
// @ts-ignore
return jose_ld_1.JWS.createSigner(k.signer('RSA'), 'RS256', options);
}
throw new Error(`getSigner does not support ${JSON.stringify(publicKeyJwk, null, 2)}`);
});
const applyJwa = (k, options) => __awaiter(void 0, void 0, void 0, function* () {
const verifier = (options === null || options === void 0 ? void 0 : options.verifier) !== undefined ? options.verifier : yield getVerifier(k, options);
k.verifier = () => verifier;
if (k.privateKey || (options === null || options === void 0 ? void 0 : options.signer) !== undefined) {
const signer = (options === null || options === void 0 ? void 0 : options.signer) !== undefined ? options.signer : yield getSigner(k, options);
k.signer = () => signer;
}
return k;
});
// this is dirty...
const useJwa = (k, options) => __awaiter(void 0, void 0, void 0, function* () {
// before mutation, annotate the apply function....
k.useJwa = (options) => __awaiter(void 0, void 0, void 0, function* () {
return applyJwa(k, options);
});
return applyJwa(k, options);
});
class JsonWebKey {
}
exports.JsonWebKey = JsonWebKey;
_a = JsonWebKey;
JsonWebKey.from = (k, options = { detached: true }) => __awaiter(void 0, void 0, void 0, function* () {
const KeyPair = getKeyPairForType(k);
const kp = yield KeyPair.from(k);
let { detached, header, signer, verifier } = options;
if (detached === undefined) {
detached = true;
}
return useJwa(kp, { detached, header, signer, verifier });
});