UNPKG

@guarani/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

44 lines (43 loc) 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonWebSignatureAlgorithm = 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-3 RFC 7518 Section 3}. * * All JSON Web Signature Algorithms supported by Guarani **MUST** extend this base class * and implement its abstract methods. */ class JsonWebSignatureAlgorithm { /** * Instantiates a new JSON Web Signature Algorithm to Sign and Verify the Messages. * * @param hash Hash Algorithm used to Sign and Verify the Messages. * @param algorithm Name of the JSON Web Signature Algorithm. * @param keyType Type of JSON Web Key supported by this JSON Web Signature Algorithm. */ constructor(hash, algorithm, keyType) { this.hash = hash; this.algorithm = algorithm; 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_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 (this.keyType !== undefined && key.kty !== this.keyType) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException(`This JSON Web Signature Algorithm only accepts "${this.keyType}" JSON Web Keys.`); } } } exports.JsonWebSignatureAlgorithm = JsonWebSignatureAlgorithm;