forked from steve21124/hoodie-plugin-social
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
403 lines (350 loc) · 19.2 KB
/
index.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
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
/*
* Copyright 2013-2014 Xiatron LLC
*/
//set some vars
var express = require('express');
var passport = require('passport');
var util = require('util');
var ports = require('ports');
var appName = require('../../package.json').name;
var compareVersion = require('compare-version');
var hoodieServerVer = require('../hoodie-server/package.json').version;
var facebookStrategy = require('passport-facebook').Strategy;
var twitterStrategy = require('passport-twitter').Strategy;
var googleStrategy = require('passport-google-oauth').OAuth2Strategy; //from git://github.com/z0mt3c/passport-google-oauth.git
var authServer = express();
var auths = {};
var host = null;
var socialApi = require('./social_api.js');
var moment = require('moment');
var socialTasks = []; //keeps track of social active tasks
//config express and passport
passport.serializeUser(function(user, done) { done(null, user); });
passport.deserializeUser(function(obj, done) { done(null, obj); });
authServer.use(express.cookieParser());
authServer.use(express.session({ secret: 'SECRET' }));
authServer.use(passport.initialize());
authServer.use(passport.session());
authServer.use(express.bodyParser());
// Add headers to support CORS
authServer.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', req.header('origin'));
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Headers', 'Authorization');
next(); //pass to next layer of middleware
});
//run the rest in the hoodie context
module.exports = function (hoodie, cb) {
//check for plugin config items and set if not there
if (!hoodie.config.get('port')) hoodie.config.set('port', ports.getPort(appName+'-hoodie-plugin-social'));
if (!hoodie.config.get('facebook_config')) hoodie.config.set('facebook_config', {"enabled":false,"settings":{"clientID":"","clientSecret":""}});
if (!hoodie.config.get('twitter_config')) hoodie.config.set('twitter_config', {"enabled":false,"settings":{"consumerKey":"","consumerSecret":""}});
if (!hoodie.config.get('google_config')) hoodie.config.set('google_config', {"enabled":false,"settings":{}});
//get the CouchDB config then setup proxy
hoodie.request('get', '_config', {}, function(err, data){
var port = hoodie.config.get('port');
if (!data.httpd_global_handlers._auth || (data.httpd_global_handlers._auth.indexOf(port) == -1)) {
var value = '{couch_httpd_proxy, handle_proxy_req, <<"http://0.0.0.0:'+port+'">>}';
hoodie.request('PUT', '_config/httpd_global_handlers/_auth/', {data:JSON.stringify(value)},function(err, data){
if (err) console.log(err);
});
}
});
//setup base route for front end status calls
authServer.get('/', function(req, res) {
//set the host directly from front end query parameter
//work around until https://github.com/hoodiehq/hoodie-server/issues/183 is resolved
if (req.query.uri) host = req.query.uri;
//check if we intend to destroy the current auth object
if (req.query.destroy == 'true' && req.session.ref) {
delete auths[req.session.ref];
req.session.destroy();
res.redirect(host+req.url.replace('destroy=true','destroy=false'));
return false;
}
//either send the current auth object or create one
if ((req.session.ref != undefined) && (auths[req.session.ref] != undefined)) {
res.send(scrubAuthObj(auths[req.session.ref]));
delete auths[req.session.ref]['temp_pass']; //only give it once!
} else {
setAuthObj({method: req.query.method, id: req.query.userid}, function(ref) {
req.session.ref = ref;
res.send(scrubAuthObj(auths[req.session.ref]));
});
}
});
//listen for tasks to set status
var social = new socialApi();
hoodie.task.on('setstatus:add', function (db, doc) {
if (socialTasks.indexOf(doc.id) > -1) return false;
socialTasks.push(doc.id); //only try to process once (workaround for mutiple repeated calls)
//process
if (doc.provider && doc.userid && doc.status) {
getSocialCreds(doc.userid, doc.provider, function(creds){
var apiClient = new social[doc.provider](creds);
apiClient.setStatus(doc.status, function(err, data){
var response = (err) ? err : data;
//complete the task
completeSocialTask(db, doc, response);
});
});
}
});
//listen for getprofile tasks
hoodie.task.on('getprofile:add', function (db, doc) {
if (socialTasks.indexOf(doc.id) > -1) return false;
socialTasks.push(doc.id); //only try to process once (workaround for mutiple repeated calls)
//process
if (doc.provider && doc.userid) {
getSocialCreds(doc.userid, doc.provider, function(creds){
var apiClient = new social[doc.provider](creds);
apiClient.getProfile(doc.options, function(err, data){
var response = (err) ? err : data;
//complete the task
completeSocialTask(db, doc, response);
});
});
}
});
//listen for getcontacts tasks
hoodie.task.on('getcontacts:add', function (db, doc) {
if (socialTasks.indexOf(doc.id) > -1) return false;
socialTasks.push(doc.id); //only try to process once (workaround for mutiple repeated calls)
//process
if (doc.provider && doc.userid) {
getSocialCreds(doc.userid, doc.provider, function(creds){
var apiClient = new social[doc.provider](creds);
apiClient.getContacts(doc.options, function(err, data){
var response = (err) ? err : data;
//complete the task
completeSocialTask(db, doc, response);
});
});
}
});
//listen for getfollowers tasks
hoodie.task.on('getfollowers:add', function (db, doc) {
if (socialTasks.indexOf(doc.id) > -1) return false;
socialTasks.push(doc.id); //only try to process once (workaround for mutiple repeated calls)
//process
if (doc.provider && doc.userid) {
getSocialCreds(doc.userid, doc.provider, function(creds){
var apiClient = new social[doc.provider](creds);
apiClient.getFollowers(doc.options, function(err, data){
var response = (err) ? err : data;
//complete the task
completeSocialTask(db, doc, response);
});
});
}
});
//setup generic authenticate route (redirect destination from specific provider routes)
authServer.get('/auth', function(req, res, next) {
if (passport._strategies[req.query.provider] == undefined) {
invokeStrategy(req.query.provider, res);
} else {
if (req.query.provider == 'facebook') {
passport.authenticate(req.query.provider, { display: 'touch', scope: ['read_friendlists', 'read_stream', 'publish_actions'] })(req, res);
} else if (req.query.provider == 'google') {
passport.authenticate(req.query.provider, {
accessType: 'offline',
requestVisibleActions: ['https://schemas.google.com/AddActivity','https://schemas.google.com/BuyActivity','https://schemas.google.com/CheckInActivity','http://schemas.google.com/CommentActivity','https://schemas.google.com/CreateActivity','https://schemas.google.com/DiscoverActivity','https://schemas.google.com/ListenActivity','https://schemas.google.com/ReserveActivity','https://schemas.google.com/ReviewActivity','https://schemas.google.com/WantActivity'].join(' ')
} )(req, res, next);
} else {
passport.authenticate(req.query.provider)(req, res, next);
}
}
});
//setup facebook specific authenicate and callback routes
authServer.get('/auth/facebook', function(req, res, next) { res.redirect(host+'/auth?provider=facebook'); });
authServer.get('/facebook/callback', passport.authenticate('facebook'), function(req, res, next) {res.redirect(host+'/callback?provider=facebook');});
//setup twitter specific authenicate and callback routes
authServer.get('/auth/twitter', function(req, res, next) { res.redirect(host+'/auth?provider=twitter'); });
authServer.get('/twitter/callback', passport.authenticate('twitter'), function(req, res, next) {res.redirect(host+'/callback?provider=twitter');});
//setup google specific authenicate and callback routes
authServer.get('/auth/google', function(req, res, next) { res.redirect(host+'/auth?provider=google'); });
authServer.get('/google/callback', passport.authenticate('google'), function(req, res, next) {res.redirect(host+'/callback?provider=google');});
//setup generic callback route (redirect destination from specific provider routes)
authServer.get('/callback', function(req, res, next) {
if (auths[req.session.ref]['id'] == undefined) {
//if there's no email provided by the provider (like twitter), we will create our own id
var id = (req.user.emails == undefined) ? req.user.displayName.replace(' ','_').toLowerCase()+'_'+req.user.id : req.user.emails[0].value;
} else {
var id = auths[req.session.ref]['id'];
}
//check if we have a couch user and act accordingly
hoodie.account.find('user', id, function(err, data){
var updateVals = {};
if (!err) {
if (auths[req.session.ref]['method'] == 'login' && !auths[req.session.ref]['authenticated']) {
auths[req.session.ref]['provider'] = req.query.provider;
auths[req.session.ref]['id'] = id;
auths[req.session.ref]['full_profile'] = req.user;
auths[req.session.ref]['authenticated'] = true;
//set the auth time value (used for cleanup)
auths[req.session.ref]['auth_time'] = new Date().getTime();
//temporarily change the users password - this is where the magic happens!
auths[req.session.ref]['temp_pass'] = Math.random().toString(36).slice(2,11);
//update password
updateVals['password'] = auths[req.session.ref]['temp_pass'];
}
//always update connections
var connections = (data.connections) ? data.connections : {};
connections[req.query.provider] = auths[req.session.ref]['connections'][req.query.provider]; //first update from the stored connections
auths[req.session.ref]['connections'] = connections; //then feed the complete obeject back to the authObject
updateVals['connections'] = connections; //and make sure we store the latest
//update values
hoodie.account.update('user', id, updateVals, function(err, data){ console.log(data); });
//mark as complete
auths[req.session.ref]['complete'] = true;
//give the user some visual feedback
res.send('<html><head><script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script></head><body onload="/*self.close();*/" style="margin:0; padding:0; width:100%; height: 100%; display: table;"><div style="display:table-cell; text-align:center; vertical-align: middle;"><div id="spin" style="display:inline-block;"></div></div><script>var spinner=new Spinner().spin(); document.getElementById("spin").appendChild(spinner.el);</script></body></html>');
} else {
//assume the error is because the couch user is not there and just create one
var uuid = Math.random().toString(36).slice(2,9);
var timeStamp = new Date();
var userdoc = {
id: id,
password: Math.random().toString(36).slice(2,11),
createdAt: timeStamp,
updatedAt: timeStamp,
signedUpAt: timeStamp,
database: 'user/'+uuid,
name: 'user/'+id
};
//set ownerHash/hoodieId
if (compareVersion(hoodieServerVer, '0.8.15') >= 0) {
userdoc['hoodieId'] = uuid;
} else {
userdoc['ownerHash'] = uuid;
}
hoodie.account.add('user', userdoc, function(err, data){
//cycle back through so we can catch the fully created user
if (!err) res.redirect(host+'/'+req.query.provider+'/callback');
});
}
});
});
//No need to keep this stuff around, so lets clean up after ourselves
var cleanupInterval = setInterval(function() {cleanupAuths();},15000);
function cleanupAuths() {
var now = new Date().getTime();
for(var i in auths) {
if (now - auths[i].auth_time >= 30000) {
delete auths[i];
}
}
}
//function to invoke a strategy
function invokeStrategy(provider, res) {
var config = hoodie.config.get(provider+'_config');
if (config.enabled) {
var settings = config.settings;
settings['passReqToCallback'] = true;
settings['failureRedirect'] = '/fail'; //todo - set this route up
if (provider == 'facebook') {
settings['callbackURL'] = host+'/facebook/callback';
var providerStrategy = facebookStrategy;
var verify = function(req, accessToken,refreshToken,profile,done){
auths[req.session.ref]['connections'][provider] = {token: accessToken};
process.nextTick(function(){ return done(null,profile); });
}
} else if (provider == 'twitter') {
settings['callbackURL'] = host+'/twitter/callback';
var providerStrategy = twitterStrategy;
var verify = function(req, accessToken,tokenSecret,profile,done){
auths[req.session.ref]['connections'][provider] = {token: accessToken, secret: tokenSecret, id: profile.id};
process.nextTick(function(){ return done(null,profile); });
}
} else if (provider == 'google') {
settings['callbackURL'] = host+'/google/callback';
settings['scope'] = [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/plus.media.upload',
'https://www.googleapis.com/auth/plus.profiles.read',
'https://www.googleapis.com/auth/plus.stream.read',
'https://www.googleapis.com/auth/plus.stream.write',
'https://www.googleapis.com/auth/plus.circles.read',
'https://www.googleapis.com/auth/plus.circles.write',
'https://www.googleapis.com/auth/plus.login'
];
var providerStrategy = googleStrategy;
var verify = function(req, accessToken,tokenSecret,profile,done){
auths[req.session.ref]['connections'][provider] = {token: accessToken, secret: tokenSecret};
process.nextTick(function(){ return done(null,profile); });
}
}
passport.use(new providerStrategy(settings,verify));
res.redirect(host+'/auth/'+provider);
} else {
res.send('Provider not configured');
return false;
}
}
//function to assign a an auth object
function setAuthObj(options, callback) {
//generate random reference ID
var ref = Math.random().toString(36).slice(2);
//set a new request object for tracking progress
auths[ref] = {
"method": options.method,
"requested": new Date().getTime(),
"authenticated":false, /*depreciated*/
"complete": false,
"auth_urls": {
"facebook":host+"/auth/facebook",
"twitter":host+"/auth/twitter",
"google":host+"/auth/google"
},
"connections": {}
};
//set the id if we have it
if (options.id) auths[ref]['id'] = options.id;
callback(ref);
}
//function to get credentials
function getSocialCreds(userid, provider, callback) {
var creds = { accessToken: null };
hoodie.account.find('user', userid, function(err, data){
if (provider == 'twitter') {
var providerConfig = hoodie.config.get('twitter_config');
creds['consumerKey'] = providerConfig.settings.consumerKey;
creds['consumerSecret'] = providerConfig.settings.consumerSecret;
creds['accessSecret'] = data.connections[provider]['secret'];
creds['id'] = data.connections[provider]['id'];
}
if (data.connections[provider] != undefined) creds['accessToken'] = data.connections[provider]['token'];
callback(creds);
});
}
//function to complete a social task and send back doneData
function completeSocialTask(db, doc, doneData) {
//clear the lock
socialTasks.splice(socialTasks.indexOf(doc.id), 1);
//mimic a 'hoodie.task.success(db, doc)' but add the doneData object
doc['$processedAt'] = moment().format();
doc['_deleted'] = true;
doc['doneData'] = doneData;
hoodie.database(db).update(doc.type, doc.id, doc, function(err, data){ if(err) console.log(err); });
}
//function to filter out any data we don't want to pass back to the front end
function scrubAuthObj(authObj) {
authObjCleaned = authObj;
//remove token data
if (authObjCleaned.connections.facebook) authObj.connections.facebook = true;
if (authObjCleaned.connections.twitter) authObj.connections.twitter = true;
if (authObjCleaned.connections.google) authObj.connections.google = true;
//remove other unecessary object data
if (authObjCleaned.method == 'connect') delete authObj.authenticated;
if (authObjCleaned.complete) delete authObj.auth_urls;
return authObjCleaned;
}
//start the server on load
var port = hoodie.config.get('port');
authServer.listen(port);
console.log('Hoodie Social Plugin: Listening on port '+port);
//Hoodie Callback
cb();
}