-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.js
287 lines (204 loc) · 7.05 KB
/
routes.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
277
278
279
280
281
282
283
284
285
286
287
var bodyParser = require('body-parser');
var fs = require('fs');
var request = require('request');
var session = require("express-session");
//*******************************************/
var config_raw = fs.readFileSync('config.json')
var config = JSON.parse(config_raw)
console.dir(config)
const valid_routes = ['aws', 'apigee', 'kong', 'mulesoft']
//*******************************************/
module.exports = function (app) {
app.get('/favicon.ico', function(req, res, next) {
res.sendStatus(200);
});
app.get('/', function(req, res, next) {
fs.readFile('./html/index.html', 'utf8', (error, page) => {
if (error) { throw new Error(error) }
fs.readFile('./html/gateway_list.html', 'utf8', (error, gateway_list) => {
if (error) { throw new Error(error) }
page = page.replace(/{{gateway_list}}/g, gateway_list)
res.send(page);
});
});
});
app.get('/:gateway', function(req, res, next) {
req.session.gateway = req.params.gateway
if (!(valid_routes.includes(req.session.gateway))) {
res.sendStatus(404)
}
else {
get_page(req.params.gateway, function(err, webPage) {
if (err) { throw new Error(err) }
res.send(webPage)
})
}
});
app.post('/getAccessToken', function(req, res, next) {
var code = req.body.code;
console.log("the authorization code is: " + code);
var url = config.okta.issuer + "/v1/token"
var redirect_uri = get_redirect_uri(req.session.gateway)
console.log("the url is: " + url);
console.log("the redirect_uri is: " + redirect_uri);
var options = {
method: 'POST',
url: url,
qs: {
grant_type: 'authorization_code',
code: code,
redirect_uri: redirect_uri
},
headers: {
'cache-control': 'no-cache',
authorization: 'Basic ' + get_basic_auth_string(req.session.gateway),
'content-type': 'application/x-www-form-urlencoded'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error)
console.log(body);
var obj = JSON.parse(body);
if (obj.hasOwnProperty("access_token")) {
req.session.access_token = obj.access_token;
console.log("the access token is: " + req.session.access_token);
}
if (obj.hasOwnProperty("id_token")) {
req.session.id_token = obj.id_token;
}
// send the access token to the introspection endpoint
// (for illustration purposes only)
url = config.okta.issuer + "/v1/introspect"
var options = {
method: 'POST',
url: url,
qs: { token: req.session.access_token },
headers: {
'cache-control': 'no-cache',
authorization: 'Basic ' + get_basic_auth_string(req.session.gateway),
accept: 'application/json',
'content-type': 'application/x-www-form-urlencoded'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log("response from Okta: ");
console.log(body);
var data = {
access_token_introsp_response: body,
access_token: req.session.access_token
}
res.json(data);
});
});
});
app.post('/getData', function(req, res, next) {
var endpoint = req.body.endpoint;
console.log("the requested endpoint is: " + endpoint)
//*******************************************/
var access_token = ""
if (req.session.hasOwnProperty('access_token')) {
access_token = req.session.access_token
}
else {
access_token = "there is no access_token in the session"
}
console.log("the access token in the session is: " + access_token)
//*******************************************/
var gateway = req.session.gateway;
console.log("the gateway is: \n" + gateway + "\n");
//*******************************************/
var url = config.gateways[gateway].proxy_uri + "/" + req.body.endpoint;
console.log("the url is: " + url)
//*******************************************/
var options = {
method: 'GET',
url: url,
headers: {
'cache-control': 'no-cache',
authorization: "Bearer " + req.session.access_token,
accept: 'application/json',
'content-type': 'application/x-www-form-urlencoded'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error)
console.log("******\nresponse from API gateway: ")
console.log("the status code is: " + response.statusCode)
console.log("the body is:")
console.log(body)
if (response.statusCode == 403) {
res.json({gateway_response: 'forbidden'})
console.log("the request is forbidden")
}
else if (response.statusCode == 401) {
res.json({ gateway_response: 'unauthorized' })
console.log("the request is unauthorized")
}
else {
res.json(body)
}
});
});
app.post('/killSession', function(req, res, next) {
req.session.destroy(function(err) {
if (err) {
console.log("unable to destroy session.")
}
else {
console.log("successfully destroyed session.");
}
res.send("OK");
})
});
}
//*******************************************/
function get_basic_auth_string() {
var x = config.okta.client_id + ":" + process.env.OKTA_CLIENT_SECRET;
var y = new Buffer(x).toString('base64');
return y;
}
function get_links(gateway) {
var links_html = ""
var links = Object.keys(config.gateways[gateway].links)
for (i = 0; i < links.length; i++) {
var name = links[i]
var href = config.gateways[gateway].links[name]
links_html += "\n<li><a href ='" + href + "' target = '_blank'>" + name + "</a></li>"
}
return links_html
}
function get_page(gateway, callback) {
var main_template = './html/main.html'
var gateway_template = './html/' + gateway + '.html'
var gateway_list = './html/gateway_list.html'
fs.readFile(main_template, 'utf8', (error, page) => {
if (error) { throw new Error(error) }
fs.readFile(gateway_template, 'utf8', (error, gateway_content) => {
if (error) { throw new Error(error) }
fs.readFile('./html/gateway_list.html', 'utf8', (error, gateway_list) => {
if (error) { throw new Error(error) }
page = page.replace(/{{client_id}}/g, config.okta.client_id)
page = page.replace(/{{display_name}}/g, config.gateways[gateway].display_name)
page = page.replace(/{{gateway_list}}/g, gateway_list)
page = page.replace(/{{okta_issuer}}/g, config.okta.issuer)
page = page.replace(/{{okta_tenant}}/g, config.okta.tenant)
page = page.replace(/{{gateway}}/g, gateway)
page = page.replace(/{{gateway_content}}/g, gateway_content)
page = page.replace(/{{gateway_links}}/g, get_links(gateway))
page = page.replace(/password: /g, "password: " + process.env.USER_PASSWORD)
page = page.replace(/{{proxy_uri}}/g, config.gateways[gateway].proxy_uri)
page = page.replace(/{{redirect_uri}}/g, get_redirect_uri(gateway))
page = page.replace(/{{title}}/g, get_title(gateway))
page = page.replace(/{{integration_guide_href}}/g, config.gateways[gateway].links["Integration Guide"])
return callback(null, page);
});
});
});
}
function get_redirect_uri(gateway) {
return process.env.REDIRECT_URI_BASE + "/" + gateway;
}
function get_title(gateway) {
return "Okta API Access Management with " + config.gateways[gateway].display_name
}