Skip to content

Commit cda7166

Browse files
committed
Merge branch 'development' of https://github.com/tcet-opensource/erp-backend into HitanshTesting
2 parents dfb45bb + e41936f commit cda7166

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

controller/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function login(req, res) {
1313
emailId: userValidated.emailId,
1414
type: userValidated.userType,
1515
};
16-
const token = util.genrateToken(userDetails);
16+
const token = util.generateToken(userDetails, req.ip);
1717
userDetails.token = token;
1818
res.json({ res: "welcome", user: userDetails });
1919
} catch (error) {

middleware/auth.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import jwt from "jsonwebtoken";
2+
import util, {logger} from "#util";
23

34
async 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-
const user = await jwt.verify(token, process.env.TOKEN_SECRET, (err, tokenData) => {
8-
if (err) return res.sendStatus(403);
9-
return tokenData;
10-
});
11-
req.user = user;
12-
next();
13-
return false;
8+
try {
9+
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
10+
const decryptedIP = util.decrypt(payload.ip);
11+
if (decryptedIP !== req.ip) {
12+
res.status(403)
13+
res.send({err:"Unauthorized"});
14+
}
15+
16+
req.user = payload.data;
17+
next();
18+
} catch (error) {
19+
res.status(403)
20+
res.send({err:"Unauthorized"});
21+
}
1422
}
1523

1624
export default { authenticateToken };

util.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import jwt from "jsonwebtoken";
22
import nodemailer from "nodemailer";
33
import { logLevel } from "#constant";
4-
4+
import crypto from "crypto"
55
import "winston-daily-rotate-file";
66
import winston from "winston";
77
import dotenv from "dotenv";
@@ -21,7 +21,28 @@ const transporter = nodemailer.createTransport({
2121
},
2222
});
2323

24-
const genrateToken = (data) => jwt.sign(data, process.env.TOKEN_SECRET);
24+
const key = crypto.randomBytes(32);
25+
const iv = crypto.randomBytes(16);
26+
const algorithm = 'aes-256-cbc';
27+
28+
const encrypt = (IP) => {
29+
const cipher = crypto.createCipheriv(algorithm, key, iv);
30+
let encrypted = cipher.update(IP, 'utf8', 'hex');
31+
encrypted += cipher.final('hex');
32+
return encrypted;
33+
}
34+
35+
const decrypt = (IP) => {
36+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
37+
let decrypted = decipher.update(IP, 'hex', 'utf8');
38+
decrypted += decipher.final('utf8');
39+
return decrypted;
40+
}
41+
42+
const generateToken = (data, IP)=>{
43+
const encryptedIP = encrypt(IP);
44+
return jwt.sign({data: data, ip: encryptedIP}, process.env.TOKEN_SECRET);
45+
}
2546

2647
const sendOTP = async (to, otp) => {
2748
await transporter.sendMail({
@@ -102,5 +123,5 @@ logger.stream = {
102123
};
103124

104125
export default {
105-
genrateToken, sendOTP, asyncPlaceholders, logger,
126+
generateToken, encrypt, decrypt, sendOTP, asyncPlaceholders, logger
106127
};

0 commit comments

Comments
 (0)