-
Notifications
You must be signed in to change notification settings - Fork 80
/
api.js
137 lines (118 loc) · 4.37 KB
/
api.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
import { Router } from 'express';
import _ from 'lodash';
import moment from 'moment';
import { middlewares } from 'auth0-extension-express-tools';
import tools from 'auth0-extension-tools';
import { requireScope } from '../lib/middlewares';
import config from '../lib/config';
import ScriptManager from '../lib/scriptmanager';
import getScopes from '../lib/getScopes';
import * as constants from '../constants';
import applications from './applications';
import connections from './connections';
import scripts from './scripts';
import me from './me';
import logs from './logs';
import users from './users';
export default (storage) => {
const scriptManager = new ScriptManager(storage);
const managementApiClient = middlewares.managementApiClient({
domain: config('AUTH0_DOMAIN'),
clientId: config('AUTH0_CLIENT_ID'),
clientSecret: config('AUTH0_CLIENT_SECRET')
});
const api = Router();
const getToken = req => _.get(req, 'headers.authorization', '').split(' ')[1];
const addExtraUserInfo = (token, user) => {
global.daeUser = global.daeUser || {};
global.daeUser[user.sub] = global.daeUser[user.sub] || { exp: 0, token: '' };
if (_.isFunction(global.daeUser[user.sub].then)) {
return global.daeUser[user.sub];
}
if (global.daeUser[user.sub].exp > moment().unix() && token &&
global.daeUser[user.sub].token === token) {
_.assign(user, global.daeUser[user.sub]);
return Promise.resolve(user);
}
if (!token) console.error('no token found');
const promise = tools.managementApi.getClient({
domain: config('AUTH0_DOMAIN'),
clientId: config('AUTH0_CLIENT_ID'),
clientSecret: config('AUTH0_CLIENT_SECRET')
})
.then(auth0 =>
auth0.users.get({ id: user.sub })
.then((userData) => {
_.assign(user, userData);
user.token = token;
global.daeUser[user.sub] = user;
return user;
})
);
global.daeUser[user.sub] = promise;
return global.daeUser[user.sub];
};
// Allow end users to authenticate.
api.use(middlewares.authenticateUsers.optional({
domain: config('AUTH0_CUSTOM_DOMAIN') || config('AUTH0_DOMAIN'),
audience: config('EXTENSION_CLIENT_ID'),
credentialsRequired: false,
onLoginSuccess: (req, res, next) => {
const currentRequest = req;
return addExtraUserInfo(getToken(req), req.user)
.then((user) => {
currentRequest.user = user;
currentRequest.user.scope = getScopes(req.user);
return next();
})
.catch(next);
}
}));
// Allow dashboard admins to authenticate.
api.use(middlewares.authenticateAdmins.optional({
credentialsRequired: false,
secret: config('EXTENSION_SECRET'),
audience: 'urn:delegated-admin',
baseUrl: config('PUBLIC_WT_URL'),
onLoginSuccess: (req, res, next) => {
const currentRequest = req;
return addExtraUserInfo(getToken(req), req.user)
.then((user) => {
currentRequest.user = user;
currentRequest.user.scope = [ constants.AUDITOR_PERMISSION, constants.USER_PERMISSION, constants.OPERATOR_PERMISSION, constants.ADMIN_PERMISSION ];
return next();
})
.catch(next);
}
}));
/* Fight caching attempts by IE */
api.use((req, res, next) => {
res.setHeader('Cache-control', 'no-cache, no-store');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
next();
});
api.use((req, res, next) => {
const permission = (req.method.toLowerCase() === 'get') ? constants.AUDITOR_PERMISSION : constants.USER_PERMISSION;
return requireScope(permission)(req, res, next);
});
api.use('/applications', managementApiClient, applications());
api.use('/connections', managementApiClient, connections(scriptManager));
api.use('/scripts', requireScope(constants.ADMIN_PERMISSION), scripts(storage, scriptManager));
api.use('/users', managementApiClient, users(storage, scriptManager));
api.use('/logs', managementApiClient, logs(scriptManager));
api.use('/me', me(scriptManager));
api.get('/settings', (req, res, next) => {
const settingsContext = {
request: {
user: req.user
},
locale: req.headers['dae-locale']
};
scriptManager.execute('settings', settingsContext)
.then(settings => res.json({ settings: settings || {} }))
.catch(next);
});
return api;
}
;