authentication.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. if (conf.mapGroups) {
  48. const groups = _.get(profile, '_json.groups')
  49. if (groups && _.isArray(groups)) {
  50. const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
  51. const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
  52. for (const groupId of _.difference(expectedGroups, currentGroups)) {
  53. await user.$relatedQuery('groups').relate(groupId)
  54. }
  55. for (const groupId of _.difference(currentGroups, expectedGroups)) {
  56. await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
  57. }
  58. }
  59. }
  60. cb(null, user)
  61. } catch (err) {
  62. cb(err, null)
  63. }
  64. })
  65. )
  66. }
  67. }