authentication.js 1.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const _ = require('lodash')

/* global WIKI */

// ------------------------------------
// Keycloak Account
// ------------------------------------

const KeycloakStrategy = require('@exlinc/keycloak-passport')

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