|
| 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