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

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