authentication.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* global WIKI */
  2. // ------------------------------------
  3. // GitHub Account
  4. // ------------------------------------
  5. const GitHubStrategy = require('passport-github2').Strategy
  6. const _ = require('lodash')
  7. module.exports = {
  8. init (passport, conf) {
  9. let githubConfig = {
  10. clientID: conf.clientId,
  11. clientSecret: conf.clientSecret,
  12. callbackURL: conf.callbackURL,
  13. scope: ['user:email'],
  14. passReqToCallback: true
  15. }
  16. if (conf.useEnterprise) {
  17. githubConfig.authorizationURL = `https://${conf.enterpriseDomain}/login/oauth/authorize`
  18. githubConfig.tokenURL = `https://${conf.enterpriseDomain}/login/oauth/access_token`
  19. githubConfig.userProfileURL = conf.enterpriseUserEndpoint
  20. githubConfig.userEmailURL = `${conf.enterpriseUserEndpoint}/emails`
  21. }
  22. passport.use(conf.key,
  23. new GitHubStrategy(githubConfig, async (req, accessToken, refreshToken, profile, cb) => {
  24. try {
  25. const user = await WIKI.db.users.processProfile({
  26. providerKey: req.params.strategy,
  27. profile: {
  28. ...profile,
  29. picture: _.get(profile, 'photos[0].value', '')
  30. }
  31. })
  32. cb(null, user)
  33. } catch (err) {
  34. cb(err, null)
  35. }
  36. }
  37. ))
  38. }
  39. }