@sendhome/common 1.0.152 → 1.0.154
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
interface UserPayload {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Express {
|
|
8
|
+
interface Request {
|
|
9
|
+
currentUser?: UserPayload;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.requireAuth = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
9
|
+
const requireAuth = (req, res, next) => {
|
|
10
|
+
let token;
|
|
11
|
+
if (req.headers.authorization &&
|
|
12
|
+
req.headers.authorization.startsWith("Bearer")) {
|
|
13
|
+
// Set token from Bearer token in header
|
|
14
|
+
token = req.headers.authorization.split(" ")[1];
|
|
15
|
+
}
|
|
16
|
+
// Make sure token exists
|
|
17
|
+
if (!token) {
|
|
18
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const payload = jsonwebtoken_1.default.verify(token, "EDWIN"
|
|
22
|
+
// process.env.JWT_KEY!
|
|
23
|
+
);
|
|
24
|
+
req.currentUser = payload;
|
|
25
|
+
}
|
|
26
|
+
catch (err) { }
|
|
27
|
+
next();
|
|
28
|
+
};
|
|
29
|
+
exports.requireAuth = requireAuth;
|
|
@@ -10,5 +10,5 @@ declare global {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void
|
|
13
|
+
export declare const requireAuth: (sessionServiceUrl: string, deviceInfo: string) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
14
14
|
export {};
|
|
@@ -1,30 +1,48 @@
|
|
|
1
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
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
5
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
15
|
exports.requireAuth = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
17
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
|
|
9
|
-
const requireAuth = (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
// Middleware to validate user session per device
|
|
19
|
+
const requireAuth = (sessionServiceUrl, deviceInfo) => {
|
|
20
|
+
return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
var _a;
|
|
22
|
+
try {
|
|
23
|
+
const token = req.cookies.token || ((_a = req.headers.authorization) === null || _a === void 0 ? void 0 : _a.split(' ')[1]);
|
|
24
|
+
if (!token) {
|
|
25
|
+
res.status(401).json({ message: 'No session token provided' });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const payload = jsonwebtoken_1.default.verify(token, "EDWIN"
|
|
29
|
+
// process.env.JWT_KEY!
|
|
30
|
+
);
|
|
31
|
+
// Call the session service to validate the session
|
|
32
|
+
const response = yield axios_1.default.get(`${sessionServiceUrl}/${payload.id}`, {
|
|
33
|
+
params: { device_info: deviceInfo }, // Send the device info for validation
|
|
34
|
+
});
|
|
35
|
+
if (response.status !== 200) {
|
|
36
|
+
res.status(401).json({ message: 'Invalid or expired session' });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// Attach user ID to request object
|
|
40
|
+
req.currentUser = response.data;
|
|
41
|
+
next();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
res.status(500).json({ message: 'Server error during session validation', error });
|
|
45
|
+
}
|
|
46
|
+
});
|
|
29
47
|
};
|
|
30
48
|
exports.requireAuth = requireAuth;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sendhome/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.154",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"@types/express": "^4.17.13",
|
|
25
25
|
"@types/jsonwebtoken": "^8.5.8",
|
|
26
26
|
"@types/mongoose": "^5.11.97",
|
|
27
|
+
"axios": "^1.7.7",
|
|
27
28
|
"cookie-session": "^2.0.0",
|
|
28
29
|
"express": "^4.17.2",
|
|
29
30
|
"express-validator": "^6.14.0",
|