authorization-z
Version:
`Authorization-Z` is a comprehensive Express middleware package for validating JWT Authorization-Z tokens, attaching permissions to requests, verifying permissions, and granting access accordingly. This package provides a robust solution for implementing
81 lines (80 loc) • 3.95 kB
JavaScript
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsJWTAuthorizationZTokenValid = void 0;
const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
const AppError_1 = require("../errors/AppError");
const messages_1 = require("../constants/messages");
const IsJWTAuthorizationZTokenValid = (secret) => {
return (req, res, next) => {
var _a, _b;
try {
if (req === null || req === void 0 ? void 0 : req.isAdmin) {
return next();
}
if (!secret || typeof secret !== "string" || secret.trim() === "") {
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "AUTHORIZATION_Z_SECRET_MISSING"), 500);
}
const token = (_b = (_a = req.headers["authorization-z"]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : null;
if (!token) {
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "AUTHORIZATION_Z_TOKEN_MISSING"), 401);
}
let decoded = null;
try {
decoded = jsonwebtoken_1.default.verify(token, secret);
}
catch (error) {
if (error instanceof jsonwebtoken_1.TokenExpiredError) {
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "AUTHORIZATION_Z_TOKEN_EXPIRED"), 401);
}
if (error instanceof jsonwebtoken_1.JsonWebTokenError) {
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "INVALID_AUTHORIZATION_Z_TOKEN"), 401);
}
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "AUTHORIZATION_Z_TOKEN_VERIFICATION_FAILED"), 401);
}
if (!decoded.permissions || typeof decoded.permissions !== "object" || Array.isArray(decoded.permissions)) {
throw new AppError_1.AppError((0, messages_1.getMessage)(req.lang, "errors", "VALID_PERMISSIONS_REQUIRED"), 400);
}
req.permissions = decoded.permissions;
return next();
}
catch (error) {
const statusCode = error instanceof AppError_1.AppError ? error.statusCode : 500;
const message = error instanceof AppError_1.AppError ? error.message : "Internal server error";
res.status(statusCode).json({ message });
}
};
};
exports.IsJWTAuthorizationZTokenValid = IsJWTAuthorizationZTokenValid;
;