@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
43 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebEncryptionKeyWrapAlgorithm = void 0;
const invalid_jsonwebkey_exception_1 = require("../../../exceptions/invalid-jsonwebkey.exception");
const jsonwebkey_1 = require("../../../jwk/jsonwebkey");
/**
* Abstract Base Class for the JSON Web Encryption Key Wrap Algorithm.
*
* All JSON Web Encryption Key Wrap Algorithms **MUST** extend this base class and implement its abstract methods.
*
* @see https://www.rfc-editor.org/rfc/rfc7518.html#section-4
*/
class JsonWebEncryptionKeyWrapAlgorithm {
/**
* Instantiates a new JSON Web Encryption Key Wrap Algorithm to Wrap and Unwrap Content Encryption Keys.
*
* @param algorithm Name of the JSON Web Encryption Key Wrap Algorithm.
* @param keyType Type of JSON Web Key supported by this JSON Web Encryption Key Wrap Algorithm.
*/
constructor(algorithm, keyType) {
this.algorithm = algorithm;
this.keyType = keyType;
}
/**
* Checks if the provided JSON Web Key can be used by the requesting JSON Web Encryption Key Wrap Algorithm.
*
* @param key JSON Web Key to be checked.
* @throws {InvalidJsonWebKeyException} The provided JSON Web Key is invalid.
*/
validateJsonWebKey(key) {
if (!(key instanceof jsonwebkey_1.JsonWebKey)) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException();
}
if (key.alg !== undefined && key.alg !== this.algorithm) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(`This JSON Web Key is intended to be used by the Algorithm "${key.alg}".`);
}
if (key.kty !== this.keyType) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(`This JSON Web Encryption Key Wrap Algorithm only accepts "${this.keyType}" JSON Web Keys.`);
}
}
}
exports.JsonWebEncryptionKeyWrapAlgorithm = JsonWebEncryptionKeyWrapAlgorithm;
//# sourceMappingURL=jsonwebencryption-key-wrap.algorithm.js.map