-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
214 lines (165 loc) · 4.54 KB
/
app.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const app = express();
const jwt = require("jsonwebtoken");
const cors = require("cors");
const path = require("path");
const http = require("http");
const masterUser = require("./users.json");
const server = http.createServer(app);
const { PORT=3000, JWT_SECRET="development_secret" } = process.env;
const users = masterUser;
const encodeMid = (req, res, next) => {
const { username } = req;
const foundUser = users.find(user => user.username === username);
if (foundUser) {
const token = jwt.sign(foundUser, JWT_SECRET, { expiresIn: Number(process.env.TOKEN_EXPIRE_TIME) || 30 });
req.token = token;
next();
return;
}
const err = {
status: 401,
message: "username unidentified"
}
next(err);
}
const decodeMid = (req, res, next) => {
const { token } = req;
try {
const verified = jwt.verify(token, JWT_SECRET);
console.log({verified, token})
const found = users.find(user => user.username === verified.username);
if (found) {
req.verified = verified;
next();
return;
}
const err = {
status: 401,
message: "token unverified"
}
next(err);
} catch (err) {
next(err);
}
}
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/", express.static(path.join(__dirname, "public")));
// GET JWT BY QUERY ENDPOINT (PUSH TOKEN DEFINED ALREADY)
app.get("/auth", (req, res, next) => {
const { id } = req.query;
req.username = id;
next();
}, encodeMid, (req, res, next) => {
const { token } = req;
res.send(token);
})
// GET USERNAMES LIST
app.get("/users", (req, res, next) => {
const { completed } = req.query;
if (completed === "true") {
res.send({ users });
return;
}
const mapped = users.map(user => ({ username: user.username, pushToken: user.pushToken }));
res.send({ users: mapped });
})
// LOGIN (PUSH TOKEN NOT DEFINED YET)
app.post("/auth", (req, res, next) => {
const { username } = req.body;
req.username = username;
next();
}, encodeMid, (req, res, next) => {
const { token } = req;
res.send(token);
})
// VERIFY JWT (PUSH TOKEN NOT DEFINED YET)
app.put("/auth", (req, res, next) => {
const { token } = req.body;
req.token = token;
next();
}, decodeMid, (req, res, next) => {
const { verified } = req;
res.send({ message: "verified", username: verified.username });
})
// SET PUSH TOKEN TO DEFINED USERNAME
app.post("/pushtoken/:username", (req, res, next) => {
const { username } = req.params;
const { token } = req.body;
const foundUser = users.find(user => user.username === username);
if (foundUser && token) {
foundUser.pushToken.push(token);
foundUser.pushToken = [... new Set(foundUser.pushToken)];
res.send({ message: "Push Token set" });
return;
}
const err = {
status: 401,
message: "username or push token unidentified"
}
next(err);
})
// REMOVE PUSH TOKEN FROM DEFINED USERNAME / LOGOUT
app.post("/logout/:username", (req, res, next) => {
const { username } = req.params;
const { token } = req.body;
const foundUser = users.find(user => user.username === username);
if (foundUser && token) {
foundUser.pushToken = foundUser.pushToken.filter(el => el !== token);
res.send({ message: "Push Token removed" });
return;
}
const err = {
status: 401,
message: "username or push token unidentified"
}
next(err);
})
// VERIFY JWT INCLUDED PUSH TOKEN INFORMATION
app.get("/internal/decode", (req, res, next) => {
const { authorization } = req.headers;
const splitted = authorization.split(' ');
if (splitted[0] !== 'Bearer') {
const err = {
status: 401,
message: "invalid token format"
}
next(err);
return;
}
const token = splitted[1];
req.token = token;
next();
}, decodeMid, (req, res, next) => {
const { verified } = req;
res.send({ verified });
})
// ERROR HANDLER
app.use((err, req, res, next) => {
console.log("ERROR", err);
if (err.error) {
console.log(err.error);
}
if (err.name === "TokenExpiredError") {
res.status(403).send({ message: err.message });
return;
}
if (err.status) {
res.status(err.status).send({ message: err.message });
return;
}
if (err.message) {
res.status(400).send({ message: err.message });
return;
}
res.status(500).send({ message: "internal error" });
})
server.listen(PORT, () => {
console.log("listen on port", PORT);
});