Skip to content

Commit 96827ca

Browse files
author
Victor Lourng
committed
Remove Hackerverse, add Google for Auth
1 parent bc8a57d commit 96827ca

File tree

8 files changed

+444
-777
lines changed

8 files changed

+444
-777
lines changed

Diff for: keystone.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Load .env for development environments
22
require('dotenv').load();
33

4+
// Initialise New Relic if an app name and license key exists
5+
if (process.env.NEW_RELIC_APP_NAME && process.env.NEW_RELIC_LICENSE_KEY) {
6+
require('newrelic');
7+
}
8+
49
/**
510
* Application Initialisation
611
*/
@@ -10,7 +15,7 @@ var keystone = require('keystone'),
1015

1116
keystone.init({
1217

13-
'name': 'HS Hackers',
18+
'name': 'HS Hackers Philly',
1419
'brand': 'HS Hackers',
1520
'back': '/me',
1621

@@ -20,16 +25,18 @@ keystone.init({
2025

2126
'views': 'templates/views',
2227
'view engine': 'jade',
28+
'view cache': false,
2329

2430
'emails': 'templates/emails',
2531

2632
'auto update': true,
2733
'mongo': process.env.MONGO_URI || 'mongodb://localhost/' + pkg.name,
2834

2935
'session': true,
36+
'session store': 'mongo',
3037
'auth': true,
3138
'user model': 'User',
32-
'cookie secret': process.env.COOKIE_SECRET || 'yOB~u{rQK`d17JFGn!@&pB9N%b_{rZa7+oVD+xVr&-u^SY^>u9Jy/-3eM+C37[4J',
39+
'cookie secret': process.env.COOKIE_SECRET || 'hshackers',
3340

3441
// the default mandrill api key is a *test* key. it will 'work', but not send emails.
3542
'mandrill api key': process.env.MANDRILL_KEY || 'v17RkIoARDkqTqPSbvrmkw',
@@ -41,11 +48,13 @@ keystone.init({
4148
'ga domain': process.env.GA_DOMAIN,
4249

4350
'chartbeat property': process.env.CHARTBEAT_PROPERTY,
44-
'chartbeat domain': process.env.CHARTBEAT_DOMAIN
51+
'chartbeat domain': process.env.CHARTBEAT_DOMAIN,
52+
53+
'basedir': __dirname
4554

4655
});
4756

48-
require('./models');
57+
keystone.import('models');
4958

5059
keystone.set('routes', require('./routes'));
5160

@@ -65,25 +74,16 @@ keystone.set('locals', {
6574
});
6675

6776
keystone.set('email locals', {
68-
keystoneURL: 'http://hshackers.org/keystone',
69-
logo: '/images/logo_email.jpg',
70-
logo_width: 120,
71-
logo_height: 112,
72-
theme: {
73-
email_bg: '#f9f9f9',
74-
link_color: '#2697de'
75-
}
76-
});
77-
78-
keystone.set('email tests', {
79-
'forgotten-password': {
80-
name: 'User',
81-
link: 'http://hshackers.org/reset-password/key'
82-
}
77+
utils: keystone.utils,
78+
host: (function() {
79+
if (keystone.get('env') === 'staging') return 'http://hshackers-beta.herokuapp.com';
80+
if (keystone.get('env') === 'production') return 'http://hshackers.org';
81+
return (keystone.get('host') || 'http://localhost:') + (keystone.get('port') || '3000');
82+
})()
8383
});
8484

8585
keystone.set('nav', {
86-
'meetups': ['meetups', 'talks'],
86+
'meetups': ['meetups', 'talks', 'rsvps'],
8787
'members': ['users', 'organizations'],
8888
'posts': ['posts', 'post-categories', 'post-comments'],
8989
'links': ['links', 'link-tags', 'link-comments']

Diff for: lib/auth/facebook.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var async = require('async'),
2+
_ = require('underscore');
3+
4+
var passport = require('passport'),
5+
passportFacebookStrategy = require('passport-facebook').Strategy;
6+
7+
var keystone = require('keystone'),
8+
User = keystone.list('User');
9+
10+
var credentials = {
11+
clientID: process.env.FACEBOOK_CLIENT_ID,
12+
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
13+
callbackURL: process.env.FACEBOOK_CALLBACK_URL
14+
};
15+
16+
exports.authenticateUser = function(req, res, next)
17+
{
18+
var self = this;
19+
20+
var redirect = '/auth/confirm';
21+
if (req.cookies.target && req.cookies.target == 'app') redirect = '/auth/app';
22+
23+
// Begin process
24+
console.log('============================================================');
25+
console.log('[services.facebook] - Triggered authentication process...');
26+
console.log('------------------------------------------------------------');
27+
28+
// Initalise Facebook credentials
29+
var facebookStrategy = new passportFacebookStrategy(credentials, function(accessToken, refreshToken, profile, done) {
30+
done(null, {
31+
accessToken: accessToken,
32+
refreshToken: refreshToken,
33+
profile: profile
34+
});
35+
});
36+
37+
// Pass through authentication to passport
38+
passport.use(facebookStrategy);
39+
40+
// Save user data once returning from Facebook
41+
if (_.has(req.query, 'cb')) {
42+
43+
console.log('[services.facebook] - Callback workflow detected, attempting to process data...');
44+
console.log('------------------------------------------------------------');
45+
46+
passport.authenticate('facebook', { session: false }, function(err, data, info) {
47+
48+
if (err || !data) {
49+
console.log("[services.facebook] - Error retrieving Facebook account data - " + JSON.stringify(err));
50+
return res.redirect('/signin');
51+
}
52+
53+
console.log('[services.facebook] - Successfully retrieved Facebook account data, processing...');
54+
console.log('------------------------------------------------------------');
55+
56+
var name = data.profile && data.profile.displayName ? data.profile.displayName.split(' ') : [];
57+
58+
var auth = {
59+
type: 'facebook',
60+
61+
name: {
62+
first: name.length ? name[0] : '',
63+
last: name.length > 1 ? name[1] : ''
64+
},
65+
66+
email: data.profile.emails.length ? _.first(data.profile.emails).value : null,
67+
68+
website: data.profile._json.blog,
69+
70+
profileId: data.profile.id,
71+
72+
username: data.profile.username,
73+
avatar: 'https://graph.facebook.com/' + data.profile.id + '/picture?width=600&height=600',
74+
75+
accessToken: data.accessToken,
76+
refreshToken: data.refreshToken
77+
}
78+
79+
req.session.auth = auth;
80+
81+
return res.redirect(redirect);
82+
83+
})(req, res, next);
84+
85+
// Perform inital authentication request to Facebook
86+
} else {
87+
88+
console.log('[services.facebook] - Authentication workflow detected, attempting to request access...');
89+
console.log('------------------------------------------------------------');
90+
91+
passport.authenticate('facebook', { scope: ['email'] })(req, res, next);
92+
93+
}
94+
95+
};

Diff for: lib/auth/github.js

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
var async = require('async'),
2+
_ = require('underscore'),
3+
request = require('request');
4+
5+
var passport = require('passport'),
6+
passportGithubStrategy = require('passport-github').Strategy;
7+
8+
var keystone = require('keystone'),
9+
User = keystone.list('User');
10+
11+
var credentials = {
12+
clientID: process.env.GITHUB_CLIENT_ID,
13+
clientSecret: process.env.GITHUB_CLIENT_SECRET,
14+
callbackURL: process.env.GITHUB_CALLBACK_URL
15+
};
16+
17+
exports.authenticateUser = function(req, res, next)
18+
{
19+
var self = this;
20+
21+
var redirect = '/auth/confirm';
22+
if (req.cookies.target && req.cookies.target == 'app') redirect = '/auth/app';
23+
24+
// Begin process
25+
console.log('============================================================');
26+
console.log('[services.github] - Triggered authentication process...');
27+
console.log('------------------------------------------------------------');
28+
29+
// Initalise GitHub credentials
30+
var githubStrategy = new passportGithubStrategy(credentials, function(accessToken, refreshToken, profile, done) {
31+
done(null, {
32+
accessToken: accessToken,
33+
refreshToken: refreshToken,
34+
profile: profile
35+
});
36+
});
37+
38+
// Pass through authentication to passport
39+
passport.use(githubStrategy);
40+
41+
// Save user data once returning from GitHub
42+
if (_.has(req.query, 'cb')) {
43+
44+
console.log('[services.github] - Callback workflow detected, attempting to process data...');
45+
console.log('------------------------------------------------------------');
46+
47+
passport.authenticate('github', { session: false }, function(err, data, info) {
48+
49+
if (err || !data) {
50+
console.log("[services.github] - Error retrieving GitHub account data - " + JSON.stringify(err));
51+
return res.redirect('/signin');
52+
}
53+
54+
console.log('[services.github] - Successfully retrieved GitHub account data, processing...');
55+
console.log('------------------------------------------------------------');
56+
57+
var name = data.profile && data.profile.displayName ? data.profile.displayName.split(' ') : [];
58+
59+
var auth = {
60+
type: 'github',
61+
62+
name: {
63+
first: name.length ? name[0] : '',
64+
last: name.length > 1 ? name[1] : ''
65+
},
66+
67+
website: data.profile._json.blog,
68+
69+
profileId: data.profile.id,
70+
71+
username: data.profile.username,
72+
avatar: data.profile._json.avatar_url,
73+
74+
accessToken: data.accessToken,
75+
refreshToken: data.refreshToken
76+
}
77+
78+
// GitHub Specific: Retrieve email address
79+
self.getEmails(auth.accessToken, function(err, email) {
80+
if (!err && email) auth.email = email;
81+
req.session.auth = auth;
82+
return res.redirect(redirect);
83+
});
84+
85+
})(req, res, next);
86+
87+
// Perform inital authentication request to GitHub
88+
} else {
89+
90+
console.log('[services.github] - Authentication workflow detected, attempting to request access...');
91+
console.log('------------------------------------------------------------');
92+
93+
passport.authenticate('github', { scope: ['user:email'] })(req, res, next);
94+
95+
}
96+
97+
};
98+
99+
exports.getEmails = function(accessToken, next)
100+
{
101+
console.log('[services.github] - Finding GitHub email addresses...');
102+
console.log('------------------------------------------------------------');
103+
104+
request({
105+
url: 'https://api.github.com/user/emails?access_token=' + accessToken,
106+
headers: {
107+
'User-Agent': 'hshackers.org'
108+
}
109+
}, function(err, data) {
110+
111+
if (err) {
112+
113+
console.log(err);
114+
console.log('[services.github] - Error retrieving GitHub email addresses.');
115+
console.log('------------------------------------------------------------');
116+
117+
return next(err);
118+
119+
} else {
120+
121+
console.log('[services.github] - Retrieved GitHub email addresses...');
122+
console.log('------------------------------------------------------------');
123+
124+
var emails = JSON.parse(data.body),
125+
primaryEmail = false;
126+
127+
if (emails.length) {
128+
_.each(emails, function(e) {
129+
if (!e.primary) return;
130+
primaryEmail = e.email;
131+
});
132+
}
133+
134+
return next(err, primaryEmail);
135+
136+
}
137+
138+
});
139+
};

0 commit comments

Comments
 (0)