@riocrypto/common-server 1.0.2590 → 1.0.2591
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from "./middlewares/require-min-role-or-permission";
|
|
|
8
8
|
export * from "./middlewares/require-min-admin-role";
|
|
9
9
|
export * from "./middlewares/verify-csrf-token";
|
|
10
10
|
export * from "./middlewares/dynamic-rate-limiter";
|
|
11
|
+
export * from "./middlewares/auth-context";
|
|
11
12
|
export * from "./services/apiKey";
|
|
12
13
|
export * from "./services/password";
|
|
13
14
|
export * from "./services/logger";
|
package/build/index.js
CHANGED
|
@@ -24,6 +24,7 @@ __exportStar(require("./middlewares/require-min-role-or-permission"), exports);
|
|
|
24
24
|
__exportStar(require("./middlewares/require-min-admin-role"), exports);
|
|
25
25
|
__exportStar(require("./middlewares/verify-csrf-token"), exports);
|
|
26
26
|
__exportStar(require("./middlewares/dynamic-rate-limiter"), exports);
|
|
27
|
+
__exportStar(require("./middlewares/auth-context"), exports);
|
|
27
28
|
__exportStar(require("./services/apiKey"), exports);
|
|
28
29
|
__exportStar(require("./services/password"), exports);
|
|
29
30
|
__exportStar(require("./services/logger"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Express {
|
|
4
|
+
interface Request {
|
|
5
|
+
adminContext?: {
|
|
6
|
+
adminId: string;
|
|
7
|
+
adminEmail: string;
|
|
8
|
+
adminName: string;
|
|
9
|
+
};
|
|
10
|
+
userContext?: {
|
|
11
|
+
userId: string;
|
|
12
|
+
phoneNumber: string;
|
|
13
|
+
userName: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export declare const authContextMiddleware: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.authContextMiddleware = void 0;
|
|
16
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
17
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
18
|
+
const common_1 = require("@riocrypto/common");
|
|
19
|
+
const secret_manager_client_1 = require("../clients/secret-manager-client");
|
|
20
|
+
const admin_auth_1 = require("../models/admin-auth");
|
|
21
|
+
const auth_1 = require("../models/auth");
|
|
22
|
+
let cachedAdminSecret = null;
|
|
23
|
+
let cachedUserSecret = null;
|
|
24
|
+
const authContextMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
const { adminAccessToken, accessToken } = req.cookies;
|
|
26
|
+
if (adminAccessToken) {
|
|
27
|
+
try {
|
|
28
|
+
if (!cachedAdminSecret) {
|
|
29
|
+
cachedAdminSecret = yield secret_manager_client_1.secretManagerClient.getSecretValue("ADMIN_ACCESS_TOKEN_SECRET");
|
|
30
|
+
}
|
|
31
|
+
if (cachedAdminSecret) {
|
|
32
|
+
const decoded = jsonwebtoken_1.default.verify(adminAccessToken, cachedAdminSecret);
|
|
33
|
+
const AdminAuth = (0, admin_auth_1.buildAdminAuth)(mongoose_1.default);
|
|
34
|
+
const adminAuth = yield AdminAuth.findById(decoded.id);
|
|
35
|
+
if (adminAuth) {
|
|
36
|
+
req.adminContext = {
|
|
37
|
+
adminId: decoded.id,
|
|
38
|
+
adminEmail: decoded.email,
|
|
39
|
+
adminName: (0, common_1.getAuthName)(adminAuth.toJSON()),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (error) { }
|
|
45
|
+
}
|
|
46
|
+
if (accessToken) {
|
|
47
|
+
try {
|
|
48
|
+
if (!cachedUserSecret) {
|
|
49
|
+
cachedUserSecret = yield secret_manager_client_1.secretManagerClient.getSecretValue("ACCESS_TOKEN_SECRET");
|
|
50
|
+
}
|
|
51
|
+
if (cachedUserSecret) {
|
|
52
|
+
const decoded = jsonwebtoken_1.default.verify(accessToken, cachedUserSecret);
|
|
53
|
+
const Auth = (0, auth_1.buildAuth)(mongoose_1.default);
|
|
54
|
+
const auth = yield Auth.findById(decoded.id);
|
|
55
|
+
if (auth) {
|
|
56
|
+
req.userContext = {
|
|
57
|
+
userId: decoded.id,
|
|
58
|
+
phoneNumber: decoded.phoneNumber,
|
|
59
|
+
userName: (0, common_1.getAuthName)(auth.toJSON()),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (error) { }
|
|
65
|
+
}
|
|
66
|
+
next();
|
|
67
|
+
});
|
|
68
|
+
exports.authContextMiddleware = authContextMiddleware;
|