2021-10-16 02:25:15 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
// ------------------------------------
|
2021-10-16 02:36:30 +00:00
|
|
|
// OAuth2 Account
|
2021-10-16 02:25:15 +00:00
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const OAuth2Strategy = require('passport-oauth2').Strategy
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
|
|
|
var client = new OAuth2Strategy({
|
|
|
|
authorizationURL: conf.authorizationURL,
|
|
|
|
tokenURL: conf.tokenURL,
|
|
|
|
clientID: conf.clientId,
|
|
|
|
clientSecret: conf.clientSecret,
|
|
|
|
userInfoURL: conf.userInfoURL,
|
|
|
|
callbackURL: conf.callbackURL,
|
2022-04-12 04:14:02 +00:00
|
|
|
passReqToCallback: true,
|
2023-01-29 21:51:40 +00:00
|
|
|
scope: conf.scope,
|
2023-01-29 22:14:34 +00:00
|
|
|
state: conf.enableCSRFProtection
|
2021-10-16 02:25:15 +00:00
|
|
|
}, async (req, accessToken, refreshToken, profile, cb) => {
|
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
|
|
|
providerKey: req.params.strategy,
|
|
|
|
profile: {
|
|
|
|
...profile,
|
2021-10-16 02:36:30 +00:00
|
|
|
id: _.get(profile, conf.userIdClaim),
|
|
|
|
displayName: _.get(profile, conf.displayNameClaim, '???'),
|
2021-10-16 02:25:15 +00:00
|
|
|
email: _.get(profile, conf.emailClaim)
|
|
|
|
}
|
|
|
|
})
|
2023-01-29 23:08:13 +00:00
|
|
|
if (conf.mapGroups) {
|
|
|
|
const groups = _.get(profile, conf.groupsClaim)
|
|
|
|
if (groups && _.isArray(groups)) {
|
|
|
|
const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
|
|
|
|
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)) {
|
|
|
|
await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:25:15 +00:00
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
client.userProfile = function (accesstoken, done) {
|
2022-04-17 00:39:07 +00:00
|
|
|
this._oauth2._useAuthorizationHeaderForGET = !conf.useQueryStringForAccessToken
|
2021-10-16 02:25:15 +00:00
|
|
|
this._oauth2.get(conf.userInfoURL, accesstoken, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
data = JSON.parse(data)
|
2021-10-16 02:36:30 +00:00
|
|
|
} catch (e) {
|
2021-10-16 02:25:15 +00:00
|
|
|
return done(e)
|
|
|
|
}
|
|
|
|
done(null, data)
|
|
|
|
})
|
|
|
|
}
|
2022-03-26 01:17:04 +00:00
|
|
|
passport.use(conf.key, client)
|
2021-10-16 02:36:30 +00:00
|
|
|
},
|
|
|
|
logout (conf) {
|
|
|
|
if (!conf.logoutURL) {
|
|
|
|
return '/'
|
|
|
|
} else {
|
|
|
|
return conf.logoutURL
|
|
|
|
}
|
2021-10-16 02:25:15 +00:00
|
|
|
}
|
|
|
|
}
|