2
0

authentication.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if (conf.mapGroups) {
  29. const groups = _.get(profile, '_json.' + conf.groupsClaim)
  30. if (groups) {
  31. const groupIDs = Object.values(WIKI.auth.groups)
  32. .filter(g => groups.includes(g.name))
  33. .map(g => g.id)
  34. for (let groupID of groupIDs) {
  35. await user.$relatedQuery('groups').relate(groupID)
  36. }
  37. }
  38. }
  39. cb(null, user)
  40. } catch (err) {
  41. cb(err, null)
  42. }
  43. })
  44. )
  45. },
  46. logout (conf) {
  47. if (!conf.logoutURL) {
  48. return '/'
  49. } else {
  50. return conf.logoutURL
  51. }
  52. }
  53. }