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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // @flow
  2. import jwtDecode from 'jwt-decode';
  3. import { SET_CONFIG } from '../config';
  4. import { SET_LOCATION_URL } from '../connection';
  5. import {
  6. getLocalParticipant,
  7. participantUpdated
  8. } from '../participants';
  9. import { MiddlewareRegistry } from '../redux';
  10. import { SET_JWT } from './actionTypes';
  11. import { setJWT } from './actions';
  12. import { parseJWTFromURLParams } from './functions';
  13. import logger from './logger';
  14. declare var APP: Object;
  15. /**
  16. * Middleware to parse token data upon setting a new room URL.
  17. *
  18. * @param {Store} store - The redux store.
  19. * @private
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. switch (action.type) {
  24. case SET_CONFIG:
  25. case SET_LOCATION_URL:
  26. // XXX The JSON Web Token (JWT) is not the only piece of state that we
  27. // have decided to store in the feature jwt, there is isGuest as well
  28. // which depends on the states of the features base/config and jwt. So
  29. // the JSON Web Token comes from the conference/room's URL and isGuest
  30. // needs a recalculation upon SET_CONFIG as well.
  31. return _setConfigOrLocationURL(store, next, action);
  32. case SET_JWT:
  33. return _setJWT(store, next, action);
  34. }
  35. return next(action);
  36. });
  37. /**
  38. * Overwrites the properties {@code avatarURL}, {@code email}, and {@code name}
  39. * of the local participant stored in the redux state base/participants.
  40. *
  41. * @param {Store} store - The redux store.
  42. * @param {Object} localParticipant - The {@code Participant} structure to
  43. * overwrite the local participant stored in the redux store base/participants
  44. * with.
  45. * @private
  46. * @returns {void}
  47. */
  48. function _overwriteLocalParticipant(
  49. { dispatch, getState },
  50. { avatarURL, email, name, features }) {
  51. let localParticipant;
  52. if ((avatarURL || email || name)
  53. && (localParticipant = getLocalParticipant(getState))) {
  54. const newProperties: Object = {
  55. id: localParticipant.id,
  56. local: true
  57. };
  58. if (avatarURL) {
  59. newProperties.avatarURL = avatarURL;
  60. }
  61. if (email) {
  62. newProperties.email = email;
  63. }
  64. if (name) {
  65. newProperties.name = name;
  66. }
  67. if (features) {
  68. newProperties.features = features;
  69. }
  70. dispatch(participantUpdated(newProperties));
  71. }
  72. }
  73. /**
  74. * Notifies the feature jwt that the action {@link SET_CONFIG} or
  75. * {@link SET_LOCATION_URL} is being dispatched within a specific redux
  76. * {@code store}.
  77. *
  78. * @param {Store} store - The redux store in which the specified {@code action}
  79. * is being dispatched.
  80. * @param {Dispatch} next - The redux dispatch function to dispatch the
  81. * specified {@code action} to the specified {@code store}.
  82. * @param {Action} action - The redux action {@code SET_CONFIG} or
  83. * {@code SET_LOCATION_URL} which is being dispatched in the specified
  84. * {@code store}.
  85. * @private
  86. * @returns {Object} The new state that is the result of the reduction of the
  87. * specified {@code action}.
  88. */
  89. function _setConfigOrLocationURL({ dispatch, getState }, next, action) {
  90. const result = next(action);
  91. const { locationURL } = getState()['features/base/connection'];
  92. dispatch(
  93. setJWT(locationURL ? parseJWTFromURLParams(locationURL) : undefined));
  94. return result;
  95. }
  96. /**
  97. * Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
  98. * within a specific redux {@code store}.
  99. *
  100. * @param {Store} store - The redux store in which the specified {@code action}
  101. * is being dispatched.
  102. * @param {Dispatch} next - The redux dispatch function to dispatch the
  103. * specified {@code action} to the specified {@code store}.
  104. * @param {Action} action - The redux action {@code SET_JWT} which is being
  105. * dispatched in the specified {@code store}.
  106. * @private
  107. * @returns {Object} The new state that is the result of the reduction of the
  108. * specified {@code action}.
  109. */
  110. function _setJWT(store, next, action) {
  111. // eslint-disable-next-line no-unused-vars
  112. const { jwt, type, ...actionPayload } = action;
  113. if (!Object.keys(actionPayload).length) {
  114. if (jwt) {
  115. const {
  116. enableUserRolesBasedOnToken
  117. } = store.getState()['features/base/config'];
  118. action.isGuest = !enableUserRolesBasedOnToken;
  119. let jwtPayload;
  120. try {
  121. jwtPayload = jwtDecode(jwt);
  122. } catch (e) {
  123. logger.error(e);
  124. }
  125. if (jwtPayload) {
  126. const { context, iss } = jwtPayload;
  127. action.jwt = jwt;
  128. action.issuer = iss;
  129. if (context) {
  130. const user = _user2participant(context.user || {});
  131. action.callee = context.callee;
  132. action.group = context.group;
  133. action.server = context.server;
  134. action.tenant = context.tenant;
  135. action.user = user;
  136. user && _overwriteLocalParticipant(
  137. store, { ...user,
  138. features: context.features });
  139. }
  140. }
  141. } else if (typeof APP === 'undefined') {
  142. // The logic of restoring JWT overrides make sense only on mobile.
  143. // On Web it should eventually be restored from storage, but there's
  144. // no such use case yet.
  145. const { user } = store.getState()['features/base/jwt'];
  146. user && _undoOverwriteLocalParticipant(store, user);
  147. }
  148. }
  149. return next(action);
  150. }
  151. /**
  152. * Undoes/resets the values overwritten by {@link _overwriteLocalParticipant}
  153. * by either clearing them or setting to default values. Only the values that
  154. * have not changed since the overwrite happened will be restored.
  155. *
  156. * NOTE Once it is possible to edit and save participant properties, this
  157. * function should restore values from the storage instead.
  158. *
  159. * @param {Store} store - The redux store.
  160. * @param {Object} localParticipant - The {@code Participant} structure used
  161. * previously to {@link _overwriteLocalParticipant}.
  162. * @private
  163. * @returns {void}
  164. */
  165. function _undoOverwriteLocalParticipant(
  166. { dispatch, getState },
  167. { avatarURL, name, email }) {
  168. let localParticipant;
  169. if ((avatarURL || name || email)
  170. && (localParticipant = getLocalParticipant(getState))) {
  171. const newProperties: Object = {
  172. id: localParticipant.id,
  173. local: true
  174. };
  175. if (avatarURL === localParticipant.avatarURL) {
  176. newProperties.avatarURL = undefined;
  177. }
  178. if (email === localParticipant.email) {
  179. newProperties.email = undefined;
  180. }
  181. if (name === localParticipant.name) {
  182. newProperties.name = undefined;
  183. }
  184. newProperties.features = undefined;
  185. dispatch(participantUpdated(newProperties));
  186. }
  187. }
  188. /**
  189. * Converts the JWT {@code context.user} structure to the {@code Participant}
  190. * structure stored in the redux state base/participants.
  191. *
  192. * @param {Object} user - The JWT {@code context.user} structure to convert.
  193. * @private
  194. * @returns {{
  195. * avatarURL: ?string,
  196. * email: ?string,
  197. * id: ?string,
  198. * name: ?string
  199. * }}
  200. */
  201. function _user2participant({ avatar, avatarUrl, email, id, name }) {
  202. const participant = {};
  203. if (typeof avatarUrl === 'string') {
  204. participant.avatarURL = avatarUrl.trim();
  205. } else if (typeof avatar === 'string') {
  206. participant.avatarURL = avatar.trim();
  207. }
  208. if (typeof email === 'string') {
  209. participant.email = email.trim();
  210. }
  211. if (typeof id === 'string') {
  212. participant.id = id.trim();
  213. }
  214. if (typeof name === 'string') {
  215. participant.name = name.trim();
  216. }
  217. return Object.keys(participant).length ? participant : undefined;
  218. }