You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import jwtDecode from 'jwt-decode';
  2. import { SET_ROOM_URL } from '../base/conference';
  3. import { parseURLParams, SET_CONFIG } from '../base/config';
  4. import { MiddlewareRegistry } from '../base/redux';
  5. import { setJWT } from './actions';
  6. import { SET_JWT } from './actionTypes';
  7. /**
  8. * Middleware to parse token data upon setting a new room URL.
  9. *
  10. * @param {Store} store - The Redux store.
  11. * @private
  12. * @returns {Function}
  13. */
  14. MiddlewareRegistry.register(store => next => action => {
  15. switch (action.type) {
  16. case SET_CONFIG:
  17. case SET_ROOM_URL:
  18. // XXX The JSON Web Token (JWT) is not the only piece of state that we
  19. // have decided to store in the feature jwt, there is isGuest as well
  20. // which depends on the states of the features base/config and jwt. So
  21. // the JSON Web Token comes from the room's URL and isGuest needs a
  22. // recalculation upon SET_CONFIG as well.
  23. return _setConfigOrRoomURL(store, next, action);
  24. case SET_JWT:
  25. return _setJWT(store, next, action);
  26. }
  27. return next(action);
  28. });
  29. /**
  30. * Notifies the feature jwt that the action {@link SET_CONFIG} or
  31. * {@link SET_ROOM_URL} is being dispatched within a specific Redux
  32. * {@code store}.
  33. *
  34. * @param {Store} store - The Redux store in which the specified {@code action}
  35. * is being dispatched.
  36. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  37. * specified {@code action} to the specified {@code store}.
  38. * @param {Action} action - The Redux action {@code SET_CONFIG} or
  39. * {@code SET_ROOM_NAME} which is being dispatched in the specified
  40. * {@code store}.
  41. * @private
  42. * @returns {Object} The new state that is the result of the reduction of the
  43. * specified {@code action}.
  44. */
  45. function _setConfigOrRoomURL({ dispatch, getState }, next, action) {
  46. const result = next(action);
  47. const { roomURL } = getState()['features/base/conference'];
  48. let jwt;
  49. if (roomURL) {
  50. jwt = parseURLParams(roomURL, true, 'search').jwt;
  51. }
  52. dispatch(setJWT(jwt));
  53. return result;
  54. }
  55. /**
  56. * Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
  57. * within a specific Redux {@code store}.
  58. *
  59. * @param {Store} store - The Redux store in which the specified {@code action}
  60. * is being dispatched.
  61. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  62. * specified {@code action} to the specified {@code store}.
  63. * @param {Action} action - The Redux action {@code SET_JWT} which is being
  64. * dispatched in the specified {@code store}.
  65. * @private
  66. * @returns {Object} The new state that is the result of the reduction of the
  67. * specified {@code action}.
  68. */
  69. function _setJWT({ getState }, next, action) {
  70. // eslint-disable-next-line no-unused-vars
  71. const { jwt, type, ...actionPayload } = action;
  72. if (jwt && !Object.keys(actionPayload).length) {
  73. const {
  74. enableUserRolesBasedOnToken
  75. } = getState()['features/base/config'];
  76. action.isGuest = !enableUserRolesBasedOnToken;
  77. const jwtPayload = jwtDecode(jwt);
  78. if (jwtPayload) {
  79. const { context, iss } = jwtPayload;
  80. action.issuer = iss;
  81. if (context) {
  82. action.callee = context.callee;
  83. action.caller = context.user;
  84. action.group = context.group;
  85. action.server = context.server;
  86. }
  87. }
  88. }
  89. return next(action);
  90. }