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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import { IStore } from '../app/types';
  2. import { APP_WILL_NAVIGATE } from '../base/app/actionTypes';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../base/conference/actionTypes';
  8. import { isRoomValid } from '../base/conference/functions';
  9. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection/actionTypes';
  10. import { hideDialog } from '../base/dialog/actions';
  11. import { isDialogOpen } from '../base/dialog/functions';
  12. import {
  13. JitsiConferenceErrors,
  14. JitsiConnectionErrors
  15. } from '../base/lib-jitsi-meet';
  16. import { MEDIA_TYPE } from '../base/media/constants';
  17. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  18. import { isLocalTrackMuted } from '../base/tracks/functions.any';
  19. import { parseURIString } from '../base/util/uri';
  20. import { openLogoutDialog } from '../settings/actions';
  21. import {
  22. CANCEL_LOGIN,
  23. LOGIN,
  24. LOGOUT,
  25. STOP_WAIT_FOR_OWNER,
  26. UPGRADE_ROLE_FINISHED,
  27. WAIT_FOR_OWNER
  28. } from './actionTypes';
  29. import {
  30. hideLoginDialog,
  31. openLoginDialog,
  32. openTokenAuthUrl,
  33. openWaitForOwnerDialog,
  34. redirectToDefaultLocation,
  35. setTokenAuthUrlSuccess,
  36. stopWaitForOwner,
  37. waitForOwner
  38. } from './actions';
  39. import { LoginDialog, WaitForOwnerDialog } from './components';
  40. import { getTokenAuthUrl, isTokenAuthEnabled } from './functions';
  41. import logger from './logger';
  42. /**
  43. * Middleware that captures connection or conference failed errors and controls
  44. * {@link WaitForOwnerDialog} and {@link LoginDialog}.
  45. *
  46. * FIXME Some of the complexity was introduced by the lack of dialog stacking.
  47. *
  48. * @param {Store} store - Redux store.
  49. * @returns {Function}
  50. */
  51. MiddlewareRegistry.register(store => next => action => {
  52. switch (action.type) {
  53. case CANCEL_LOGIN: {
  54. const { dispatch, getState } = store;
  55. const state = getState();
  56. const { thenableWithCancel } = state['features/authentication'];
  57. thenableWithCancel?.cancel();
  58. // The LoginDialog can be opened on top of "wait for owner". The app
  59. // should navigate only if LoginDialog was open without the
  60. // WaitForOwnerDialog.
  61. if (!isDialogOpen(store, WaitForOwnerDialog)) {
  62. if (_isWaitingForOwner(store)) {
  63. // Instead of hiding show the new one.
  64. const result = next(action);
  65. dispatch(openWaitForOwnerDialog());
  66. return result;
  67. }
  68. dispatch(hideLoginDialog());
  69. const { authRequired, conference } = state['features/base/conference'];
  70. const { passwordRequired } = state['features/base/connection'];
  71. // Only end the meeting if we are not already inside and trying to upgrade.
  72. // NOTE: Despite it's confusing name, `passwordRequired` implies an XMPP
  73. // connection auth error.
  74. if ((passwordRequired || authRequired) && !conference) {
  75. dispatch(redirectToDefaultLocation());
  76. }
  77. }
  78. break;
  79. }
  80. case CONFERENCE_FAILED: {
  81. const { error } = action;
  82. // XXX The feature authentication affords recovery from
  83. // CONFERENCE_FAILED caused by
  84. // JitsiConferenceErrors.AUTHENTICATION_REQUIRED.
  85. let recoverable;
  86. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  87. const [ _lobbyJid, lobbyWaitingForHost ] = error.params;
  88. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED
  89. || (error.name === JitsiConferenceErrors.MEMBERS_ONLY_ERROR && lobbyWaitingForHost)) {
  90. if (typeof error.recoverable === 'undefined') {
  91. error.recoverable = true;
  92. }
  93. recoverable = error.recoverable;
  94. }
  95. if (recoverable) {
  96. store.dispatch(waitForOwner());
  97. } else {
  98. store.dispatch(stopWaitForOwner());
  99. }
  100. break;
  101. }
  102. case CONFERENCE_JOINED: {
  103. const { dispatch, getState } = store;
  104. const state = getState();
  105. const config = state['features/base/config'];
  106. if (isTokenAuthEnabled(config)
  107. && config.tokenAuthUrlAutoRedirect
  108. && state['features/base/jwt'].jwt) {
  109. // auto redirect is turned on and we have successfully logged in
  110. // let's mark that
  111. dispatch(setTokenAuthUrlSuccess(true));
  112. }
  113. if (_isWaitingForOwner(store)) {
  114. store.dispatch(stopWaitForOwner());
  115. }
  116. store.dispatch(hideLoginDialog());
  117. break;
  118. }
  119. case CONFERENCE_LEFT:
  120. store.dispatch(stopWaitForOwner());
  121. break;
  122. case CONNECTION_ESTABLISHED:
  123. store.dispatch(hideLoginDialog());
  124. break;
  125. case CONNECTION_FAILED: {
  126. const { error } = action;
  127. const { getState } = store;
  128. const state = getState();
  129. const { jwt } = state['features/base/jwt'];
  130. if (error
  131. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  132. && typeof error.recoverable === 'undefined'
  133. && !jwt) {
  134. error.recoverable = true;
  135. _handleLogin(store);
  136. }
  137. break;
  138. }
  139. case LOGIN: {
  140. _handleLogin(store);
  141. break;
  142. }
  143. case LOGOUT: {
  144. _handleLogout(store);
  145. break;
  146. }
  147. case APP_WILL_NAVIGATE: {
  148. const { dispatch, getState } = store;
  149. const state = getState();
  150. const config = state['features/base/config'];
  151. const room = state['features/base/conference'].room;
  152. if (isRoomValid(room)
  153. && config.tokenAuthUrl && config.tokenAuthUrlAutoRedirect
  154. && state['features/authentication'].tokenAuthUrlSuccessful
  155. && !state['features/base/jwt'].jwt) {
  156. // if we have auto redirect enabled, and we have previously logged in successfully
  157. // we will redirect to the auth url to get the token and login again
  158. // we want to mark token auth success to false as if login is unsuccessful
  159. // the participant can join anonymously and not go in login loop
  160. dispatch(setTokenAuthUrlSuccess(false));
  161. }
  162. break;
  163. }
  164. case STOP_WAIT_FOR_OWNER:
  165. _clearExistingWaitForOwnerTimeout(store);
  166. store.dispatch(hideDialog(WaitForOwnerDialog));
  167. break;
  168. case UPGRADE_ROLE_FINISHED: {
  169. const { error, progress } = action;
  170. if (!error && progress === 1) {
  171. store.dispatch(hideLoginDialog());
  172. }
  173. break;
  174. }
  175. case WAIT_FOR_OWNER: {
  176. _clearExistingWaitForOwnerTimeout(store);
  177. const { handler, timeoutMs }: { handler: () => void; timeoutMs: number; } = action;
  178. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  179. // The WAIT_FOR_OWNER action is cyclic, and we don't want to hide the
  180. // login dialog every few seconds.
  181. isDialogOpen(store, LoginDialog)
  182. || store.dispatch(openWaitForOwnerDialog());
  183. break;
  184. }
  185. }
  186. return next(action);
  187. });
  188. /**
  189. * Will clear the wait for conference owner timeout handler if any is currently
  190. * set.
  191. *
  192. * @param {Object} store - The redux store.
  193. * @returns {void}
  194. */
  195. function _clearExistingWaitForOwnerTimeout({ getState }: IStore) {
  196. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  197. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  198. }
  199. /**
  200. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  201. *
  202. * @param {Object} store - The redux store.
  203. * @returns {boolean}
  204. */
  205. function _isWaitingForOwner({ getState }: IStore) {
  206. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  207. }
  208. /**
  209. * Handles login challenge. Opens login dialog or redirects to token auth URL.
  210. *
  211. * @param {Store} store - The redux store in which the specified {@code action}
  212. * is being dispatched.
  213. * @returns {void}
  214. */
  215. function _handleLogin({ dispatch, getState }: IStore) {
  216. const state = getState();
  217. const config = state['features/base/config'];
  218. const room = state['features/base/conference'].room;
  219. const { locationURL = { href: '' } as URL } = state['features/base/connection'];
  220. const { tenant } = parseURIString(locationURL.href) || {};
  221. const { enabled: audioOnlyEnabled } = state['features/base/audio-only'];
  222. const audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  223. const videoMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.VIDEO);
  224. if (!room) {
  225. logger.warn('Cannot handle login, room is undefined!');
  226. return;
  227. }
  228. if (!isTokenAuthEnabled(config)) {
  229. dispatch(openLoginDialog());
  230. return;
  231. }
  232. getTokenAuthUrl(
  233. config,
  234. locationURL,
  235. {
  236. audioMuted,
  237. audioOnlyEnabled,
  238. skipPrejoin: true,
  239. videoMuted
  240. },
  241. room,
  242. tenant
  243. )
  244. .then((tokenAuthServiceUrl: string | undefined) => {
  245. if (!tokenAuthServiceUrl) {
  246. logger.warn('Cannot handle login, token service URL is not set');
  247. return;
  248. }
  249. return dispatch(openTokenAuthUrl(tokenAuthServiceUrl));
  250. });
  251. }
  252. /**
  253. * Handles logout challenge. Opens logout dialog and hangs up the conference.
  254. *
  255. * @param {Store} store - The redux store in which the specified {@code action}
  256. * is being dispatched.
  257. * @param {string} logoutUrl - The url for logging out.
  258. * @returns {void}
  259. */
  260. function _handleLogout({ dispatch, getState }: IStore) {
  261. const state = getState();
  262. const { conference } = state['features/base/conference'];
  263. if (!conference) {
  264. return;
  265. }
  266. dispatch(openLogoutDialog());
  267. }