-
Notifications
You must be signed in to change notification settings - Fork 19
/
web.js
336 lines (271 loc) · 9.5 KB
/
web.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/******************************************************************************
* Libraries
*****************************************************************************/
var express = require("express");
var oauth = require('oauth');
var mongo = require('mongodb');
var gcal = require('google-calendar');
var q = require('q');
/******************************************************************************
* Variables
*****************************************************************************/
var oa;
var app = express();
var clientId = 'GOOGLE_CLIENT_ID';
var clientSecret = 'GOOGLE_CLIENT_SECRET';
var scopes = 'https://www.googleapis.com/auth/calendar';
var googleUserId;
var refreshToken;
var baseUrl;
/******************************************************************************
* App Setup
*****************************************************************************/
app.configure('development',function(){
console.log('!! DEVELOPMENT MODE !!');
googleUserId = 'GOOGLE_EMAIL_ADDRESS';
refreshToken = 'GOOGLE_REFRESH_TOKEN';
baseUrl = 'DEV_API_URL';
});
app.configure('production', function(){
console.log('!! PRODUCTION MODE !!');
googleUserId = 'GOOGLE_EMAIL_ADDRESS';
refreshToken = 'GOOGLE_REFRESH_TOKEN';
baseUrl = 'PRODUCTION_API_URL';
});
var allowCrossDomain = function(req, res, next){
//instantiate allowed domains list
var allowedDomains = [
'http://YOUR_DOMAIN.com',
'https://YOUR_DOMAIN.com'
];
//check if request origin is in allowed domains list
if(allowedDomains.indexOf(req.headers.origin) != -1)
{
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
}
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
app.use(express.logger());
app.use(express.bodyParser());
/******************************************************************************
* Database Setup
*****************************************************************************/
var mongoCollectionName = 'MONGO_COLLECTION_NAME';
var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/default';
var database;
function connect(callback)
{
var deferred = q.defer();
if(database === undefined)
{
mongo.Db.connect(mongoUri, function(err, db){
if(err) deferred.reject({error: err});
database = db;
deferred.resolve();
});
}
else
{
deferred.resolve();
}
return deferred.promise;
}
/******************************************************************************
* Methods
*****************************************************************************/
function authorize()
{
var deferred = q.defer();
oa = new oauth.OAuth2(clientId,
clientSecret,
"https://accounts.google.com/o",
"/oauth2/auth",
"/oauth2/token");
if(refreshToken)
{
oa.getOAuthAccessToken(refreshToken, {grant_type:'refresh_token', client_id: clientId, client_secret: clientSecret}, function(err, access_token, refresh_token, res){
//lookup settings from database
connect().then(function(){
database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){
var expiresIn = parseInt(res.expires_in);
var accessTokenExpiration = new Date().getTime() + (expiresIn * 1000);
//add refresh token if it is returned
if(refresh_token != undefined) settings.google_refresh_token = refresh_token;
//update access token in database
settings.google_access_token = access_token;
settings.google_access_token_expiration = accessTokenExpiration;
database.collection(mongoCollectionName).save(settings);
console.log('-- access token updated:', access_token);
deferred.resolve(access_token);
});
});
})
}
else
{
deferred.reject({error: 'Application needs authorization.'});
}
return deferred.promise;
}
function getAccessToken()
{
var deferred = q.defer();
var accessToken;
connect().then(function(){
database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){
console.log('GOOGLE SETTINGS RESPONSE:', settings, findError);
//check if access token is still valid
var today = new Date();
var currentTime = today.getTime();
if(currentTime < settings.google_access_token_expiration)
{
//use the current access token
accessToken = settings.google_access_token;
deferred.resolve(accessToken)
}
else
{
//refresh the access token
authorize().then(function(token){
accessToken = token;
deferred.resolve(accessToken);
}, function(error){
deferred.reject(error);
});
}
});
}, function(error){
deferred.reject(error);
});
return deferred.promise;
}
/******************************************************************************
* API
*****************************************************************************/
app.get('/', function(request, response) {
response.send('<!DOCTYPE html><meta charset=utf-8><form action=/authorize><input type=submit>');
});
app.get('/authorize', function(request, response){
oa = new oauth.OAuth2(clientId,
clientSecret,
"https://accounts.google.com/o",
"/oauth2/auth",
"/oauth2/token");
response.redirect(oa.getAuthorizeUrl({scope:scopes, response_type:'code', redirect_uri:baseUrl+'/callback', access_type:'offline',user_id:googleUserId}));
});
app.get('/deauthorize', function(request, response){
//TODO: implmenet deauthorize API method
});
app.get('/callback', function(request, response){
if(request.query.code){
oa.getOAuthAccessToken(request.query.code, {grant_type:'authorization_code', redirect_uri:baseUrl+'/callback'}, function(err, access_token, refresh_token, res){
if(err)
{
response.end('error: ' + JSON.stringify(err));
}
else
{
//lookup settings from database
connect().then(function(){
database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){
console.log('--writing access token to database--');
var accessTokenExpiration = new Date().getTime() + (3500 * 1000);
//update access token in database
settings.google_access_token = access_token;
settings.google_access_token_expiration = accessTokenExpiration;
//set google refresh token if it is returned
if(refresh_token != undefined) settings.google_refresh_token = refresh_token;
database.collection(mongoCollectionName).save(settings);
response.writeHead(200, {"Content-Type": "application/javascript"});
response.write('refresh token: ' + refresh_token + '\n');
response.write(JSON.stringify(res, null, '\t'));
response.end();
});
});
}
});
}
});
app.get('/events', function(request, response){
var getGoogleEvents = function(accessToken)
{
//instantiate google calendar instance
var google_calendar = new gcal.GoogleCalendar(accessToken);
google_calendar.events.list(googleUserId, {'timeMin': new Date().toISOString()}, function(err, eventList){
if(err){
response.send(500, err);
}
else{
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(eventList, null, '\t'));
response.end();
}
});
};
//retrieve current access token
getAccessToken().then(function(accessToken){
getGoogleEvents(accessToken);
}, function(error){
//TODO: handle getAccessToken error
});
});
app.post('/event', function(request, response){
var addEventBody = {
'status':'confirmed',
'summary': request.body.contact.firstName + ' ' + request.body.contact.lastName,
'description': request.body.contact.phone + '\n' + request.body.contact.details,
'organizer': {
'email': googleUserId,
'self': true
},
'start': {
'dateTime': request.body.startdate,
},
'end': {
'dateTime': request.body.enddate
},
'attendees': [
{
'email': googleUserId,
'organizer': true,
'self': true,
'responseStatus': 'needsAction'
},
{
'email': request.body.contact.email,
'organizer': false,
'responseStatus': 'needsAction'
}
]
};
var addGoogleEvent = function(accessToken){
//instantiate google calendar instance
var google_calendar = new gcal.GoogleCalendar(accessToken);
console.log('****** ADDING GOOGLE EVENT *******');
google_calendar.events.insert(googleUserId, addEventBody, function(addEventError, addEventResponse){
console.log('GOOGLE RESPONSE:', addEventError, addEventResponse);
if(!addEventError)
response.send(200, addEventResponse);
response.send(400, addEventError);
});
};
//retrieve current access token
getAccessToken().then(function(accessToken){
addGoogleEvent(accessToken);
}, function(error){
//TODO: handle error
});
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});