2019-07-20 15:09:55 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Azure AD Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
2019-07-20 04:16:29 +00:00
|
|
|
const OIDCStrategy = require('passport-azure-ad').OIDCStrategy
|
2017-07-29 21:33:08 +00:00
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
2020-11-01 18:10:50 +00:00
|
|
|
// Workaround for Chrome's SameSite cookies
|
|
|
|
// cookieSameSite needs useCookieInsteadOfSession to work correctly.
|
|
|
|
// cookieEncryptionKeys is extracted from conf.cookieEncryptionKeyString.
|
|
|
|
// It's a concatnation of 44-character length strings each of which represents a single pair of key/iv.
|
|
|
|
// Valid cookieEncryptionKeys enables both cookieSameSite and useCookieInsteadOfSession.
|
|
|
|
const keyArray = [];
|
|
|
|
if (conf.cookieEncryptionKeyString) {
|
|
|
|
let keyString = conf.cookieEncryptionKeyString;
|
|
|
|
while (keyString.length >= 44) {
|
|
|
|
keyArray.push({ key: keyString.substring(0, 32), iv: keyString.substring(32, 44) });
|
|
|
|
keyString = keyString.substring(44);
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 01:17:04 +00:00
|
|
|
passport.use(conf.key,
|
2019-07-20 04:16:29 +00:00
|
|
|
new OIDCStrategy({
|
|
|
|
identityMetadata: conf.entryPoint,
|
2017-09-11 03:00:03 +00:00
|
|
|
clientID: conf.clientId,
|
2019-07-20 04:16:29 +00:00
|
|
|
redirectUrl: conf.callbackURL,
|
|
|
|
responseType: 'id_token',
|
|
|
|
responseMode: 'form_post',
|
|
|
|
scope: ['profile', 'email', 'openid'],
|
2020-08-30 05:36:17 +00:00
|
|
|
allowHttpForRedirectUrl: WIKI.IS_DEBUG,
|
2020-11-01 18:10:50 +00:00
|
|
|
passReqToCallback: true,
|
|
|
|
cookieSameSite: keyArray.length > 0,
|
|
|
|
useCookieInsteadOfSession: keyArray.length > 0,
|
|
|
|
cookieEncryptionKeys: keyArray
|
2020-08-30 05:36:17 +00:00
|
|
|
}, async (req, iss, sub, profile, cb) => {
|
2019-10-27 17:57:39 +00:00
|
|
|
const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')
|
2019-07-20 15:09:55 +00:00
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
2020-08-30 05:36:17 +00:00
|
|
|
providerKey: req.params.strategy,
|
2019-07-20 15:09:55 +00:00
|
|
|
profile: {
|
|
|
|
id: profile.oid,
|
|
|
|
displayName: profile.displayName,
|
2019-10-27 17:57:39 +00:00
|
|
|
email: usrEmail,
|
2019-07-20 15:09:55 +00:00
|
|
|
picture: ''
|
2020-08-30 05:36:17 +00:00
|
|
|
}
|
2019-07-20 15:09:55 +00:00
|
|
|
})
|
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|