UNPKG

outers

Version:

outers - a all in one package for your day to day use

124 lines 7.61 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Middleware_Constant_1 = require("../../Config/Constant/Middleware.Constant"); // Import X-Powered-By Header // Import JWT Method for Token Validation const JWT_method_1 = __importDefault(require("../../JWT/JWT.method")); // Import JWT Method // Import Console from Utilities const Console_log_1 = require("../../Logs/Console.log"); // import Red Console // Import Serve Function const JSON_Response_class_1 = __importDefault(require("../../Response/Class/JSON-Response.class")); // Import JSON Response // Import Status Codes const Code_1 = require("../../StatusCode/Code"); // Import Status Codes // Main Function /** * Middleware function that validates a JWT token. * @param TokenFieldName - The name of the token field in the request headers, body, query, or params. * @param SecretToken - The secret token used to verify the JWT. * @returns The middleware function. */ exports.default = (TokenFieldName, SecretToken, bearerToken, excludingText) => { // Check if User Provided Token Field Name & Secret Token if (!TokenFieldName) { throw new Error("Token Field Name is Required"); // Throw Error if Token Field Name is not Provided } if (!SecretToken) { throw new Error("Secret Token is Required"); // Throw Error if Secret Token is not Provided } // Create A new JWT Instance const JWT_Manager = new JWT_method_1.default(SecretToken); // Create New JWT Instance return (Request, Response, Next) => __awaiter(void 0, void 0, void 0, function* () { // Change Response X-Powered-By Header & Server Header Response.setHeader("X-Powered-By", (0, Middleware_Constant_1.XPoweredBy)()); // Set X-Powered-By Header Response.setHeader("Server", (0, Middleware_Constant_1.ServerName)()); // Set Server Header // Create Response Instances const EmptyToken = new JSON_Response_class_1.default(Response, Code_1.StatusCode.UNAUTHORIZED, "json", `${TokenFieldName} is Required`, false, `${TokenFieldName} is Required to access this route`); // Create New JSON Response Instance for Empty Token const BAD_REQUEST = new JSON_Response_class_1.default(Response, Code_1.StatusCode.BAD_REQUEST, "json", "Bad Request", false, "No headers provided"); // Create New JSON Response Instance const INTERNAL_SERVER_ERROR = new JSON_Response_class_1.default(Response, Code_1.StatusCode.INTERNAL_SERVER_ERROR, "json", "Internal Server Error", false, "Internal Server Error, Please Try Again"); // Create New JSON Response Instance for Internal Server Error const InvalidToken = new JSON_Response_class_1.default(Response, Code_1.StatusCode.UNAUTHORIZED, "json", "Invalid Token", false, "Invalid Token Provided to access this route"); // Create New JSON Response Instance for Invalid Token // Common Data Set to send in Response const CommonResponseData = { requestedUrl: Request.url, requestedMethod: Request.method, requestedBody: Request.body, requestedHeaders: Request.headers, }; // Common Data Set to send in Response // Check if Request has Headers if (!Request.headers) { return BAD_REQUEST.Send(undefined); // Send Response if Headers are not available } try { if (!Request.headers[TokenFieldName]) { if (!Request.body[TokenFieldName]) { if (!Request.query[TokenFieldName]) { if (!Request.params[TokenFieldName]) { return EmptyToken.Send(CommonResponseData); // Send Response if Token is not available } else { const toKenValidation = yield JWT_Manager.decode(String(Request.params[TokenFieldName])); // Verify Token if (toKenValidation.status === "Success") { Next(); // Next Middleware } else if (toKenValidation.status === "Invalid") { return InvalidToken.Send(CommonResponseData); // Send Response if Token is Invalid } } } else { const toKenValidation = yield JWT_Manager.decode(String(Request.query[TokenFieldName])); // Verify Token if (toKenValidation.status === "Success") { Next(); // Next Middleware } else if (toKenValidation.status === "Invalid") { return InvalidToken.Send(CommonResponseData); // Send Response if Token is Invalid } } } else { const toKenValidation = yield JWT_Manager.decode(String(Request.body[TokenFieldName])); // Verify Token if (toKenValidation.status === "Success") { Next(); // Next Middleware } else if (toKenValidation.status === "Invalid") { return InvalidToken.Send(CommonResponseData); // Send Response if Token is Invalid } } } else { if (bearerToken && excludingText) { if (String(Request.headers[TokenFieldName]).includes(excludingText)) { Request.headers[TokenFieldName] = String(Request.headers[TokenFieldName]).replace(excludingText, ""); // Remove excludingText from Token } } else if (bearerToken && !excludingText) { if (String(Request.headers[TokenFieldName]).includes("Bearer")) { Request.headers[TokenFieldName] = String(Request.headers[TokenFieldName]).replace("Bearer", ""); // Remove Bearer from Token } } const toKenValidation = yield JWT_Manager.decode(String(Request.headers[TokenFieldName])); // Verify Token if (toKenValidation.status === "Success") { Next(); // Next Middleware } else if (toKenValidation.status === "Invalid") { return InvalidToken.Send(CommonResponseData); // Send Response if Token is Invalid } } } catch (error) { (0, Console_log_1.red)(error); // Log Error return INTERNAL_SERVER_ERROR.Send(undefined); // Send Response if Internal Server Error } }); }; //# sourceMappingURL=Base.middleware.js.map