wikijs-fork/server/modules/authentication/azure/authentication.js

59 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-07-20 15:09:55 +00:00
const _ = require('lodash')
/* 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
module.exports = {
init (passport, conf) {
// 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);
}
}
passport.use(conf.key,
2019-07-20 04:16:29 +00:00
new OIDCStrategy({
identityMetadata: conf.entryPoint,
clientID: conf.clientId,
2019-07-20 04:16:29 +00:00
redirectUrl: conf.callbackURL,
responseType: 'id_token',
responseMode: 'form_post',
scope: ['profile', 'email', 'openid'],
allowHttpForRedirectUrl: WIKI.IS_DEBUG,
passReqToCallback: true,
cookieSameSite: keyArray.length > 0,
useCookieInsteadOfSession: keyArray.length > 0,
cookieEncryptionKeys: keyArray
}, async (req, iss, sub, profile, cb) => {
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({
providerKey: req.params.strategy,
2019-07-20 15:09:55 +00:00
profile: {
id: profile.oid,
displayName: profile.displayName,
email: usrEmail,
2019-07-20 15:09:55 +00:00
picture: ''
}
2019-07-20 15:09:55 +00:00
})
cb(null, user)
} catch (err) {
cb(err, null)
}
})
)
}
2017-07-29 21:33:08 +00:00
}