authentication.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const _ = require('lodash')
  2. // ------------------------------------
  3. // Azure AD Account
  4. // ------------------------------------
  5. const OIDCStrategy = require('passport-azure-ad').OIDCStrategy
  6. module.exports = {
  7. init (passport, conf) {
  8. // Workaround for Chrome's SameSite cookies
  9. // cookieSameSite needs useCookieInsteadOfSession to work correctly.
  10. // cookieEncryptionKeys is extracted from conf.cookieEncryptionKeyString.
  11. // It's a concatnation of 44-character length strings each of which represents a single pair of key/iv.
  12. // Valid cookieEncryptionKeys enables both cookieSameSite and useCookieInsteadOfSession.
  13. const keyArray = [];
  14. if (conf.cookieEncryptionKeyString) {
  15. let keyString = conf.cookieEncryptionKeyString;
  16. while (keyString.length >= 44) {
  17. keyArray.push({ key: keyString.substring(0, 32), iv: keyString.substring(32, 44) });
  18. keyString = keyString.substring(44);
  19. }
  20. }
  21. passport.use(conf.key,
  22. new OIDCStrategy({
  23. identityMetadata: conf.entryPoint,
  24. clientID: conf.clientId,
  25. redirectUrl: conf.callbackURL,
  26. responseType: 'id_token',
  27. responseMode: 'form_post',
  28. scope: ['profile', 'email', 'openid'],
  29. allowHttpForRedirectUrl: WIKI.IS_DEBUG,
  30. passReqToCallback: true,
  31. cookieSameSite: keyArray.length > 0,
  32. useCookieInsteadOfSession: keyArray.length > 0,
  33. cookieEncryptionKeys: keyArray
  34. }, async (req, iss, sub, profile, cb) => {
  35. const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')
  36. try {
  37. const user = await WIKI.db.users.processProfile({
  38. providerKey: req.params.strategy,
  39. profile: {
  40. id: profile.oid,
  41. displayName: profile.displayName,
  42. email: usrEmail,
  43. picture: ''
  44. }
  45. })
  46. cb(null, user)
  47. } catch (err) {
  48. cb(err, null)
  49. }
  50. })
  51. )
  52. }
  53. }