authentication.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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('keycloak',
  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. }