UNPKG

@guarani/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

69 lines (68 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonWebKey = void 0; const objects_1 = require("@guarani/objects"); const invalid_json_web_key_exception_1 = require("../exceptions/invalid-json-web-key.exception"); /** * Implementation of {@link https://www.rfc-editor.org/rfc/rfc7517.html#section-4 RFC 7517 Section 4}. */ class JsonWebKey { /** * Instantiates a new JSON Web Key based on the provided Parameters. * * @param params Parameters of the JSON Web Key. */ constructor(params = {}) { if (params.use !== undefined && typeof params.use !== 'string') { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid 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_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid parameter "key_ops".'); } if (new Set(params.key_ops).size !== params.key_ops.length) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('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_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid combination of "use" and "key_ops".'); } } if (params.alg !== undefined && typeof params.alg !== 'string') { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid parameter "alg".'); } if (params.kid !== undefined && typeof params.kid !== 'string') { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid parameter "kid".'); } if (params.x5u !== undefined) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Unsupported parameter "x5u".'); } if (params.x5c !== undefined) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Unsupported parameter "x5c".'); } if (params.x5t !== undefined) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Unsupported parameter "x5t".'); } if (params['x5t#256'] !== undefined) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Unsupported parameter "x5t#256".'); } const cryptoKey = this.loadCryptoKey(params); Object.defineProperty(this, 'cryptoKey', { value: cryptoKey }); Object.assign(this, (0, objects_1.removeNullishValues)(params)); } /** * Checks if the provided data conforms to the JSON Web Key Specification. * * @param data Data to be checked. */ static isJsonWebKey(data) { return typeof data === 'object' && typeof data.kty === 'string'; } } exports.JsonWebKey = JsonWebKey;