2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// GitHub Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const GitHubStrategy = require('passport-github2').Strategy
|
2019-04-22 01:43:33 +00:00
|
|
|
const _ = require('lodash')
|
2017-07-29 21:33:08 +00:00
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
2019-07-14 17:36:39 +00:00
|
|
|
let githubConfig = {
|
|
|
|
clientID: conf.clientId,
|
|
|
|
clientSecret: conf.clientSecret,
|
|
|
|
callbackURL: conf.callbackURL,
|
2020-08-30 05:36:17 +00:00
|
|
|
scope: ['user:email'],
|
|
|
|
passReqToCallback: true
|
2019-07-14 17:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (conf.useEnterprise) {
|
|
|
|
githubConfig.authorizationURL = `https://${conf.enterpriseDomain}/login/oauth/authorize`
|
|
|
|
githubConfig.tokenURL = `https://${conf.enterpriseDomain}/login/oauth/access_token`
|
|
|
|
githubConfig.userProfileURL = conf.enterpriseUserEndpoint
|
2019-08-04 20:31:13 +00:00
|
|
|
githubConfig.userEmailURL = `${conf.enterpriseUserEndpoint}/emails`
|
2019-07-14 17:36:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 01:17:04 +00:00
|
|
|
passport.use(conf.key,
|
2020-08-30 05:36:17 +00:00
|
|
|
new GitHubStrategy(githubConfig, async (req, accessToken, refreshToken, profile, cb) => {
|
2019-04-22 01:43:33 +00:00
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
2020-08-30 05:36:17 +00:00
|
|
|
providerKey: req.params.strategy,
|
2019-04-22 01:43:33 +00:00
|
|
|
profile: {
|
|
|
|
...profile,
|
|
|
|
picture: _.get(profile, 'photos[0].value', '')
|
2020-08-30 05:36:17 +00:00
|
|
|
}
|
2019-04-22 01:43:33 +00:00
|
|
|
})
|
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|