-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 999 Bytes
/
index.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
/**
* Foxx Services Template
* @author Skitsanos, https://linkedin.com/in/skitsanos
*/
const {query} = require('@arangodb');
module.context.use('/', require('./foxx-services/echo/index'));
module.context.use('/', require('./foxx-services/users/index'));
module.context.checkAuth = headers =>
{
if (module.context.configuration.skipAuthorization)
{
return true;
}
const auth_header = headers.authorization;
if (!auth_header)
{
return false;
}
if (!auth_header.toLowerCase().includes('bearer'))
{
return false;
}
const [, token] = auth_header.split(' ');
const user = query`
FOR user in auth
FILTER user.token==${token}
UPDATE user WITH { lastLogin: DATE_NOW() } IN auth
RETURN user
`.toArray();
return user.length === 1;
};
module.context.use((req, res, next) =>
{
if (!module.context.checkAuth(req.headers))
{
res.throw('unauthorized');
}
next();
});