Skip to content

Commit 5aff3ae

Browse files
committed
The User IP added to the JWT token's payload. In addition, the IP is encrypted, so as it go back to user browser it will not be exposes.
1 parent efc6251 commit 5aff3ae

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

controller/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { genrateToken } = require('../util');
1+
const { generateToken } = require('../util');
22
const user = require('../models/user');
33

44
exports.login = async function(req, res, next) {
@@ -11,7 +11,7 @@ exports.login = async function(req, res, next) {
1111
"emailId":userValidated.emailId,
1212
"type": userValidated.userType,
1313
}
14-
let token = genrateToken(userDetails);
14+
let token = generateToken(userDetails, req.ip);
1515
userDetails["token"] = token;
1616
res.send({res:"welcome", user:userDetails})
1717
}

middleware/auth.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
const jwt = require('jsonwebtoken');
2+
const { decrypt } = require('../util')
23

34
function authenticateToken(req, res, next) {
45
const authHeader = req.headers['authorization'];
56
const token = authHeader && authHeader.split(' ')[1];
67
if (token == null) return res.sendStatus(401);
7-
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
88

9-
if (err) return res.sendStatus(403);
10-
11-
req.user = user;
9+
try {
10+
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
11+
console.log('try');
12+
const decryptedIP = decrypt(payload.ip);
13+
if (decryptedIP !== req.ip) {
14+
res.status(403)
15+
res.send({err:"Unauthorized"});
16+
}
1217

1318
next();
14-
})
19+
} catch (error) {
20+
res.status(403)
21+
res.send({err:"Unauthorized"});
22+
}
1523
}
1624

1725
module.exports = authenticateToken;

util.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
const jwt = require("jsonwebtoken");
2+
const crypto = require("crypto");
23

3-
exports.genrateToken = (data)=>{
4-
return jwt.sign(data, process.env.TOKEN_SECRET);
5-
}
4+
const key = crypto.randomBytes(32);
5+
const iv = crypto.randomBytes(16);
6+
const algorithm = 'aes-256-cbc';
7+
8+
exports.generateToken = (data, IP)=>{
9+
const encryptedIP = this.encrypt(IP);
10+
return jwt.sign({data: data, ip: encryptedIP}, process.env.TOKEN_SECRET);
11+
}
12+
13+
exports.encrypt = (IP) => {
14+
const cipher = crypto.createCipheriv(algorithm, key, iv);
15+
let encrypted = cipher.update(IP, 'utf8', 'hex');
16+
encrypted += cipher.final('hex');
17+
return encrypted;
18+
}
19+
20+
exports.decrypt = (IP) => {
21+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
22+
let decrypted = decipher.update(IP, 'hex', 'utf8');
23+
decrypted += decipher.final('utf8');
24+
return decrypted;
25+
}
626

727
/**
828
*

0 commit comments

Comments
 (0)