您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 3.5KB

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