@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
116 lines • 5.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebKey = void 0;
const invalid_jsonwebkey_exception_1 = require("../exceptions/invalid-jsonwebkey.exception");
const jose_exception_1 = require("../exceptions/jose.exception");
const unsupported_algorithm_exception_1 = require("../exceptions/unsupported-algorithm.exception");
const eckey_algorithm_1 = require("./algorithms/eckey.algorithm");
const octkey_algorithm_1 = require("./algorithms/octkey.algorithm");
const rsakey_algorithm_1 = require("./algorithms/rsakey.algorithm");
/**
* Implementation of a JSON Web Key.
*
* @see https://www.rfc-editor.org/rfc/rfc7517.html#section-4
*/
class JsonWebKey {
/**
* Instantiates a new JSON Web Key based on the provided Parameters.
*
* @param parameters Parameters of the JSON Web Key.
* @param additionalParameters Additional JSON Web Key Parameters. Overrides the attributes of `parameters`.
*/
constructor(parameters, additionalParameters = {}) {
const params = { ...parameters, ...additionalParameters };
if (params.kty === undefined) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "kty".');
}
let algorithm;
if ((algorithm = JsonWebKey.algorithms[params.kty]) === undefined) {
throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Key Algorithm "${params.kty}".`);
}
if (params.use !== undefined && typeof params.use !== 'string') {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "use".');
}
if (params.key_ops !== undefined) {
if (!Array.isArray(params.key_ops) ||
params.key_ops.length === 0 ||
params.key_ops.some((p) => typeof p !== 'string')) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "key_ops".');
}
if (new Set(params.key_ops).size !== params.key_ops.length) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Key parameter "key_ops" cannot have repeated operations.');
}
}
if (params.use !== undefined && params.key_ops !== undefined) {
const sig = ['sign', 'verify'];
const enc = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey', 'deriveKey', 'deriveBits'];
if ((params.use === 'sig' && params.key_ops.some((p) => !sig.includes(p))) ||
(params.use === 'enc' && params.key_ops.some((p) => !enc.includes(p)))) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid combination of "use" and "key_ops".');
}
}
if (params.alg !== undefined && typeof params.alg !== 'string') {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "alg".');
}
if (params.kid !== undefined && typeof params.kid !== 'string') {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "kid".');
}
if (params.x5u !== undefined) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Unsupported key parameter "x5u".');
}
if (params.x5c !== undefined) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Unsupported key parameter "x5c".');
}
if (params.x5t !== undefined) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Unsupported key parameter "x5t".');
}
if (params['x5t#S256'] !== undefined) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Unsupported key parameter "x5t#S256".');
}
try {
algorithm.validateParameters(params);
}
catch (exc) {
throw exc instanceof jose_exception_1.JoseException ? exc : new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(null, exc);
}
Object.defineProperty(this, 'cryptoKey', { value: algorithm.loadCryptoKey(params) });
Object.assign(this, params);
}
/**
* Parses a JSON String into a JSON Web Key.
*
* @param data JSON String representation of the JSON Web Key to be parsed.
* @returns Instance of a JSON Web Key based on the provided JSON String.
*/
static parse(data) {
try {
return new JsonWebKey(JSON.parse(data));
}
catch (exc) {
throw exc instanceof jose_exception_1.JoseException ? exc : new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(null, exc);
}
}
/**
* Returns the Parameters of the JSON Web Key.
*
* @param exportPublic Exports only the Public Parameters of the JSON Web Key.
*/
toJSON(exportPublic = true) {
if (!exportPublic) {
return this;
}
const { privateParameters } = JsonWebKey.algorithms[this.kty];
const publicParameters = Object.keys(this).filter((key) => !privateParameters.includes(key));
return Object.fromEntries(publicParameters.map((key) => [key, Reflect.get(this, key)]));
}
}
exports.JsonWebKey = JsonWebKey;
/**
* Supported JSON Web Key Algorithms.
*/
JsonWebKey.algorithms = {
EC: new eckey_algorithm_1.EcKeyAlgorithm(),
RSA: new rsakey_algorithm_1.RsaKeyAlgorithm(),
oct: new octkey_algorithm_1.OctKeyAlgorithm(),
};
//# sourceMappingURL=jsonwebkey.js.map