authentication.js 2.0 KB

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