UNPKG

@xevolab/jades

Version:

JAdES Digital Signatures compatible with the ETSI TS 119 182-1 Standard

160 lines (159 loc) 5.69 kB
"use strict"; /* * Author : Francesco * Created at: 2024-06-29 21:04 * Edited by : Francesco * Edited at : 2024-06-30 15:03 * * Copyright (c) 2024 Xevolab S.R.L. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Classes var ProtectedHeaders_1 = __importDefault(require("./ProtectedHeaders")); var UnprotectedHeaders_1 = __importDefault(require("./UnprotectedHeaders")); var sign_1 = __importStar(require("../utils/sign")); var crypto_1 = require("crypto"); var Token = /** @class */ (function () { function Token(_claim) { /** * Protected headers of the token * @var {object} */ this.protectedHeader = new ProtectedHeaders_1.default({}); /** * Unprotected headers of the token * @var {object} */ this.header = new UnprotectedHeaders_1.default({}); /** * The signature of the token * @var {Buffer} */ this.signature = Buffer.from(""); // --> Validating payload var claim = typeof _claim === "string" ? _claim : JSON.stringify(_claim); this.claim = Buffer.from(new TextEncoder().encode(claim)).toString("base64url"); } /** * Set the protected headers of the token. * * @param {ProtectedHeaders} headers The protected headers of the token. * * @returns {void} */ Token.prototype.setProtectedHeaders = function (headers) { this.protectedHeader = headers; }; /** * Set the unprotected headers of the token. * * @param {UnprotectedHeaders} headers The unprotected headers of the token. * * @returns {void} */ Token.prototype.setUnprotectedHeaders = function (headers) { this.header = headers; }; /** * Method to use a detached signature for this token. * This will require you to pass a `sigD` header value (validation not yet implemented), and will * also remove the claim from the token in accordance with the detached signature requirements. * * @param {Object} sigD The detached signature object * * @returns {void} */ Token.prototype.setDetachedSignature = function (sigD) { this.protectedHeader.setDetached(sigD); this.claim = ""; }; /** * Method to get the hased value to be signed. * * @param {SignAlg} alg The algorithm to use to sign the token. * * @returns {Buffer} The hashed value to be signed. */ Token.prototype.getHash = function (alg) { return (0, crypto_1.createHash)((0, sign_1.digestAlg)(alg)) .update("".concat(this.protectedHeader.toString(), ".").concat(this.claim)) .digest(); }; /** * Set the signature of the token. * * @param {Buffer} signature The signature of the token. * * @returns */ Token.prototype.setSignature = function (alg, signature) { this.signature = signature; this.protectedHeader.addHeaders({ alg: alg }); }; /** * Sign the token using the specified algorithm and key. * * @param {SignAlg} alg Algorithm to use to sign the token * @param {KeyObject} key Key to use to sign the token * * @return {string} Base64url encoded signature */ Token.prototype.sign = function (alg, key) { // Checking the key type (0, sign_1.checkKeyType)(alg, key); this.protectedHeader.addHeaders({ alg: alg }); // Signing the token this.signature = (0, sign_1.default)(alg, key, Buffer.from("".concat(this.protectedHeader.toString(), ".").concat(this.claim))); return this.signature.toString("base64url"); }; /** * Export the token to a string using in compact serialization. * * @return {string} The token in compact serialization. */ Token.prototype.toString = function () { return "".concat(this.protectedHeader.toString(), ".").concat(this.claim, ".").concat(this.signature.toString("base64url")); }; /** * Export the token to an object. * * @return {object} The token in object form. */ Token.prototype.toObject = function () { return { protected: this.protectedHeader.toString(), header: this.header.toString(), payload: this.claim, signature: this.signature.toString("base64url"), }; }; return Token; }()); exports.default = Token; ;