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

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