Skip to content

Commit 73c3288

Browse files
committed
added custom token remote user
1 parent 16af0f8 commit 73c3288

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

_apidoc.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
* @apiError (Error 500) err Error in updating database
291291
*
292292
*/
293-
293+
294294
/**
295295
* @api {get} accreditation/list Get Accreditation List
296296
* @apiName GetAccreditation
@@ -307,4 +307,3 @@
307307
* @apiSuccess {Date} accreditation.dateofAccreditation Date on which accreditation was issued.
308308
* @apiSuccess {Date} accreditation.dateofExpiry Date till which accreditation is valid.
309309
*/
310-

app.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ import usersRouter from "#routes/users";
1010
import authRouter from "#routes/auth";
1111
import accreditationRouter from "#routes/accreditation";
1212
import infrastructureRouter from "#routes/infrastructure";
13+
import { identifyUser } from "#middleware/identifyUser";
1314

1415
const app = express();
1516
const currDirName = dirname(fileURLToPath(import.meta.url));
1617

18+
morgan.token("remote-user", (req) => req.user);
19+
app.use(identifyUser);
20+
app.use(cors());
21+
app.use(express.json());
22+
app.use(express.urlencoded({ extended: false }));
23+
app.use(cookieParser());
1724
app.use(morgan(
1825
":remote-addr - :remote-user \":method :url HTTP/:http-version\" :status \":referrer\" \":user-agent\"",
1926
{ stream: logger.stream },
2027
));
2128

22-
app.use(cors());
23-
app.use(express.json());
24-
app.use(express.urlencoded({ extended: false }));
25-
app.use(cookieParser());
2629
app.use(express.static(path.join(currDirName, "public")));
2730

2831
app.use("/", indexRouter);

middleware/identifyUser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import jwt from "jsonwebtoken";
2+
import util, { logger } from "#util";
3+
4+
export async function identifyUser(req, res, next) {
5+
const authHeader = req.headers.authorization;
6+
const token = authHeader && authHeader.split(" ")[1];
7+
if (token === undefined) {
8+
req.user = "anonymous";
9+
next();
10+
return false;
11+
}
12+
try {
13+
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
14+
const decryptedIP = util.decrypt(payload.ip);
15+
if (decryptedIP !== req.ip) {
16+
req.user = "unauthorized";
17+
next();
18+
}
19+
req.user = JSON.stringify(payload.data.uid);
20+
req.userData = payload.data;
21+
next();
22+
return true;
23+
} catch (error) {
24+
logger.error("Error while finding user ", error);
25+
req.user = "unauthorized";
26+
next();
27+
return false;
28+
}
29+
}

routes/auth.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import express from "express";
22
import authController from "#controller/auth";
3-
import middleware from "#middleware/auth";
43

54
const router = express.Router();
65
router.post("/", authController.login);
7-
router.post("/validateUser", middleware.authenticateToken, authController.validateUser);
6+
router.post("/validateUser", authController.validateUser);
87
router.post("/sendOTP", authController.sendOTP);
98
router.post("/resetPassword", authController.resetPassword);
109

0 commit comments

Comments
 (0)