-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpassport.js
More file actions
115 lines (103 loc) · 3.98 KB
/
passport.js
File metadata and controls
115 lines (103 loc) · 3.98 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
/**
* @module middlewares/passport
*/
'use strict'
var Boom = require('dat-middleware').Boom
var GitHubStrategy = require('passport-github').Strategy
var async = require('async')
var find = require('101/find')
var hasProps = require('101/has-properties')
var keypather = require('keypather')()
var passport = require('passport')
const UserService = require('models/services/user-service')
var GitHubTokenStrategy = require('models/passport-github-token')
var User = require('models/mongo/user')
var logger = require('middlewares/logger')(__filename)
// Example:
// https://github.com/jaredhanson/passport-github/blob/master/examples/login/app.js
var GITHUB_AUTHORIZATION_URL = `${process.env.GITHUB_URL}/login/oauth/authorize`
var GITHUB_CALLBACK_URL = process.env.GITHUB_CALLBACK_URL
var GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID
var GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET
var GITHUB_SCOPE = process.env.GITHUB_SCOPE
var GITHUB_TOKEN_URL = `${process.env.GITHUB_URL}/login/oauth/access_token`
var GITHUB_USER_PROFILE_URL = `${process.env.GITHUB_API_URL}/user`
var IS_GITHUB_ENTERPRISE = process.env.IS_GITHUB_ENTERPRISE
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete GitHub profile is serialized
// and deserialized.
passport.serializeUser(function (user, done) {
done(null, user._id)
})
passport.deserializeUser(function (userId, done) {
UserService.getCompleteUserById(userId)
.asCallback(done)
})
passport.use(new GitHubTokenStrategy({}, manageUser))
// Use the GitHubStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and GitHub
// profile), and invoke a callback with a user object.
passport.use(new GitHubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: GITHUB_CALLBACK_URL,
scope: GITHUB_SCOPE,
authorizationURL: GITHUB_AUTHORIZATION_URL,
tokenURL: GITHUB_TOKEN_URL,
userProfileURL: GITHUB_USER_PROFILE_URL
},
manageUser
))
function manageUser (accessToken, refreshToken, profile, done) {
profile.id = Number(profile.id)
async.waterfall([
User.findByGithubId.bind(User, profile.id),
updateOrCreateUser
], done)
function updateOrCreateUser (user, cb) {
var primaryEmail = find(profile.emails, hasProps({ primary: true }))
if (!IS_GITHUB_ENTERPRISE) {
if (!primaryEmail) {
return cb(Boom.badRequest('GitHub account is missing primary email'))
} else if (!primaryEmail.verified) {
return cb(Boom.badRequest('GitHub primary email is not verified'))
}
}
profile.accessToken = accessToken
profile.refreshToken = refreshToken
profile.username = profile.username || profile.login
if (user && user._id) {
logger.log.info({user: user}, 'existing user, updating...')
// found existing user, updating
user.email = primaryEmail.value
user.accounts = {
github: profile
}
user.gravatar = keypather.get(profile, '_json.avatar_url')
return user.saveAsync()
.then(function () {
return UserService.createOrUpdateUser(profile.id, profile.accessToken)
})
.then(function () {
return UserService.getCompleteUserByGithubId(profile.id)
})
.asCallback(cb)
}
logger.log.info({user: user}, 'new user, inserting...')
// fresh user, inserting
return UserService.createCompleteUser({
'email': primaryEmail.value,
'accounts': { 'github': profile },
'gravatar': keypather.get(profile, '_json.avatar_url'),
'permissionLevel': 1,
'created': new Date()
})
.asCallback(cb)
}
}
module.exports = passport