You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

  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, results, 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. req.session.keycloak_id_token = results.id_token
  36. cb(null, user)
  37. } catch (err) {
  38. cb(err, null)
  39. }
  40. })
  41. )
  42. },
  43. logout (conf, context) {
  44. if (!conf.logoutUpstream) {
  45. return '/'
  46. } else if (conf.logoutURL && conf.logoutURL.length > 5) {
  47. const idToken = context.req.session.keycloak_id_token
  48. const redirURL = encodeURIComponent(WIKI.config.host)
  49. if (conf.logoutUpstreamRedirectLegacy) {
  50. // keycloak < 18
  51. return `${conf.logoutURL}?redirect_uri=${redirURL}`
  52. } else if (idToken) {
  53. // keycloak 18+
  54. return `${conf.logoutURL}?post_logout_redirect_uri=${redirURL}&id_token_hint=${idToken}`
  55. } else {
  56. // fall back to no redirect if keycloak_id_token isn't available
  57. return conf.logoutURL
  58. }
  59. } else {
  60. WIKI.logger.warn('Keycloak logout URL is not configured!')
  61. return '/'
  62. }
  63. }
  64. }