forked from Widen/cloudfront-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
318 lines (298 loc) · 10.1 KB
/
tests.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const fs = require('fs');
const opn = require('opn');
const prompt = require('prompt');
const colors = require('colors/safe');
const http = require('http');
const url = require('url');
const qs = require('querystring');
const shell = require('shelljs');
var beautify = require('json-beautify');
var ngrok = require('ngrok');
var dateFormat = require('dateformat');
var AWS = require('aws-sdk');
var logName = dateFormat(Date.now(), "mm-dd-yyyy-hh:MM:ss");
let server;
// Check for distribution argument
const DISTRIBUTION = process.argv.slice(2)[0];
if(!fs.existsSync("distributions/" + DISTRIBUTION)) {
console.log(colors.red("Distribution '" + DISTRIBUTION + "' does not exist. Stopping test..."));
process.exit();
}
shell.mkdir('-p', 'distributions/' + DISTRIBUTION + "/logs");
// Check for config-test.json
if(!fs.existsSync("distributions/" + DISTRIBUTION + "/config-test.json")) {
console.log(colors.red("config-test.json does not exist in '" + DISTRIBUTION + "'. Add config-test.json and restart. Stopping test..."));
process.exit();
}
// Load configs
var config = JSON.parse(fs.readFileSync("distributions/" + DISTRIBUTION + "/config.json", 'utf8'));
var testConfig = JSON.parse(fs.readFileSync("distributions/" + DISTRIBUTION + "/config-test.json"));
// Update AWS config
AWS.config.update({
accessKeyId: testConfig.aws.accessKeyId,
secretAccessKey: testConfig.aws.secretAccessKey,
region: testConfig.aws.region
});
var lambda = new AWS.Lambda();
// Start local server
server = http.createServer();
server.on('request', function(req, res){
var querystring = qs.parse(url.parse(req.url).query);
res.writeHead(200, {'Content-Type': 'text/html'});
if (querystring.code != undefined) {
// Write simple HTML page with copy button for code
var codeHtml = '<html>' +
' <head>' +
' <title>cloudfront-auth testing</title>' +
' <script>' +
' function copyCode() {' +
' var copyField = document.getElementById("codeField");' +
' copyField.select();' +
' document.execCommand("Copy");' +
' }' +
' </script>' +
' </head>' +
' <body>' +
' <input type="text" value="' + querystring.code + '" id="codeField">' +
' <button onclick="copyCode()">Copy code</button>' +
' </body>'
'</html>';
res.write(codeHtml);
}
res.end();
});
server.listen(testConfig.port);
// Toggle ngrok auth if necessary
switch(testConfig.auth) {
case("y"):
// Generate password for ngrok
var generator = require('generate-password');
var username = generator.generate({
length: 10,
numbers: true
});
var password = generator.generate({
length: 10,
numbers: true
});
console.log(colors.red("USERNAME: ") + colors.green(username));
console.log(colors.red("PASSWORD: ") + colors.green(password));
ngrok.connect({
addr: testConfig.port,
auth: username + ':' + password
}, function(err, url){
if (err) {
console.log(err);
exit(1);
}
setupRedirect(url, testConfig.lambdaFunction);
});
break;
case("n"):
default:
ngrok.connect(testConfig.port, function(err, url){
if (err) {
console.log(err);
exit(1);
}
setupRedirect(url, testConfig.lambdaFunction);
});
}
// Prompt user to enter temporary ngrok url as redirect uri
function setupRedirect(url, lambdaFunction) {
prompt.message = colors.blue(">");
prompt.start();
prompt.get({
properties: {
wait: {
description: colors.red("Add " + colors.green(url + config.CALLBACK_PATH) + " to your list of redirect URIs. Press enter when finished"),
required: false,
}
}
}, function (err, result) {
// Setup initial lambda request
var params = {
FunctionName: lambdaFunction,
Payload: initialRequestPayload(url),
LogType: "Tail"
}
// Update log
fs.writeFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "*/ Initial Request Payload /*\n" + beautify(JSON.parse(params.Payload), null, 2, 80));
// Invoke lambda
lambda.invoke(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
// Update log
fs.appendFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "\n*/ Initial Request Response /*\nStatus Code: " + data.StatusCode + "\nExecuted Version: " + data.ExecutedVersion + "\nLog Result:\n" + new Buffer(data.LogResult, 'base64').toString('ascii') + "\nPayload:\n" + beautify(JSON.parse(data.Payload), null, 2, 80));
var payload = JSON.parse(data.Payload);
console.log(payload.headers.location[0].value);
opn(payload.headers.location[0].value);
// Prompt user for code on open page
prompt.message = colors.blue(">");
prompt.start();
prompt.get({
properties: {
code: {
description: colors.red("Enter the code in your browser received from authentication provider"),
required: false,
}
}
}, function (err, result) {
// Setup callback lambda request
var params = {
FunctionName: lambdaFunction,
Payload: callbackPayload(url, result.code),
LogType: "Tail"
}
// Update log
fs.appendFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "\n\n*/ Callback Payload /*\n" + beautify(JSON.parse(params.Payload), null, 2, 80));
// Invoke lambda
lambda.invoke(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
// Update log
fs.appendFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "\n*/ Callback Response /*\nStatus Code: " + data.StatusCode + "\nExecuted Version: " + data.ExecutedVersion + "\nLog Result:\n" + new Buffer(data.LogResult, 'base64').toString('ascii') + "\nPayload:\n" + beautify(JSON.parse(data.Payload), null, 2, 80));
// Setup token lambda request
var params = {
FunctionName: lambdaFunction,
Payload: tokenRequestPayload(url, JSON.parse(data.Payload).headers["set-cookie"][0].value),
LogType: "Tail"
}
// Update log
fs.appendFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "\n\n*/ Token Payload /*\n" + beautify(JSON.parse(params.Payload), null, 2, 80));
// Invoke lambda
lambda.invoke(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
// Update log
fs.appendFileSync('distributions/' + DISTRIBUTION + '/logs/' + logName + '.log', "\n*/ Token Response /*\nStatus Code: " + data.StatusCode + "\nExecuted Version: " + data.ExecutedVersion + "\nLog Result:\n" + new Buffer(data.LogResult, 'base64').toString('ascii') + "\nPayload:\n" + beautify(JSON.parse(data.Payload), null, 2, 80));
}
// Notify user of test end and kill ngrok/local server
console.log(colors.green("Log created at /distributions/" + DISTRIBUTION + "/logs/" + logName + ".log"));
server.close();
ngrok.disconnect();
ngrok.kill();
process.exit();
});
}
});
});
}
});
});
}
/** Lambda test payloads **/
function initialRequestPayload(url) {
var payload = {
"Records": [
{
"cf": {
"request": {
"headers": {
"host": [
{
"value": "example.com",
"key": "Host"
}
],
"user-agent": [
{
"value": "test-agent",
"key": "User-Agent"
}
]
},
"clientIp": "1234:abcd::5678:1234",
"uri": "/",
"method": "GET",
"querystring": ""
},
"config": {
"distributionId": "EXAMPLE",
"test": url
}
}
}
]
};
return JSON.stringify(payload).replace(/\r?\n|\r/g, "");
}
function callbackPayload(url, code) {
var payload = {
"Records": [
{
"cf": {
"request": {
"headers": {
"host": [
{
"value": "example.com",
"key": "Host"
}
],
"user-agent": [
{
"value": "test-agent",
"key": "User-Agent"
}
]
},
"clientIp": "2001:cdba::3257:9652",
"uri": "/_callback",
"method": "GET",
"querystring": "code=" + code + "&state=%2f&session_state=fc350b67-673e-4ecd-98e7-3c2f5a875d0a"
},
"config": {
"distributionId": "EXAMPLE",
"test": url
}
}
}
]
};
return JSON.stringify(payload).replace(/\r?\n|\r/g, "");
}
function tokenRequestPayload(url, cookie) {
var payload = {
"Records": [
{
"cf": {
"request": {
"headers": {
"host": [
{
"value": "example.com",
"key": "Host"
}
],
"user-agent": [
{
"value": "test-agent",
"key": "User-Agent"
}
],
"cookie": [
{
"value": cookie,
"key": "Cookie"
}
]
},
"clientIp": "1234:abcd::5678:1234",
"uri": "/",
"method": "GET",
"querystring": ""
},
"config": {
"distributionId": "EXAMPLE",
"test": url
}
}
}
]
}
return JSON.stringify(payload).replace(/\r?\n|\r/g, "");
}