UNPKG

@guarani/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

82 lines (81 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OctKey = void 0; const crypto_1 = require("crypto"); const util_1 = require("util"); const invalid_json_web_key_exception_1 = require("../../../exceptions/invalid-json-web-key.exception"); const unsupported_algorithm_exception_1 = require("../../../exceptions/unsupported-algorithm.exception"); const jsonwebkey_1 = require("../../jsonwebkey"); const randomBytesAsync = (0, util_1.promisify)(crypto_1.randomBytes); /** * Implementation of {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-6.4 RFC 7518 Section 6.4}. */ class OctKey extends jsonwebkey_1.JsonWebKey { /** * Instantiates an Octet Sequence JSON Web Key based on the provided Parameters. * * @param key Parameters of the Octet Sequence JSON Web Key. * @param options Optional JSON Web Key Parameters. */ constructor(key, options = {}) { if (key instanceof OctKey) { return key; } const params = { ...key, ...options }; if (typeof params.kty !== 'string') { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid parameter "kty".'); } if (params.kty !== 'oct') { throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Invalid JSON Web Key Type. Expected "EC", got "${params.kty}".`); } if (typeof params.k !== 'string') { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('Invalid key parameter "k".'); } if (Buffer.from(params.k, 'base64url').length === 0) { throw new invalid_json_web_key_exception_1.InvalidJsonWebKeyException('The Secret cannot be empty.'); } super(params); } /** * Generates a new Octet Sequence JSON Web Key. * * @param options Options for the generation of the Octet Sequence JSON Web Key. * @param params Optional JSON Web Key Parameters. * @returns Generated Octet Sequence JSON Web Key. */ static async generate(options, params = {}) { const { size } = options; if (!Number.isInteger(size)) { throw new TypeError('The Secret size must be a valid integer.'); } if (size < 1) { throw new Error('The Secret size must be a positive integer.'); } const buffer = await randomBytesAsync(size); return new OctKey({ kty: 'oct', k: buffer.toString('base64url') }, params); } /** * Loads the provided JSON Web Key into a NodeJS Crypto Key. * * @param params Parameters of the JSON Web Key. * @returns NodeJS Crypto Key. */ loadCryptoKey(params) { return (0, crypto_1.createSecretKey)(params.k, 'base64url'); } /** * Exports the data of the Octet Sequence JSON Web Key. * * @param options Options for exporting the data of the Octet Sequence JSON Web Key. * @returns Resulting Object. */ export(options) { const { encoding } = options; if (typeof encoding !== 'string') { throw new TypeError('Invalid option "encoding".'); } const secret = this.cryptoKey.export(); return encoding === 'buffer' ? secret : secret.toString(encoding); } } exports.OctKey = OctKey;