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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // eslint-disable-next-line max-depth
  137. if (context.user && context.user.role === 'visitor') {
  138. action.preferVisitor = true;
  139. }
  140. } else if (jwtPayload.name || jwtPayload.picture || jwtPayload.email) {
  141. // there are some tokens (firebase) having picture and name on the main level.
  142. _overwriteLocalParticipant(store, {
  143. avatarURL: jwtPayload.picture,
  144. name: jwtPayload.name,
  145. email: jwtPayload.email
  146. });
  147. }
  148. }
  149. } else if (typeof APP === 'undefined') {
  150. // The logic of restoring JWT overrides make sense only on mobile.
  151. // On Web it should eventually be restored from storage, but there's
  152. // no such use case yet.
  153. const { user } = store.getState()['features/base/jwt'];
  154. user && _undoOverwriteLocalParticipant(store, user);
  155. }
  156. }
  157. return next(action);
  158. }
  159. /**
  160. * Undoes/resets the values overwritten by {@link _overwriteLocalParticipant}
  161. * by either clearing them or setting to default values. Only the values that
  162. * have not changed since the overwrite happened will be restored.
  163. *
  164. * NOTE Once it is possible to edit and save participant properties, this
  165. * function should restore values from the storage instead.
  166. *
  167. * @param {Store} store - The redux store.
  168. * @param {Object} localParticipant - The {@code Participant} structure used
  169. * previously to {@link _overwriteLocalParticipant}.
  170. * @private
  171. * @returns {void}
  172. */
  173. function _undoOverwriteLocalParticipant(
  174. { dispatch, getState }: IStore,
  175. { avatarURL, name, email }: { avatarURL?: string; email?: string; name?: string; }) {
  176. let localParticipant;
  177. if ((avatarURL || name || email)
  178. && (localParticipant = getLocalParticipant(getState))) {
  179. const newProperties: IParticipant = {
  180. id: localParticipant.id,
  181. local: true
  182. };
  183. if (avatarURL === localParticipant.avatarURL) {
  184. newProperties.avatarURL = undefined;
  185. }
  186. if (email === localParticipant.email) {
  187. newProperties.email = undefined;
  188. }
  189. if (name === localParticipant.name) {
  190. newProperties.name = undefined;
  191. }
  192. newProperties.features = undefined;
  193. dispatch(participantUpdated(newProperties));
  194. }
  195. }
  196. /**
  197. * Converts the JWT {@code context.user} structure to the {@code Participant}
  198. * structure stored in the redux state base/participants.
  199. *
  200. * @param {Object} user - The JWT {@code context.user} structure to convert.
  201. * @private
  202. * @returns {{
  203. * avatarURL: ?string,
  204. * email: ?string,
  205. * id: ?string,
  206. * name: ?string,
  207. * hidden-from-recorder: ?boolean
  208. * }}
  209. */
  210. function _user2participant({ avatar, avatarUrl, email, id, name, 'hidden-from-recorder': hiddenFromRecorder }:
  211. { avatar?: string; avatarUrl?: string; email: string; 'hidden-from-recorder': string | boolean;
  212. id: string; name: string; }) {
  213. const participant: {
  214. avatarURL?: string;
  215. email?: string;
  216. hiddenFromRecorder?: boolean;
  217. id?: string;
  218. name?: string;
  219. } = {};
  220. if (typeof avatarUrl === 'string') {
  221. participant.avatarURL = avatarUrl.trim();
  222. } else if (typeof avatar === 'string') {
  223. participant.avatarURL = avatar.trim();
  224. }
  225. if (typeof email === 'string') {
  226. participant.email = email.trim();
  227. }
  228. if (typeof id === 'string') {
  229. participant.id = id.trim();
  230. }
  231. if (typeof name === 'string') {
  232. participant.name = name.trim();
  233. }
  234. if (hiddenFromRecorder === 'true' || hiddenFromRecorder === true) {
  235. participant.hiddenFromRecorder = true;
  236. }
  237. return Object.keys(participant).length ? participant : undefined;
  238. }