-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
449 lines (414 loc) · 17.1 KB
/
server.js
File metadata and controls
449 lines (414 loc) · 17.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
var http = require('http'),
url = require('url'),
fs = require('fs'),
qstr = require('querystring'),
crypto = require('crypto'),
util = require('util'),
moment = require('moment');
var DASHBOARD_PATH = "/dashboard",
BASE_PATH = "/appharbor/resources",
SSO_PATH = "/appharbor/sso",
EY_ACC_PATH = "/ey/resources",
EY_CFG_PATH = "/ey/sso",
EY_PVS_PATH = "/ey/provision",
SSO_SALT = "JjEqmttYOKcbbeJy",
CREDENTIAL = "appfirst:Jm8Poyu3CbRts3ZH",
EY_AUTH_ID = "68c2104b53521752",
EY_AUTH_KEY = "fde420642d8c1a83940d71ae651388946a94818df49991d0341d83dd273626ae0cf52d2ea9938ff1",
LISTEN_ADDR = "localhost",
LISTEN_PORT = 8080,
EXPIRE_TIME = 2 * 60;
var ey_registration_url = "http://localhost:8088/api/1/partners/48/services";
var service_url,
service_account_url,
service_account_messages_url,
service_account_invoices_url,
provisioned_service_url,
provisioned_service_messages_url;
var RED = '\033[31m',
GREEN = '\033[32m',
YELLOW = '\033[33m',
BLUE = '\033[34m',
MAGENTA = '\033[35m',
CYAN = '\033[36m',
RESET = '\033[0m';
var indexhtml = fs.readFileSync('./index.html');
var sendResponse = function(response, errcode, header, msg){
if (header["Content-Type"] == "text/html"){
console.log("%s<= response%s %s, %s, %s", MAGENTA, RESET, errcode, JSON.stringify(header), "[HTML Content]");
} else {
console.log("%s<= response%s %s, %s, %s", MAGENTA, RESET, errcode, JSON.stringify(header), msg);
}
response.writeHead(errcode, header);
response.write(msg);
response.end();
}
var sendRequest = function(method, dest_url, jsonData, handleResponse){
var parsed_url = url.parse(dest_url)
var contentType = "application/json";
var strData = JSON.stringify(jsonData);
var now = new Date();
var hmac = getAPIAuth(method, parsed_url.path, contentType, strData, now);
var headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Date": now,
"User-agenet": "EY-ServiceAPI/0.0.1",
"Host": "services.engineyard.com",
"Authentication": hmac
};
var options = {
host: parsed_url.hostname,
port: parseInt(parsed_url.port),
path: parsed_url.path,
method: method,
headers: headers
};
var request = http.request(options, function(response){
var postData = "";
response.on("data", function(postDataChunk){
postData += postDataChunk;
});
response.on("end", function(){
var statusCode = response.statusCode,
headers = response.headers;
if (response.headers["Content-Type"] == "text/html"){
console.log("%s<= response%s %s, %s, %s", MAGENTA, RESET, statusCode, JSON.stringify(headers), "[HTML Content]");
} else {
console.log("%s<= response%s %s, %s, %s", MAGENTA, RESET, statusCode, JSON.stringify(headers), postData);
}
handleResponse(request, response, postData);
});
})
request.write(strData);
request.end();
console.log("\n");
console.log("%s=> request%s to %s", MAGENTA, RESET, dest_url);
}
var authenticate = function(request){
console.log("%s[~]%s headers%s | %s", BLUE, CYAN, RESET, request.headers);
var header=request.headers['authorization']||'', // get the header
token=header.split(/\s+/).pop()||'', // and the encoded auth token
auth=new Buffer(token, 'base64').toString(); // convert from base64
console.log("%s[~]%s auth%s | %s", BLUE, CYAN, RESET, auth);
if (auth==CREDENTIAL){
console.log("%s[/]%s - auth success!", GREEN, RESET);
return true;
} else {
console.log("%s[x]%s - auth failed!", RED, RESET);
return false;
}
}
var sso = function(request, data){
var seed = data.id + ':' + SSO_SALT + ':' + data.timestamp;
var shasum = crypto.createHash('sha1');
shasum.update(seed);
var token = shasum.digest('hex');
var nowts = Math.floor(new Date().getTime() / 1000);
var ts = parseInt(data.timestamp);
if (token!=data.token) {
console.log("%s[x]%s - invalid sso token", RED, RESET);
return 403;
} else if (nowts-ts > EXPIRE_TIME) {
console.log("%s[x]%s - request expired", RED, RESET);
return 403;
} else if (false) { // tenant not found
return 404;
} else {
console.log("%s[/]%s - sso success", GREEN, RESET);
return 302;
}
}
var provision = function(response, pathname, data) {
console.log("%s[/]%s - provision a tenant", GREEN, RESET);
var tenant_id = Math.floor(Math.random() * 1000),
jsonmsg = { id:tenant_id, config:{"APPFIRST_URL":"http://dev.appfirst.com/?tenant=" + tenant_id} };
sendResponse(response, 200, {'Content-Type': 'text/json'}, JSON.stringify(jsonmsg));
}
var changePlan = function(response, pathname, data) {
var paths = pathname.split("/");
var tenant_id = paths[paths.length-1];
console.log("%s[/]%s - change plan of tenant %s to plan %s", GREEN, RESET, tenant_id, data.plan);
var jsonmsg = { config:{"APPFIRST_URL":"http://dev.appfirst.com/?tenant=" + tenant_id},
message:"welcome to plan " + data.plan };
sendResponse(response, 200, {'Content-Type': 'text/json'}, JSON.stringify(jsonmsg));
}
var deprovision = function(response, pathname, data) {
var tenant_id = pathname.substring(pathname.lastIndexOf("/")+1, pathname.length);
console.log("%s[/]%s - deprovision tenant %s", GREEN, RESET, tenant_id);
sendResponse(response, 200, {'Content-Type': 'text/plain'}, "ok");
}
var getAPIAuth = function(method, path, contentType, strData, date){
var isoFormat = "yyyy-mm-dd hh:MM:ss o";
var contentMD5 = crypto.createHash("md5").update(strData).digest('hex');
var canonical_string = util.format("%s\n%s\n%s\n%s\n%s",
method, contentType, contentMD5, moment(date).format(), path);
return getHmac(EY_AUTH_ID, EY_AUTH_KEY, canonical_string);
}
var getSSOUrl = function(dest_url){
return getHmac(EY_AUTH_ID, EY_AUTH_KEY, dest_url);
}
var getHmac = function(auth_id, auth_key, message){
var signing = crypto.createHmac("sha1", auth_key)
.update(message)
.digest("base64");
return util.format("AuthHMAC %s:%s", auth_id, signing);
}
var ey_authenticate = function(request, postData){
var method = request.method;
var urlObj = url.parse(request.url),
path = urlObj.path,
auth = request.headers['authentication']||'',
contentType = request.headers["content-type"],
date = Date.parse(request.headers['Date']);
console.log("%s[~]%s auth%s | %s", BLUE, CYAN, RESET, auth);
var calc_auth = getAPIAuth(method, path, contentType, postData, date)
if (auth = calc_auth){
console.log("%s[/]%s - auth success!", GREEN, RESET);
return true;
} else {
console.log("%s[x]%s - auth failed!", RED, RESET);
return false;
}
}
var ey_sso = function(request, postData) {
var method = request.method;
var urlObj = url.parse(request.url),
path = urlObj.path,
query = qstr.parse(urlObj.query)
org_url = request.url.substring(0, request.url.indexOf("&signature="))
token = getSSOUrl(org_url);
if (token!=query["signature"]) {
console.log("%s[x]%s - invalid sso token", RED, RESET);
return 403;
} else if (false) { // tenant not found
return 404;
} else {
console.log("%s[/]%s - sso success", GREEN, RESET);
return 302;
}
}
var ey_register_partner = function(response, pathname, data){
sendResponse(response, 200, {}, "Will send Registration request");
var reqJson = {
"service": {
"name": "AppFirst",
"description": "We post friendly messages to your dashboard daily. Sign-up is free!",
"vars": [
"api_key",
"daily_supplement_path"
],
"home_url" : util.format("http://%s:%s", LISTEN_ADDR, LISTEN_PORT),
"terms_and_conditions_url": util.format("http://%s:%s%s", LISTEN_ADDR, LISTEN_PORT, "/terms"),
"service_accounts_url" : util.format("http://%s:%s%s", LISTEN_ADDR, LISTEN_PORT, EY_ACC_PATH),
}
};
sendRequest("POST", ey_registration_url, reqJson, function (request, response, postData){
console.log("%s[/]%s - service registered", GREEN, RESET);
});
}
var ey_create_account = function(response, pathname, data) {
var tenant_id = Math.floor(Math.random() * 1000);
var respJson = {
"service_account": {
"url" : util.format("http://%s:%s%s/4331", LISTEN_ADDR, LISTEN_PORT, EY_ACC_PATH),
"configuration_required" : true,
"configuration_url" : util.format("http://%s:%s%s/4331", LISTEN_ADDR, LISTEN_PORT, EY_CFG_PATH),
"provisioned_services_url": util.format("http://%s:%s%s/4331", LISTEN_ADDR, LISTEN_PORT, EY_PVS_PATH),
},"message": {}
};
console.log("%s[/]%s - create a tenant", GREEN, RESET);
sendResponse(response, 200, {'Content-Type': 'application/json'}, JSON.stringify(respJson));
}
var ey_cancel_account = function(response, pathname, data) {
var tenant_id = pathname.substring(pathname.lastIndexOf("/")+1, pathname.length);
console.log("%s[/]%s - delete tenant %s", GREEN, RESET, tenant_id);
sendResponse(response, 200, {'Content-Type': 'application/json'}, "OK");
}
var ey_provision = function(response, pathname, data) {
console.log("%s[/]%s - provision a tenant", GREEN, RESET);
var tenant_id = Math.floor(Math.random() * 1000),
respJson = {
"provisioned_service": {
"url" : util.format("http://%s:%s%s/%s/23", LISTEN_ADDR, LISTEN_PORT, EY_PVS_PATH, tenant_id),
"configuration_url" : util.format("http://%s:%s%s/%s/23", LISTEN_ADDR, LISTEN_PORT, EY_PVS_PATH, tenant_id),
"vars": {
"api_key" : "987698AFB0987EFBB983",
"daily_supplement_path" : "/etc/"
}
},
"message" : {}
}
sendResponse(response, 200, {'Content-Type': 'application/json'}, JSON.stringify(respJson));
}
var ey_deprovision = function(response, pathname, data) {
var tenant_id = pathname.substring(pathname.lastIndexOf("/")+1, pathname.length);
console.log("%s[/]%s - deprovision tenant %s", GREEN, RESET, tenant_id);
sendResponse(response, 200, {'Content-Type': 'application/json'}, "OK");
}
var ey_account_msg = function(response, pathname, data){
sendResponse(response, 200, {}, "Will send service account message");
var reqJson = {
"message": {
"message_type": "notification",
"subject": "That's a nice looking app deployment you've got there",
"body": "And a db_slave, spiffy!",
// Optional, will show as collapsed until user clicks 'read more'
}
};
sendRequest("POST", ey_registration_url, reqJson, function (request, response, postData){
console.log("%s[/]%s - account msg sent", GREEN, RESET);
});
}
var ey_provisioned_service_msg = function(response, pathname, data){
sendResponse(response, 200, {}, "Will send provisioned service message");
var reqJson = {
"message": {
"message_type": "notification",
"subject": "That's a nice looking app deployment you've got there",
"body": "And a db_slave, spiffy!",
// Optional, will show as collapsed until user clicks 'read more'
}
};
sendRequest("POST", ey_registration_url, reqJson, function (request, response, postData){
console.log("%s[/]%s - provisioned service msg sent", GREEN, RESET);
});
}
var ey_billing = function(response, pathname, data){
sendResponse(response, 200, {}, "Will send invoices");
var reqJson = {
"invoice":
{
"total_amount_cents": "3050", //USD amount in cents ($30.50)
"line_item_description": "Invoice ID: 122. For service from Jan 1 to Feb 1 of 2012, rendered in a complimentary fashion.",
}
};
sendRequest("POST", ey_registration_url, reqJson, function (request, response, postData){
console.log("%s[/]%s - invoices sent", GREEN, RESET);
});
}
function isEmpty(map) {
for (var name in map) {
if (map.hasOwnProperty(name)) {
return false;
}
}
return true;
}
var logRequest = function(request, postData){
var method = request.method;
var urlObj = url.parse(request.url),
pathname = urlObj.pathname,
query = qstr.parse(urlObj.query);
console.log("%s[~]%s meth%s | %s", BLUE, CYAN, RESET, method);
console.log("%s[~]%s path%s | %s", BLUE, CYAN, RESET, pathname);
if (!isEmpty(request.headers)){
console.log("%s[~]%s head%s | %s", BLUE, CYAN, RESET, JSON.stringify(request.headers));
}
if (!isEmpty(query)){
console.log("%s[~]%s qstr%s | %s", BLUE, CYAN, RESET, JSON.stringify(query));
}
if (typeof postData == "object" && !isEmpty(postData)) {
console.log("%s[~]%s data%s | %s", BLUE, CYAN, RESET, JSON.stringify(postData));
} else if (typeof postData == "string" && postData.length == 0) {
console.log("%s[~]%s data%s | %s", BLUE, CYAN, RESET, postData);
}
}
var handleRequest = function(request, response, postData){
var pathname = url.parse(request.url).pathname;
var method = request.method;
if (pathname.search(BASE_PATH) >= 0 && pathname.search(SSO_PATH) == -1){
if (authenticate(request)) {
logRequest(request, postData);
if (method == "DELETE"){
deprovision(response, pathname, postData);
} else if (method == "PUT") {
changePlan(response, pathname, postData);
} else {
provision(response, pathname, postData);
}
} else {
sendResponse(response, 401, {'Content-Type': 'text/plain'}, "Authentication Failed");
}
} else if (pathname == SSO_PATH && method == "POST"){
postData = qstr.parse(postData);
var errcode = sso(request, postData);
if (errcode == 302) {
logRequest(request, postData);
response.setHeader("Set-Cookie", ["heroku-nav-data="+postData['nav-data']]);
console.log("%s[/]%s - set cookie heroku-nav-data=%s", GREEN, RESET, postData['nav-data']);
sendResponse(response, 302, {"Location": "/dashboard"}, "Single Sign-On Success");
} else if (errcode == 404) {
sendResponse(response, 404, {'Content-Type': 'text/plain'}, "User Not Found");
} else {
sendResponse(response, 403, {'Content-Type': 'text/plain'}, "Access Denied");
}
} else if (pathname == DASHBOARD_PATH && method == "GET") {
logRequest(request, postData);
sendResponse(response, 200, {'Content-Type': 'text/html'}, indexhtml);
} else if (pathname.search(EY_ACC_PATH) >= 0){
if (ey_authenticate(request, postData)){
logRequest(request, postData);
if (method == "DELETE"){
ey_cancel_account(response, pathname, postData);
} else if (pathname == EY_ACC_PATH && method == "POST"){
ey_create_account(response, pathname, postData);
} else {
sendResponse(response, 404, {'Content-Type': 'text/plain'}, "Page Not Found");
}
} else {
sendResponse(response, 401, {'Content-Type': 'text/plain'}, "Authentication Failed");
}
} else if (pathname.search(EY_PVS_PATH) >= 0) {
if (ey_authenticate(request, postData)){
logRequest(request, postData);
if (method == "POST"){
ey_provision(response, pathname, postData);
} else if (method == "DELETE"){
ey_deprovision(response, pathname, postData);
} else {
sendResponse(response, 404, {'Content-Type': 'text/plain'}, "Page Not Found");
}
} else {
sendResponse(response, 401, {'Content-Type': 'text/plain'}, "Authentication Failed");
}
} else if (pathname.search(EY_CFG_PATH) >= 0){
var errcode = ey_sso(request, postData);
if (errcode == 302) {
logRequest(request, postData);
sendResponse(response, 302, {'Location': '/dashboard'}, "Single Sign-On Success");
} else if (errcode == 404) {
sendResponse(response, 404, {'Content-Type': 'text/plain'}, "User Not Found");
} else {
sendResponse(response, 403, {'Content-Type': 'text/plain'}, "Access Denied");
}
} else if (pathname.search("/ey/test/registration") >= 0 && method == "PUT") {
logRequest(request, postData);
ey_register_partner(response, pathname, postData);
} else if (pathname.search("/ey/test/account_message") >= 0 && method == "PUT") {
logRequest(request, postData);
ey_register_partner(response, pathname, postData);
} else if (pathname.search("/ey/test/provision_message") >= 0 && method == "PUT") {
logRequest(request, postData);
ey_register_partner(response, pathname, postData);
} else if (pathname.search("/ey/test/invoices") >= 0 && method == "PUT") {
logRequest(request, postData);
ey_billing(response, pathname, postData);
} else {
logRequest(request, postData);
sendResponse(response, 404, {'Content-Type': 'text/plain'}, "Page Not Found");
}
}
http.createServer(function (request, response) {
var postData = "";
console.log("\n");
console.log("%s=> request%s from %s", MAGENTA, RESET, request.connection.remoteAddress);
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
});
request.addListener("end", function() {
handleRequest(request, response, postData);
});
}).listen(LISTEN_PORT, LISTEN_ADDR);
console.log('Server running at http://%s:%s/', LISTEN_ADDR, LISTEN_PORT);