@xevolab/jades
Version:
JAdES Digital Signatures compatible with the ETSI TS 119 182-1 Standard
132 lines (131 loc) • 4.37 kB
JavaScript
/*
* 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.
*/
// Classes
import ProtectedHeaders from "./ProtectedHeaders";
import UnprotectedHeaders from "./UnprotectedHeaders";
import calculateSignature, { checkKeyType, digestAlg } from "../utils/sign";
import { createHash } from "crypto";
var Token = /** @class */ (function () {
function Token(_claim) {
/**
* Protected headers of the token
* @var {object}
*/
this.protectedHeader = new ProtectedHeaders({});
/**
* Unprotected headers of the token
* @var {object}
*/
this.header = new UnprotectedHeaders({});
/**
* 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 createHash(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
checkKeyType(alg, key);
this.protectedHeader.addHeaders({ alg: alg });
// Signing the token
this.signature = calculateSignature(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;
}());
export default Token;
;