authentication.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const _ = require('lodash')
  2. /* global WIKI */
  3. // ------------------------------------
  4. // OpenID Connect Account
  5. // ------------------------------------
  6. const OpenIDConnectStrategy = require('passport-openidconnect').Strategy
  7. module.exports = {
  8. init (passport, conf) {
  9. passport.use(conf.key,
  10. new OpenIDConnectStrategy({
  11. authorizationURL: conf.authorizationURL,
  12. tokenURL: conf.tokenURL,
  13. clientID: conf.clientId,
  14. clientSecret: conf.clientSecret,
  15. issuer: conf.issuer,
  16. userInfoURL: conf.userInfoURL,
  17. callbackURL: conf.callbackURL,
  18. passReqToCallback: true
  19. }, async (req, iss, sub, profile, cb) => {
  20. try {
  21. const user = await WIKI.models.users.processProfile({
  22. providerKey: req.params.strategy,
  23. profile: {
  24. ...profile,
  25. email: _.get(profile, '_json.' + conf.emailClaim)
  26. }
  27. })
  28. cb(null, user)
  29. } catch (err) {
  30. cb(err, null)
  31. }
  32. })
  33. )
  34. },
  35. logout (conf) {
  36. if (!conf.logoutURL) {
  37. return '/'
  38. } else {
  39. return conf.logoutURL
  40. }
  41. }
  42. }