authentication.js 1.22 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 16 17 18 19 20 21 22
    let githubConfig = {
      clientID: conf.clientId,
      clientSecret: conf.clientSecret,
      callbackURL: conf.callbackURL,
      scope: ['user:email']
    }

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

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