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
|
|
|
|
}, async (req, accessToken, refreshToken, 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
|
|
|
})
|
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2020-09-02 00:01:25 +00:00
|
|
|
},
|
|
|
|
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 '/'
|
|
|
|
}
|
2019-08-30 18:26:34 +00:00
|
|
|
}
|
|
|
|
}
|