-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
120 lines (93 loc) · 4.15 KB
/
test.js
File metadata and controls
120 lines (93 loc) · 4.15 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
var AutomaticAPI = require('./automatic-api')
, http = require('http')
, moira = require('moira')
, url = require('url')
, util = require('util')
;
var clientID = '...'
, clientSecret = '...'
, portno = { external: 8894, local: 8894 }
, clientState
;
var clients = {};
var users = {};
// this code assumes that your external IP address + portno.external is mapped to your your local IP's portno.local
moira.getIP(function(zeroP, ipaddr, service) {/* jshint unused: false */
if (!!zeroP) return console.log('no IP addresses found');
http.createServer(function(request, response) {
if (request.method !== 'GET') return webhook(request, response);
request.on('data', function(chunk) {/* jshint unused: false */
}).on('close', function() {
console.log('http error: premature close');
}).on('clientError', function(err, socket) {/* jshint unused: false */
console.log('http error: ' + err.message);
}).on('end', function() {
var client, parts, requestURL;
parts = url.parse(request.url, true);
if (!!parts.query.code) {
if (!parts.query.state) return console.log('invalid response from server');
client = clients[parts.query.state];
if (!client) return console.log('cross-site request forgery suspected');
client.authorize(parts.query.code, parts.query.state, function(err, user, state, scopes) {
if (!!err) return console.log('authorization error: ' + err.message);
// remember state as clientState
console.log(util.inspect(state, { depth: null }));
users[user.id] = client;
getToWork(client);
});
response.writeHead(200, {'content-length' : 0 });
return response.end();
}
client = new AutomaticAPI.AutomaticAPI({ clientID: clientID , clientSecret: clientSecret }).on('error', function(err) {
console.log('background error: ' + err.message);
});
requestURL = client.authenticateURL(null, 'http://' + ipaddr + ':' + portno.external + '/');
parts = url.parse(requestURL, true);
clients[parts.query.state] = client;
response.writeHead(307, { location: requestURL, 'content-length' : 0 });
response.end();
});
}).listen(portno.local, function() {
var client;
if (!clientState) return console.log('please connect to http://localhost:' + portno.local + ' to authorize application');
console.log('listening on port ' + portno.local + ' for incoming connections to http://' + ipaddr + ':' + portno.external);
client = new AutomaticAPI.AutomaticAPI({ clientID: clientID , clientSecret: clientSecret }).on('error', function(err) {
console.log('background error: ' + err.message);
}).setState(clientState);
users[clientState.id] = client;
getToWork(client);
});
});
var webhook = function (request, response) {
var body = '';
request.setEncoding('utf8');
request.on('data', function(chunk) {
body += chunk.toString();
}).on('close', function() {
console.log('http error: premature close');
}).on('clientError', function(err, socket) {/* jshint unused: false */
console.log('http error: ' + err.message);
}).on('end', function() {
var client, data;
var loser = function (message) {
response.writeHead(200, { 'content-type': 'text/plain; charset=utf8', 'content-length' : message.length });
response.end(message);
};
try { data = JSON.parse(body); } catch(ex) { return loser(ex.message); }
if (!data.type) return loser('webhook missing type parameter');
if ((!data.user) || (!data.user.id)) return loser('webhook missing user.id');
client = users[data.user.id];
if (!client) return loser('internal error (somewhere!)');
response.writeHead(200, {'content-length' : 0 });
response.end();
// now process data.type
console.log(util.inspect(data, { depth: null }));
});
};
var getToWork = function(client) {
client.roundtrip('GET', '/trips', null, function(err, results) {
if (!!err) return console.log('/trips: ' + err.message);
console.log('trips');
console.log(util.inspect(results, { depth: null }));
});
};