authentication.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const _ = require('lodash')
  2. // ------------------------------------
  3. // Keycloak Account
  4. // ------------------------------------
  5. const KeycloakStrategy = require('@exlinc/keycloak-passport')
  6. module.exports = {
  7. init (passport, conf) {
  8. passport.use(conf.key,
  9. new KeycloakStrategy({
  10. authorizationURL: conf.authorizationURL,
  11. userInfoURL: conf.userInfoURL,
  12. tokenURL: conf.tokenURL,
  13. host: conf.host,
  14. realm: conf.realm,
  15. clientID: conf.clientId,
  16. clientSecret: conf.clientSecret,
  17. callbackURL: conf.callbackURL,
  18. passReqToCallback: true
  19. }, async (req, accessToken, refreshToken, profile, cb) => {
  20. let displayName = profile.username
  21. if (_.isString(profile.fullName) && profile.fullName.length > 0) {
  22. displayName = profile.fullName
  23. }
  24. try {
  25. const user = await WIKI.db.users.processProfile({
  26. providerKey: req.params.strategy,
  27. profile: {
  28. id: profile.keycloakId,
  29. email: profile.email,
  30. name: displayName,
  31. picture: ''
  32. }
  33. })
  34. cb(null, user)
  35. } catch (err) {
  36. cb(err, null)
  37. }
  38. })
  39. )
  40. },
  41. logout (conf) {
  42. if (!conf.logoutUpstream) {
  43. return '/'
  44. } else if (conf.logoutURL && conf.logoutURL.length > 5) {
  45. return `${conf.logoutURL}?redirect_uri=${encodeURIComponent(WIKI.config.host)}`
  46. } else {
  47. WIKI.logger.warn('Keycloak logout URL is not configured!')
  48. return '/'
  49. }
  50. }
  51. }