-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
276 lines (224 loc) · 7.25 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const express = require('express');
const path = require('path');
const logger = require('morgan');
const http = require('http');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const { Users } = require("./services/users");
const {
middleware: openApiMiddleware,
resolvers,
} = require('express-openapi-validator');
const port = 8081;
const app = express();
// const apiSpec = path.join(__dirname, 'api.yaml');
const apiSpec = path.join(__dirname, 'zooprocess.openapi.yaml');
function verifyToken (req, res, next){
// console.log("verifyToken middleware")
// console.debug("req.path", req.path)
// console.debug("req.headers", req.headers)
const bearerHeader = req.headers [ "authorization" ];
// console.debug("Bearer header: ", bearerHeader)
if (typeof bearerHeader !== 'undefined' ) {
const bearer = bearerHeader .split(' ');
const bearerToken = bearer[1];
// console.log("Bearer found : ", bearerToken);
req.token = bearerToken;
next ();
}else{
// res. sendstatus
console.log("No Bearer")
next();
// res.sendStatus(403);
}
}
function extractJWT(req, res, next){
if ( req.token ){
// console.log("secret: ", process.env.JWT_SECRET)
// console.log("have token " , typeof(req.token))
// console.debug("jwt.decode()", jwt.decode(req.token))
jwt.verify (req.token, process.env.JWT_SECRET, (err, authData) => {
if (err) {
console.error("Error", err.message)
console.error("Error", err.name)
console.error("Error", err.expiredAt)
// if (err.name.includes("jwt expired") ){
if (err.name == "TokenExpiredError" ) {
console.error("Error Token", err)
// res.status(498).send("Token expired") // 498 used by Nginx
res.status(401).send("Token expired")
// next()
/*
Missing token <=> Missing identity card <=> Rejected because of identity card check <=> 401
Expired token <=> Expired identity card <=> Rejected because of identity card check <=> 401
Malformed token <=> Unreadable/damaged identity card <=> Rejected because of identity card check => 401
User doesn't have privilege for the resource <=> Passenger is not allowed to access this plane <=> Not rejected because of identity card check => 403
You banned a user <=> Passenger is blacklisted <=> Not rejected because of identity card check => 403
*/
}
else {
res.sendStatus(403)
// next(); // pas le next sinon passe au midleware suivant et l'erreur n'est pas traité
}
}
else {
//res.json({
// message: "Welcome to Profile",
// userData: authData
//})
// console.debug("JWT: ", authData)
req.jwt = authData;
next();
}
})
}
else {
// console.log("no token")
next();
}
}
async function getRole(req, res, next){
if ( req.jwt ){
id = req.jwt.id
// console.log("getRole id: ", id)
const users = new Users();
await users.get(id)
.then( user => {
// console.log("user:", user)
// console.log("user.role: ", user.role)
//req.jwt.role = user.role
switch (user.role) {
case "ADMIN":
req.jwt.role = "Manager" // "admin"
break;
case "MANAGER":
req.jwt.role = "Manager"
break;
case "USER":
req.jwt.role = "User"
break;
default:
req.jwt.role = "User"
}
// console.log("user.role mapped: ", req.jwt.role)
next()
})
.catch(async(e) => {
console.error("Can't find user: ", e );
// return res.status(500).json({error:e});
res.sendStatus(401)
// res.status(err.status || 500).json({
// message: err.message,
// errors: err.errors,
// });
// next()
})
}
else { // else is neccessary to avoid error
// console.log("getRole - no token")
next()
}
}
// function isRoleAllowed(req,res,next){
// const allowedRoles = req.openapi.schema.tags;
// const role = req.jwt.role
// console.log("role: ", role)
// console.log("TAGS", allowedRoles);
// if (allowedRoles.includes(role)) {
// next()
// } else {
// res.sendStatus(401)
// }
// }
// function role(req,res,next){
// console.log("req:", req)
// next()
// // console.log(": ", req.jwt.role)
// // if ( req.jwt.role == "admin" ){
// // next()
// // }else{
// // res.sendStatus(401)
// // }
// }
// function testRole(req){
// const allowedRoles = req.openapi.schema.tags;
// console.log("TAGS", allowedRoles);
// return allowedRoles.includes(role) ? true : false
// }
// ne peux pas fonctionner car c'est juste un constructeur de route
// vers la fonction à appeler
// function modulePathResolverSeb(
// handlersPath,//: string,
// route,//: RouteMetadata,
// apiDoc//: OpenAPIV3.Document,
// )/*: RequestHandler*/ {
// const pathKey = route.openApiRoute.substring(route.basePath.length);
// const schema = apiDoc.paths[pathKey][route.method.toLowerCase()];
// // const allowedRoles = req.openapi.schema.tags;
// const allowedRoles = schema['tags']
// console.log("TAGS", allowedRoles);
// const role = req.jwt.role
// console.log("role: ", role)
// if (allowedRoles.includes(role)) {
// next()
// } else {
// //res.sendStatus(401)
// throw new Error ("Not allowed")
// }
// const [controller, method] = schema['operationId'].split('.');
// const modulePath = path.join(handlersPath, controller);
// const handler = require(modulePath);
// if (handler[method] === undefined) {
// throw new Error(
// `Could not find a [${method}] function in ${modulePath} when trying to route [${route.method} ${route.expressRoute}].`,
// );
// }
// return handler[method];
// }
// console.log("modulePathResolverSeb ok")
var corsOption = {
origin: 'http://zooprocess.imev-mer.fr',
optionSuccessStatus: 200 // for legacy browser
}
// app.use (cors(corsOption))
app.use(cors())
// console.log("cors ok")
// 1. Install bodyParsers for the request types your API will support
app.use(express.urlencoded({ extended: false }));
app.use(express.text());
app.use(express.json());
// console.log("bodyParsers ok")
app.use(logger('dev'));
// console.log("logger ok")
app.use('/spec', express.static(apiSpec));
app.use(verifyToken)
app.use(extractJWT)
app.use(getRole)
// app.use(isRoleAllowed)
// console.log("verifyToken ok")
// 2. Install the OpenApiValidator middleware
app.use(
openApiMiddleware({
apiSpec,
validateResponses: true, //true ,// testRole(req), // default false
operationHandlers: {
// 3. Provide the path to the controllers directory
basePath: path.join(__dirname, 'routes'),
// 4. Provide a function responsible for resolving an Express RequestHandler
// function from the current OpenAPI Route object.
resolver: resolvers.modulePathResolver,
// resolver: modulePathResolverSeb
},
}),
);
// 5. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);
module.exports = app;