-
Notifications
You must be signed in to change notification settings - Fork 0
/
authServer.js
139 lines (121 loc) · 3.11 KB
/
authServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Environmet Variables
require("dotenv").config();
//Requiring modules
const express = require("express");
const bodyParser = require("body-parser");
const jwt = require("jsonwebtoken");
const fs = require("fs");
//Instance of Express
const app = express();
//Getting data
let dataJSON = fs.readFileSync("./data/data.json");
let data = JSON.parse(dataJSON);
//Global Middlewares
app.use(bodyParser.json());
//Routing
app.get("/", (req, res) => {
res.send("Welcome to Auth Server");
});
app.post("/Refreshing", refreshingToken, (req, res) => {
let NewAccessToken = req.newToken;
console.log("[+] Refreshing, Access-Token sent");
res.send(NewAccessToken);
});
app.post("/login", getCredential, (req, res) => {
let name = req.credential[0].name;
let password = req.credential[0].password;
login(name, password)
.then((tokens) => {
console.log("[+] Access-Token sent");
console.log("[+] Refresh-Token sent");
res.json(tokens);
})
.catch((e) => {
res.status(500).send(e.message);
});
});
//Function and Middlewares
function getCredential(req, res, next) {
let name = req.body.name;
let pass = req.body.password;
req.credential = [
{
name: name,
password: pass,
},
];
next();
}
async function login(name, pass) {
try {
const userName = await checkName(name);
const userData = await checkPassword(userName, pass);
let tokens = getAllToken(userData);
return tokens;
} catch (e) {
throw new Error(e);
}
}
function checkName(name) {
return new Promise((resolve, reject) => {
let userData = data.filter(
(item) => item.name.toUpperCase() == name.toUpperCase()
);
userData !== `undefined` && userData !== null && userData.length > 0
? resolve(userData[0].name)
: reject("User not found");
});
}
function checkPassword(name, pass) {
return new Promise((resolve, reject) => {
let userData = data.filter(
(item) => item.name == name && item.password == pass
);
userData !== `undefined` && userData !== null && userData.length > 0
? resolve(userData)
: reject("Wrong Password");
});
}
function getAllToken(userData) {
let data = {
name: userData[0].name,
workplace: userData[0].workplace,
};
let AccessToken = getAccessToken(data);
let RefreshToken = getRefreshToken(data);
const tokens = {
AccessToken: AccessToken,
RefreshToken: RefreshToken,
};
return tokens;
}
function getAccessToken(data) {
return jwt.sign(data, process.env.ACCESS_TOKEN, {
expiresIn: "5m",
});
}
function getRefreshToken(data) {
return jwt.sign(data, process.env.REFRESH_TOKEN, {
expiresIn: "2w",
});
}
function refreshingToken(req, res, next) {
jwt.verify(req.body.token, process.env.REFRESH_TOKEN, (err, data) => {
if (!err) {
let newData = {
name: data.name,
workplace: data.workplace,
};
let newToken = getAccessToken(newData);
req.newToken = newToken;
next();
} else {
throw err;
}
});
}
//Server
const port = process.env.AUTH_PORT;
app.listen(port, () => {
console.log("Server is running on port: " + port);
});