outers
Version:
outers - a all in one package for your day to day use
254 lines • 13.2 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-await-in-loop */
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _Jwt_signatureKey, _Jwt_cipherList;
Object.defineProperty(exports, "__esModule", { value: true });
const Console_log_1 = require("../Logs/Console.log"); // Importing from outers for coloring the output
const jsonwebtoken_1 = require("jsonwebtoken"); // Importing jsonwebtoken for generating the token
const JWT_Constant_1 = require("../Config/Constant/JWT.Constant"); // Importing todayDate from JWT.Constant
// Import Functions
const Generate_1 = __importDefault(require("./Functions/Generate")); // Importing GenerateJWT from Functions
const Destroy_1 = __importDefault(require("./Functions/Destroy")); // Importing DestroyJWT from functions
const verifyCipher_1 = __importDefault(require("./Functions/verifyCipher")); // Importing verifyCipher from functions
// Class for all features
/* The Jwt class is used to generate a token with a payload and optional expiry time. */
class Jwt {
/**
* The constructor function initializes the signatureKey property with the provided value or a default
* value of 'secret'.
* @param {string} signatureKey - The `signatureKey` parameter is a string that represents the key used
* for signing or verifying signatures. It is an optional parameter, meaning it can be provided or
* omitted when creating an instance of the class. If no value is provided, the default value is set to
* `'secret'`.
*/
constructor(signatureKey) {
// Token
_Jwt_signatureKey.set(this, void 0); // Signature Key for the token
_Jwt_cipherList.set(this, void 0); // List of ciphers used to destroy the token
__classPrivateFieldSet(this, _Jwt_signatureKey, signatureKey !== null && signatureKey !== void 0 ? signatureKey : "secret", "f");
__classPrivateFieldSet(this, _Jwt_cipherList, JWT_Constant_1.cipherList, "f"); // List of ciphers used to destroy the token
}
// Generate the token
/**
* The function generates a token using a payload and an optional expiry time, and returns a record
* containing information about the generated token.
* @param {any} Payload - The `Payload` parameter is of type `unknown`, which means it can accept
* any type of data. It represents the data that you want to include in the token.
* @param [expiry=1h] - The `expiry` parameter is an optional parameter that specifies the expiration
* time for the generated token. It is set to a default value of '1h', which means the token will
* expire after 1 hour.
* @returns a Promise that resolves to a Record<string, unknown> object.
*/
generate(Payload, expiry = "1h") {
try {
if (!Payload) {
(0, Console_log_1.red)("Payload is required"); // Log the error
return {
status: "Empty",
message: "Payload is required",
algoRithm: "HS256 (Default)",
currentTimeStamp: JWT_Constant_1.todayDate,
}; // Return the error
}
// Generate the token
const signedData = (0, Generate_1.default)(Payload, __classPrivateFieldGet(this, _Jwt_signatureKey, "f"), expiry); // Generate the token
// Check if the token is valid
if (signedData == null) {
return {
status: "error",
message: "Something went wrong when generating the token",
algoRithm: "HS256 (Default)",
currentTimeStamp: JWT_Constant_1.todayDate,
};
}
return {
status: "Success",
message: "Token generated successfully",
toKen: signedData,
algoRithm: "HS256 (Default)",
expiry,
currentTimeStamp: JWT_Constant_1.todayDate,
}; // Create a result object
}
catch (_a) {
(0, Console_log_1.red)("Error generating token"); // Log the error
return {
status: "error",
message: "Error generating token",
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Create an error object
}
}
/**
* The function generates a login token by repeatedly calling a token generation function for a
* specified number of rounds.
* @param {unknown} Payload - The `Payload` parameter is of type `unknown` and represents the data that
* will be used to generate the login token. It can be any type of data, such as an object or a string.
* @param [Rounds=5] - The "Rounds" parameter determines the number of times the token generation
* process will be repeated. Each round generates a new token based on the previous token. The default
* value is 5 rounds.
* @param [expiry=1h] - The `expiry` parameter is a string that represents the expiration time of the
* generated token. It is set to a default value of '1h', which means the token will expire after 1
* hour.
* @returns an object with the following properties:
* - status: a boolean indicating whether the token was generated successfully or not.
* - message: a string message indicating the result of the token generation.
* - toKen: the generated token.
* - algoRithm: a string indicating the algorithm used for token generation (HS256 in this case).
* - expiry: a string indicating the expiration time of
*/
generateLoginToken(Payload, Rounds = 5, expiry = "1h") {
try {
let daTa = Payload; // Set the data to the payload
let tiMes = Rounds; // Set the times to the rounds
do {
const result = this.generate(daTa, expiry); // Destroy the token
daTa = result.toKen; // Set the data to the token
tiMes--; // Reduce the times
} while (tiMes > 0);
return {
status: true,
message: "Token generated successfully",
toKen: daTa,
algoRithm: "HS256 (Default)",
expiry,
currentTimeStamp: JWT_Constant_1.todayDate,
}; // Return the result
}
catch (_a) {
(0, Console_log_1.red)("Error generating token"); // Log the error
return {
status: false,
message: "Error generating login token",
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Return the error
}
}
// Destroy the token
/**
* The `destroy` function takes a token, adds ciphers to specific positions, reverses the token, and
* returns a result object with the modified token and other information.
* @param {string} token - The `token` parameter is a string that represents a token.
* @returns The `destroy` function returns a Promise that resolves to a `Record<string, unknown>`
* object.
*/
destroy(token) {
try {
const modifiedToken = (0, Destroy_1.default)(token, __classPrivateFieldGet(this, _Jwt_cipherList, "f")); // Destroy the token
return {
status: "Successfully destroyed",
message: "Token destroyed successfully",
token: modifiedToken,
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Create a result object; // Return the result
}
catch (_a) {
return {
status: "error",
message: "Error destroying token",
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Create an error object
}
}
/**
* The function decodes a token by verifying its cipher and checking if it is valid, returning an error
* object if it is empty, destroyed, or invalid.
* @param {string} token - The `token` parameter is a string that represents a token that needs to be
* decoded.
* @returns The function `decode` returns a Promise that resolves to an unknown value. The value being
* returned depends on the conditions inside the function.
*/
decode(token) {
try {
if (!token) {
return {
status: "empty",
message: "Token is required",
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Create an error object
}
const cipherResult = (0, verifyCipher_1.default)(token, __classPrivateFieldGet(this, _Jwt_cipherList, "f")); // Verify the cipher
if (cipherResult.status === "Already Destroyed") {
return cipherResult;
}
// Check if the token is destroyed by manually checking the token
const resultData = (0, jsonwebtoken_1.verify)(token, __classPrivateFieldGet(this, _Jwt_signatureKey, "f")); // Decode the token
return {
status: "Success",
message: "Token decoded successfully",
data: resultData,
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Return the result
}
catch (_a) {
return {
status: "Invalid",
message: "Invalid Token Provided, token might have been tampered, not match the signature key or expired",
currentTimeStamp: JWT_Constant_1.todayDate,
algoRithm: "HS256 (Default)",
}; // Create an error object
}
}
/**
* Sets or updates the cipher list.
*
* This method updates the internal cipher list used for cryptographic operations.
* It validates the input to ensure that it is a non-empty array of strings. If the
* input does not meet these criteria, an error is thrown.
*
* @param {string[]} cipherList - An array of strings representing the new cipher list.
* @throws {Error} Throws an error if `cipherList` is not provided or is not an array.
*/
setCipherList(cipherList) {
if (!cipherList) {
throw new Error("Cipher list is required to update the cipher list"); // Return the error
}
if (!Array.isArray(cipherList)) {
throw new Error("Cipher list should be an array"); // Return the error
}
// Update the cipher list
__classPrivateFieldSet(this, _Jwt_cipherList, cipherList, "f"); // Update the cipher list
}
/**
* Sets the signature key used for signing or verifying.
* This method updates the instance's signature key with the provided value.
*
* @param {string} signatureKey - The new signature key to be set. Must be a non-empty string.
* @throws Will throw an error if `signatureKey` is not provided or if it is not a string.
*/
setSignatureKey(signatureKey) {
// Check if the signature key is provided
if (!signatureKey) {
throw new Error("Signature key is required to update the signature key"); // Return the error
}
// Check if the signature key is a string
if (typeof signatureKey !== "string") {
throw new Error("Signature key should be a string"); // Return the error
}
// Update the signature key
__classPrivateFieldSet(this, _Jwt_signatureKey, signatureKey, "f"); // Update the signature key
}
}
_Jwt_signatureKey = new WeakMap(), _Jwt_cipherList = new WeakMap();
exports.default = Jwt;
//# sourceMappingURL=JWT.method.js.map