authentication.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* global WIKI */
  2. // ------------------------------------
  3. // Google ID Account
  4. // ------------------------------------
  5. const GoogleStrategy = require('passport-google-oauth20').Strategy
  6. const _ = require('lodash')
  7. module.exports = {
  8. init (passport, conf) {
  9. const strategy = new GoogleStrategy({
  10. clientID: conf.clientId,
  11. clientSecret: conf.clientSecret,
  12. callbackURL: conf.callbackURL,
  13. passReqToCallback: true
  14. }, async (req, accessToken, refreshToken, profile, cb) => {
  15. try {
  16. if (conf.hostedDomain && conf.hostedDomain != profile._json.hd) {
  17. throw new Error('Google authentication should have been performed with domain ' + conf.hostedDomain)
  18. }
  19. const user = await WIKI.models.users.processProfile({
  20. providerKey: req.params.strategy,
  21. profile: {
  22. ...profile,
  23. picture: _.get(profile, 'photos[0].value', '')
  24. }
  25. })
  26. cb(null, user)
  27. } catch (err) {
  28. cb(err, null)
  29. }
  30. })
  31. if (conf.hostedDomain) {
  32. strategy.authorizationParams = function(options) {
  33. return {
  34. hd: conf.hostedDomain
  35. }
  36. }
  37. }
  38. passport.use(conf.key, strategy)
  39. },
  40. logout (conf) {
  41. return '/'
  42. }
  43. }