authentication.js 1.6 KB

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