@guarani/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
42 lines (41 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebEncryptionKeyWrapAlgorithm = void 0;
const invalid_json_web_key_exception_1 = require("../../../exceptions/invalid-json-web-key.exception");
const jsonwebkey_1 = require("../../../jwk/jsonwebkey");
/**
* Abstract Base Class for {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-4 RFC 7518 Section 4}.
*
* All JSON Web Encryption Key Wrap Algorithms supported by Guarani **MUST** extend this base class
* and implement its abstract methods.
*/
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_json_web_key_exception_1.InvalidJsonWebKeyException();
}
if (key.alg !== undefined && key.alg !== this.algorithm) {
throw new invalid_json_web_key_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_json_web_key_exception_1.InvalidJsonWebKeyException(`This JSON Web Encryption Key Wrap Algorithm only accepts "${this.keyType}" JSON Web Keys.`);
}
}
}
exports.JsonWebEncryptionKeyWrapAlgorithm = JsonWebEncryptionKeyWrapAlgorithm;