-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
140 lines (123 loc) · 3.79 KB
/
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
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
var url = require('url');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var rsaValidation = require('auth0-api-jwt-rsa-validation');
var ejs = require('ejs');
var app = new (require('express'))();
var crypto = require('crypto');
var utils = require('./utils');
var webtaskJson = require('./webtask.json');
var logsTemplate = require('./templates/logs');
var logoutTemplate = require('./templates/logout');
var notAuthorizedTemplate = require('./templates/notAuthorized');
app.use(function(req, res, next) {
var xfproto = req.get('x-forwarded-proto');
var xfport = req.get('x-forwarded-port');
req.baseUrl = [
xfproto ? xfproto.split(',')[0].trim() : 'https',
'://',
req.get('host'),
//xfport ? ':' + xfport.split(',')[0].trim() : '',
url.parse(req.originalUrl).pathname
].join('');
req.audience = 'https://' + req.webtaskContext.data.AUTH0_DOMAIN + '/api/v2/';
next();
});
app.get('/', function(req, res) {
res.redirect(
[
req.webtaskContext.data.AUTH0_RTA || 'https://auth0.auth0.com',
'/authorize',
'?client_id=',
req.baseUrl,
'&response_type=token&expiration=86400000&response_mode=form_post',
'&scope=',
encodeURIComponent('openid profile'),
'&redirect_uri=',
req.baseUrl,
'&audience=',
req.audience,
'&nonce=' + encodeURIComponent(crypto.randomBytes(16).toString('hex'))
].join('')
);
});
app.get('/.well-known/oauth2-client-configuration', function(req, res) {
res.json({
redirect_uris: [req.baseUrl.replace('/.well-known/oauth2-client-configuration', '')],
client_name: 'Auth0 Extension',
post_logout_redirect_uris: [req.baseUrl.replace('/.well-known/oauth2-client-configuration', '')]
});
});
function jwtMiddleware(req, res, next) {
const rta = req.webtaskContext.data.AUTH0_RTA || 'https://auth0.auth0.com';
const middleware = expressJwt({
secret: rsaValidation({ strictSSL: true }),
algorithms: ['RS256'],
getToken: function(req) { return req.body.access_token; },
/**
* Note: We're normalizing the issuer because the access token `iss`
* ends in a slash whereas the `AUTH0_RTA` secret does not.
*/
issuer: rta.endsWith('/') ? rta : `${rta}/`,
})
return middleware(req, res, next);
}
app.post(
'/',
bodyParser.urlencoded({ extended: false }),
jwtMiddleware,
function(req, res) {
if (
req.user.aud === req.audience ||
(Array.isArray(req.user.aud) && req.user.aud.indexOf(req.audience) > -1)
) {
res.send(ejs.render(logsTemplate, {
a0Token: req.body.access_token,
token: req.x_wt.token,
container: req.x_wt.container,
baseUrl: req.baseUrl,
rta: req.webtaskContext.data.AUTH0_RTA || 'https://auth0.auth0.com',
manageUrl: req.webtaskContext.data.AUTH0_MANAGE_URL,
webtaskAPIUrl: utils.resolveWebtaskAPIHost(req.get('host'), req.webtaskContext)
}));
} else {
res.status(403);
res.send(ejs.render(notAuthorizedTemplate, {
baseUrl: req.baseUrl
}));
}
}
);
app.get('/meta', function(req, res) {
res.json(webtaskJson);
});
app.get('/logout', function(req, res) {
res.send(ejs.render(logoutTemplate, {
container: req.x_wt.container,
baseUrl: req.baseUrl
}));
});
app.use(function(err, req, res, next) {
if (err && err.status) {
res.status(err.status);
return res.json({
error: err.code || err.name,
message: err.message || err.name
});
}
res.status(500);
if (process.env.NODE_ENV === 'production') {
return res.json({
error: 'InternalServerError',
message: err.message || err.name
});
}
return res.json({
error: 'InternalServerError',
message: err.message || err.name,
details: {
stack: err.stack
}
});
});
module.exports = app;