UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

45 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonWebSignatureAlgorithm = 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 Signature Algorithm. * * All JSON Web Signature Algorithms **MUST** extend this base class and implement its abstract methods. * * @see https://www.rfc-editor.org/rfc/rfc7518.html#section-3 */ class JsonWebSignatureAlgorithm { /** * Instantiates a new JSON Web Signature Algorithm to Sign and Verify Messages. * * @param algorithm Name of the JSON Web Signature Algorithm. * @param hash Hash Algorithm used to Sign and Verify Messages. * @param keyType Type of JSON Web Key supported by this JSON Web Signature Algorithm. */ constructor(algorithm, hash, keyType) { this.algorithm = algorithm; this.hash = hash; this.keyType = keyType; } /** * Checks if the provided JSON Web Key can be used by the requesting JSON Web Signature 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 (this.keyType !== undefined && key.kty !== this.keyType) { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(`This JSON Web Signature Algorithm only accepts "${this.keyType}" JSON Web Keys.`); } } } exports.JsonWebSignatureAlgorithm = JsonWebSignatureAlgorithm; //# sourceMappingURL=jsonwebsignature.algorithm.js.map