authentication.js 1.27 KB
Newer Older
1
/* global WIKI */
2 3 4 5 6 7

// ------------------------------------
// GitHub Account
// ------------------------------------

const GitHubStrategy = require('passport-github2').Strategy
8
const _ = require('lodash')
9

10 11
module.exports = {
  init (passport, conf) {
12 13 14 15
    let githubConfig = {
      clientID: conf.clientId,
      clientSecret: conf.clientSecret,
      callbackURL: conf.callbackURL,
16 17
      scope: ['user:email'],
      passReqToCallback: true
18 19 20 21 22 23
    }

    if (conf.useEnterprise) {
      githubConfig.authorizationURL = `https://${conf.enterpriseDomain}/login/oauth/authorize`
      githubConfig.tokenURL = `https://${conf.enterpriseDomain}/login/oauth/access_token`
      githubConfig.userProfileURL = conf.enterpriseUserEndpoint
24
      githubConfig.userEmailURL = `${conf.enterpriseUserEndpoint}/emails`
25 26
    }

27
    passport.use(conf.key,
28
      new GitHubStrategy(githubConfig, async (req, accessToken, refreshToken, profile, cb) => {
29 30
        try {
          const user = await WIKI.models.users.processProfile({
31
            providerKey: req.params.strategy,
32 33 34
            profile: {
              ...profile,
              picture: _.get(profile, 'photos[0].value', '')
35
            }
36 37 38 39 40
          })
          cb(null, user)
        } catch (err) {
          cb(err, null)
        }
41 42 43
      }
      ))
  }
44
}