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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, id: jwtId, 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 (jwtId) {
  62. newProperties.jwtId = jwtId;
  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. let jwtPayload;
  116. try {
  117. jwtPayload = jwtDecode(jwt);
  118. } catch (e) {
  119. logger.error(e);
  120. }
  121. if (jwtPayload) {
  122. const { context, iss, sub } = jwtPayload;
  123. action.jwt = jwt;
  124. action.issuer = iss;
  125. if (context) {
  126. const user = _user2participant(context.user || {});
  127. action.callee = context.callee;
  128. action.group = context.group;
  129. action.server = context.server;
  130. action.tenant = context.tenant || sub || undefined;
  131. action.user = user;
  132. user && _overwriteLocalParticipant(
  133. store, { ...user,
  134. features: context.features });
  135. }
  136. }
  137. } else if (typeof APP === 'undefined') {
  138. // The logic of restoring JWT overrides make sense only on mobile.
  139. // On Web it should eventually be restored from storage, but there's
  140. // no such use case yet.
  141. const { user } = store.getState()['features/base/jwt'];
  142. user && _undoOverwriteLocalParticipant(store, user);
  143. }
  144. }
  145. return next(action);
  146. }
  147. /**
  148. * Undoes/resets the values overwritten by {@link _overwriteLocalParticipant}
  149. * by either clearing them or setting to default values. Only the values that
  150. * have not changed since the overwrite happened will be restored.
  151. *
  152. * NOTE Once it is possible to edit and save participant properties, this
  153. * function should restore values from the storage instead.
  154. *
  155. * @param {Store} store - The redux store.
  156. * @param {Object} localParticipant - The {@code Participant} structure used
  157. * previously to {@link _overwriteLocalParticipant}.
  158. * @private
  159. * @returns {void}
  160. */
  161. function _undoOverwriteLocalParticipant(
  162. { dispatch, getState },
  163. { avatarURL, name, email }) {
  164. let localParticipant;
  165. if ((avatarURL || name || email)
  166. && (localParticipant = getLocalParticipant(getState))) {
  167. const newProperties: Object = {
  168. id: localParticipant.id,
  169. local: true
  170. };
  171. if (avatarURL === localParticipant.avatarURL) {
  172. newProperties.avatarURL = undefined;
  173. }
  174. if (email === localParticipant.email) {
  175. newProperties.email = undefined;
  176. }
  177. if (name === localParticipant.name) {
  178. newProperties.name = undefined;
  179. }
  180. newProperties.features = undefined;
  181. dispatch(participantUpdated(newProperties));
  182. }
  183. }
  184. /**
  185. * Converts the JWT {@code context.user} structure to the {@code Participant}
  186. * structure stored in the redux state base/participants.
  187. *
  188. * @param {Object} user - The JWT {@code context.user} structure to convert.
  189. * @private
  190. * @returns {{
  191. * avatarURL: ?string,
  192. * email: ?string,
  193. * id: ?string,
  194. * name: ?string,
  195. * hidden-from-recorder: ?boolean
  196. * }}
  197. */
  198. function _user2participant({ avatar, avatarUrl, email, id, name, 'hidden-from-recorder': hiddenFromRecorder }) {
  199. const participant = {};
  200. if (typeof avatarUrl === 'string') {
  201. participant.avatarURL = avatarUrl.trim();
  202. } else if (typeof avatar === 'string') {
  203. participant.avatarURL = avatar.trim();
  204. }
  205. if (typeof email === 'string') {
  206. participant.email = email.trim();
  207. }
  208. if (typeof id === 'string') {
  209. participant.id = id.trim();
  210. }
  211. if (typeof name === 'string') {
  212. participant.name = name.trim();
  213. }
  214. if (hiddenFromRecorder === 'true' || hiddenFromRecorder === true) {
  215. participant.hiddenFromRecorder = true;
  216. }
  217. return Object.keys(participant).length ? participant : undefined;
  218. }