2018-08-04 21:27:55 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// OpenID Connect Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const OpenIDConnectStrategy = require('passport-openidconnect').Strategy
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
2022-03-26 01:17:04 +00:00
|
|
|
passport.use(conf.key,
|
2018-08-04 21:27:55 +00:00
|
|
|
new OpenIDConnectStrategy({
|
|
|
|
authorizationURL: conf.authorizationURL,
|
|
|
|
tokenURL: conf.tokenURL,
|
|
|
|
clientID: conf.clientId,
|
|
|
|
clientSecret: conf.clientSecret,
|
|
|
|
issuer: conf.issuer,
|
2020-08-15 17:32:58 +00:00
|
|
|
userInfoURL: conf.userInfoURL,
|
2020-08-30 05:36:17 +00:00
|
|
|
callbackURL: conf.callbackURL,
|
2023-03-17 21:58:41 +00:00
|
|
|
passReqToCallback: true,
|
2023-07-05 22:18:21 +00:00
|
|
|
skipUserProfile: conf.skipUserProfile,
|
|
|
|
acrValues: conf.acrValues
|
2022-11-07 21:50:47 +00:00
|
|
|
}, async (req, iss, uiProfile, idProfile, context, idToken, accessToken, refreshToken, params, cb) => {
|
|
|
|
const profile = Object.assign({}, idProfile, uiProfile)
|
|
|
|
|
2020-08-15 17:32:58 +00:00
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
2020-08-30 05:36:17 +00:00
|
|
|
providerKey: req.params.strategy,
|
2020-08-15 17:32:58 +00:00
|
|
|
profile: {
|
|
|
|
...profile,
|
2023-01-29 22:55:47 +00:00
|
|
|
email: _.get(profile, '_json.' + conf.emailClaim),
|
2023-02-08 03:00:26 +00:00
|
|
|
displayName: _.get(profile, '_json.' + conf.displayNameClaim, '')
|
2020-08-30 05:36:17 +00:00
|
|
|
}
|
2020-08-15 17:32:58 +00:00
|
|
|
})
|
2022-09-05 22:40:54 +00:00
|
|
|
if (conf.mapGroups) {
|
|
|
|
const groups = _.get(profile, '_json.' + conf.groupsClaim)
|
2022-09-17 21:54:11 +00:00
|
|
|
if (groups && _.isArray(groups)) {
|
2022-10-25 00:04:25 +00:00
|
|
|
const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
|
2022-09-17 21:54:11 +00:00
|
|
|
const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
|
|
|
|
for (const groupId of _.difference(expectedGroups, currentGroups)) {
|
|
|
|
await user.$relatedQuery('groups').relate(groupId)
|
|
|
|
}
|
|
|
|
for (const groupId of _.difference(currentGroups, expectedGroups)) {
|
2022-10-25 00:04:25 +00:00
|
|
|
await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
|
2022-09-05 22:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 17:32:58 +00:00
|
|
|
cb(null, user)
|
2020-08-30 05:36:17 +00:00
|
|
|
} catch (err) {
|
2020-08-15 17:32:58 +00:00
|
|
|
cb(err, null)
|
|
|
|
}
|
2018-08-04 21:27:55 +00:00
|
|
|
})
|
|
|
|
)
|
2020-09-10 00:20:55 +00:00
|
|
|
},
|
|
|
|
logout (conf) {
|
|
|
|
if (!conf.logoutURL) {
|
|
|
|
return '/'
|
|
|
|
} else {
|
|
|
|
return conf.logoutURL
|
|
|
|
}
|
2018-08-04 21:27:55 +00:00
|
|
|
}
|
|
|
|
}
|