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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // @ts-expect-error
  2. import jwtDecode from 'jwt-decode';
  3. import { AnyAction } from 'redux';
  4. import { IStore } from '../../app/types';
  5. import { SET_CONFIG } from '../config/actionTypes';
  6. import { SET_LOCATION_URL } from '../connection/actionTypes';
  7. import { participantUpdated } from '../participants/actions';
  8. import { getLocalParticipant } from '../participants/functions';
  9. import { IParticipant } from '../participants/types';
  10. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  11. import { SET_JWT } from './actionTypes';
  12. import { setJWT } from './actions';
  13. import { parseJWTFromURLParams } from './functions';
  14. import logger from './logger';
  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 }: IStore,
  47. { avatarURL, email, id: jwtId, name, features }:
  48. { avatarURL?: string; email?: string; features?: any; id?: string; name?: string; }) {
  49. let localParticipant;
  50. if ((avatarURL || email || name || features) && (localParticipant = getLocalParticipant(getState))) {
  51. const newProperties: IParticipant = {
  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 }: IStore, next: Function, action: AnyAction) {
  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: IStore, next: Function, action: AnyAction) {
  111. // eslint-disable-next-line @typescript-eslint/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. const newUser = user ? { ...user } : {};
  133. _overwriteLocalParticipant(
  134. store, { ...newUser,
  135. features: context.features });
  136. }
  137. }
  138. } else if (typeof APP === 'undefined') {
  139. // The logic of restoring JWT overrides make sense only on mobile.
  140. // On Web it should eventually be restored from storage, but there's
  141. // no such use case yet.
  142. const { user } = store.getState()['features/base/jwt'];
  143. user && _undoOverwriteLocalParticipant(store, user);
  144. }
  145. }
  146. return next(action);
  147. }
  148. /**
  149. * Undoes/resets the values overwritten by {@link _overwriteLocalParticipant}
  150. * by either clearing them or setting to default values. Only the values that
  151. * have not changed since the overwrite happened will be restored.
  152. *
  153. * NOTE Once it is possible to edit and save participant properties, this
  154. * function should restore values from the storage instead.
  155. *
  156. * @param {Store} store - The redux store.
  157. * @param {Object} localParticipant - The {@code Participant} structure used
  158. * previously to {@link _overwriteLocalParticipant}.
  159. * @private
  160. * @returns {void}
  161. */
  162. function _undoOverwriteLocalParticipant(
  163. { dispatch, getState }: IStore,
  164. { avatarURL, name, email }: { avatarURL?: string; email?: string; name?: string; }) {
  165. let localParticipant;
  166. if ((avatarURL || name || email)
  167. && (localParticipant = getLocalParticipant(getState))) {
  168. const newProperties: IParticipant = {
  169. id: localParticipant.id,
  170. local: true
  171. };
  172. if (avatarURL === localParticipant.avatarURL) {
  173. newProperties.avatarURL = undefined;
  174. }
  175. if (email === localParticipant.email) {
  176. newProperties.email = undefined;
  177. }
  178. if (name === localParticipant.name) {
  179. newProperties.name = undefined;
  180. }
  181. newProperties.features = undefined;
  182. dispatch(participantUpdated(newProperties));
  183. }
  184. }
  185. /**
  186. * Converts the JWT {@code context.user} structure to the {@code Participant}
  187. * structure stored in the redux state base/participants.
  188. *
  189. * @param {Object} user - The JWT {@code context.user} structure to convert.
  190. * @private
  191. * @returns {{
  192. * avatarURL: ?string,
  193. * email: ?string,
  194. * id: ?string,
  195. * name: ?string,
  196. * hidden-from-recorder: ?boolean
  197. * }}
  198. */
  199. function _user2participant({ avatar, avatarUrl, email, id, name, 'hidden-from-recorder': hiddenFromRecorder }:
  200. { avatar?: string; avatarUrl?: string; email: string; 'hidden-from-recorder': string | boolean;
  201. id: string; name: string; }) {
  202. const participant: {
  203. avatarURL?: string;
  204. email?: string;
  205. hiddenFromRecorder?: boolean;
  206. id?: string;
  207. name?: string;
  208. } = {};
  209. if (typeof avatarUrl === 'string') {
  210. participant.avatarURL = avatarUrl.trim();
  211. } else if (typeof avatar === 'string') {
  212. participant.avatarURL = avatar.trim();
  213. }
  214. if (typeof email === 'string') {
  215. participant.email = email.trim();
  216. }
  217. if (typeof id === 'string') {
  218. participant.id = id.trim();
  219. }
  220. if (typeof name === 'string') {
  221. participant.name = name.trim();
  222. }
  223. if (hiddenFromRecorder === 'true' || hiddenFromRecorder === true) {
  224. participant.hiddenFromRecorder = true;
  225. }
  226. return Object.keys(participant).length ? participant : undefined;
  227. }