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.4KB

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