2019-08-30 18:26:34 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Keycloak Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const KeycloakStrategy = require('@exlinc/keycloak-passport')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
2022-03-26 01:17:04 +00:00
|
|
|
passport.use(conf.key,
|
2019-08-30 18:26:34 +00:00
|
|
|
new KeycloakStrategy({
|
2020-08-30 05:36:17 +00:00
|
|
|
authorizationURL: conf.authorizationURL,
|
|
|
|
userInfoURL: conf.userInfoURL,
|
2019-08-30 18:26:34 +00:00
|
|
|
tokenURL: conf.tokenURL,
|
2020-08-30 05:36:17 +00:00
|
|
|
host: conf.host,
|
2019-08-30 18:26:34 +00:00
|
|
|
realm: conf.realm,
|
|
|
|
clientID: conf.clientId,
|
|
|
|
clientSecret: conf.clientSecret,
|
2020-08-30 05:36:17 +00:00
|
|
|
callbackURL: conf.callbackURL,
|
|
|
|
passReqToCallback: true
|
2023-08-10 21:45:06 +00:00
|
|
|
}, async (req, accessToken, refreshToken, results, profile, cb) => {
|
2020-05-15 17:28:55 +00:00
|
|
|
let displayName = profile.username
|
|
|
|
if (_.isString(profile.fullName) && profile.fullName.length > 0) {
|
|
|
|
displayName = profile.fullName
|
|
|
|
}
|
2019-08-30 18:26:34 +00:00
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
2020-08-30 05:36:17 +00:00
|
|
|
providerKey: req.params.strategy,
|
2019-08-30 18:26:34 +00:00
|
|
|
profile: {
|
|
|
|
id: profile.keycloakId,
|
|
|
|
email: profile.email,
|
2020-05-15 17:28:55 +00:00
|
|
|
name: displayName,
|
2019-08-30 18:26:34 +00:00
|
|
|
picture: ''
|
2020-08-30 05:36:17 +00:00
|
|
|
}
|
2019-08-30 18:26:34 +00:00
|
|
|
})
|
2023-08-10 21:45:06 +00:00
|
|
|
req.session.keycloak_id_token = results.id_token
|
2019-08-30 18:26:34 +00:00
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2020-09-02 00:01:25 +00:00
|
|
|
},
|
2023-08-10 21:45:06 +00:00
|
|
|
logout (conf, context) {
|
2020-09-02 00:01:25 +00:00
|
|
|
if (!conf.logoutUpstream) {
|
|
|
|
return '/'
|
|
|
|
} else if (conf.logoutURL && conf.logoutURL.length > 5) {
|
2023-08-10 21:45:06 +00:00
|
|
|
const idToken = context.req.session.keycloak_id_token
|
|
|
|
const redirURL = encodeURIComponent(WIKI.config.host)
|
|
|
|
if (conf.logoutUpstreamRedirectLegacy) {
|
|
|
|
// keycloak < 18
|
|
|
|
return `${conf.logoutURL}?redirect_uri=${redirURL}`
|
|
|
|
} else if (idToken) {
|
|
|
|
// keycloak 18+
|
|
|
|
return `${conf.logoutURL}?post_logout_redirect_uri=${redirURL}&id_token_hint=${idToken}`
|
|
|
|
} else {
|
|
|
|
// fall back to no redirect if keycloak_id_token isn't available
|
|
|
|
return conf.logoutURL
|
|
|
|
}
|
2020-09-02 00:01:25 +00:00
|
|
|
} else {
|
|
|
|
WIKI.logger.warn('Keycloak logout URL is not configured!')
|
|
|
|
return '/'
|
|
|
|
}
|
2019-08-30 18:26:34 +00:00
|
|
|
}
|
|
|
|
}
|