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.

actions.any.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // @ts-ignore
  2. // eslint-disable-next-line
  3. import { openTokenAuthUrl } from '../authentication/actions';
  4. // @ts-ignore
  5. import { getTokenAuthUrl, isTokenAuthEnabled } from '../authentication/functions';
  6. import { getJwtExpirationDate } from '../base/jwt/functions';
  7. import { getLocationContextRoot, parseURIString } from '../base/util/uri';
  8. import { addTrackStateToURL } from './functions.any';
  9. import logger from './logger';
  10. import { IStore } from './types';
  11. /**
  12. * Redirects to another page generated by replacing the path in the original URL
  13. * with the given path.
  14. *
  15. * @param {(string)} pathname - The path to navigate to.
  16. * @returns {Function}
  17. */
  18. export function redirectWithStoredParams(pathname: string) {
  19. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  20. const { locationURL } = getState()['features/base/connection'];
  21. const newLocationURL = new URL(locationURL?.href ?? '');
  22. newLocationURL.pathname = pathname;
  23. window.location.assign(newLocationURL.toString());
  24. };
  25. }
  26. /**
  27. * Assigns a specific pathname to window.location.pathname taking into account
  28. * the context root of the Web app.
  29. *
  30. * @param {string} pathname - The pathname to assign to
  31. * window.location.pathname. If the specified pathname is relative, the context
  32. * root of the Web app will be prepended to the specified pathname before
  33. * assigning it to window.location.pathname.
  34. * @param {string} hashParam - Optional hash param to assign to
  35. * window.location.hash.
  36. * @returns {Function}
  37. */
  38. export function redirectToStaticPage(pathname: string, hashParam?: string) {
  39. return () => {
  40. const windowLocation = window.location;
  41. let newPathname = pathname;
  42. if (!newPathname.startsWith('/')) {
  43. // A pathname equal to ./ specifies the current directory. It will be
  44. // fine but pointless to include it because contextRoot is the current
  45. // directory.
  46. newPathname.startsWith('./')
  47. && (newPathname = newPathname.substring(2));
  48. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  49. }
  50. if (hashParam) {
  51. windowLocation.hash = hashParam;
  52. }
  53. windowLocation.pathname = newPathname;
  54. };
  55. }
  56. /**
  57. * Reloads the page by restoring the original URL.
  58. *
  59. * @returns {Function}
  60. */
  61. export function reloadWithStoredParams() {
  62. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  63. const state = getState();
  64. const { locationURL } = state['features/base/connection'];
  65. // Preserve the local tracks muted states.
  66. // @ts-ignore
  67. const newURL = addTrackStateToURL(locationURL, state);
  68. const windowLocation = window.location;
  69. const oldSearchString = windowLocation.search;
  70. windowLocation.replace(newURL.toString());
  71. if (newURL.search === oldSearchString) {
  72. // NOTE: Assuming that only the hash or search part of the URL will
  73. // be changed!
  74. // location.replace will not trigger redirect/reload when
  75. // only the hash params are changed. That's why we need to call
  76. // reload in addition to replace.
  77. windowLocation.reload();
  78. }
  79. };
  80. }
  81. /**
  82. * Checks whether tokenAuthUrl is set, we have a jwt token that will expire soon
  83. * and redirect to the auth url to obtain new token if this is the case.
  84. *
  85. * @param {Dispatch} dispatch - The Redux dispatch function.
  86. * @param {Function} getState - The Redux state.
  87. * @param {Function} failureCallback - The callback on failure to obtain auth url.
  88. * @returns {boolean} Whether we will redirect or not.
  89. */
  90. export function maybeRedirectToTokenAuthUrl(
  91. dispatch: IStore['dispatch'], getState: IStore['getState'], failureCallback: Function) {
  92. const state = getState();
  93. const config = state['features/base/config'];
  94. const { locationURL = { href: '' } as URL } = state['features/base/connection'];
  95. if (!isTokenAuthEnabled(config)) {
  96. return false;
  97. }
  98. // if tokenAuthUrl check jwt if is about to expire go through the url to get new token
  99. const jwt = state['features/base/jwt'].jwt;
  100. const expirationDate = getJwtExpirationDate(jwt);
  101. // if there is jwt and its expiration time is less than 3 minutes away
  102. // let's obtain new token
  103. if (expirationDate && expirationDate.getTime() - Date.now() < 3 * 60 * 1000) {
  104. const room = state['features/base/conference'].room;
  105. const { tenant } = parseURIString(locationURL.href) || {};
  106. getTokenAuthUrl(config, room, tenant, true, locationURL)
  107. .then((tokenAuthServiceUrl: string | undefined) => {
  108. if (!tokenAuthServiceUrl) {
  109. logger.warn('Cannot handle login, token service URL is not set');
  110. return Promise.reject();
  111. }
  112. return dispatch(openTokenAuthUrl(tokenAuthServiceUrl));
  113. })
  114. .catch(() => {
  115. failureCallback();
  116. });
  117. return true;
  118. }
  119. return false;
  120. }