You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

  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. }
  15. if (conf.useEnterprise) {
  16. githubConfig.authorizationURL = `https://${conf.enterpriseDomain}/login/oauth/authorize`
  17. githubConfig.tokenURL = `https://${conf.enterpriseDomain}/login/oauth/access_token`
  18. githubConfig.userProfileURL = conf.enterpriseUserEndpoint
  19. githubConfig.userEmailURL = `${conf.enterpriseUserEndpoint}/emails`
  20. }
  21. passport.use('github',
  22. new GitHubStrategy(githubConfig, async (accessToken, refreshToken, profile, cb) => {
  23. try {
  24. const user = await WIKI.models.users.processProfile({
  25. profile: {
  26. ...profile,
  27. picture: _.get(profile, 'photos[0].value', '')
  28. },
  29. providerKey: 'github'
  30. })
  31. cb(null, user)
  32. } catch (err) {
  33. cb(err, null)
  34. }
  35. }
  36. ))
  37. }
  38. }