Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
feat: Check jwt expiration and redirects to auth url if any. (#13879)
Дамян Минковa843406cb099
diff --git a/react/features/app/actions.any.ts b/react/features/app/actions.any.tsindex 937e95b999..ac562a0393 100644--- a/react/features/app/actions.any.ts+++ b/react/features/app/actions.any.ts@@ -1,6 +1,14 @@-import { getLocationContextRoot } from '../base/util/uri';+// @ts-ignore+// eslint-disable-next-line+import { openTokenAuthUrl } from '../authentication/actions';++// @ts-ignore+import { getTokenAuthUrl, isTokenAuthEnabled } from '../authentication/functions';+import { getJwtExpirationDate } from '../base/jwt/functions';+import { getLocationContextRoot, parseURIString } from '../base/util/uri';import { addTrackStateToURL } from './functions.any';+import logger from './logger';import { IStore } from './types';/**@@ -83,4 +91,53 @@ export function reloadWithStoredParams() {};}+/**+ * Checks whether tokenAuthUrl is set, we have a jwt token that will expire soon+ * and redirect to the auth url to obtain new token if this is the case.+ *+ * @param {Dispatch} dispatch - The Redux dispatch function.+ * @param {Function} getState - The Redux state.+ * @param {Function} failureCallback - The callback on failure to obtain auth url.+ * @returns {boolean} Whether we will redirect or not.+ */+export function maybeRedirectToTokenAuthUrl(+ dispatch: IStore['dispatch'], getState: IStore['getState'], failureCallback: Function) {+ const state = getState();+ const config = state['features/base/config'];+ const { locationURL = { href: '' } } = state['features/base/connection'];++ if (!isTokenAuthEnabled(config)) {+ return false;+ }++ // if tokenAuthUrl check jwt if is about to expire go through the url to get new token+ const jwt = state['features/base/jwt'].jwt;+ const expirationDate = getJwtExpirationDate(jwt);++ // if there is jwt and its expiration time is less than 3 minutes away+ // let's obtain new token+ if (expirationDate && expirationDate.getTime() - Date.now() < 3 * 60 * 1000) {+ const room = state['features/base/conference'].room;+ const { tenant } = parseURIString(locationURL.href) || {};++ getTokenAuthUrl(config, room, tenant, true)+ .then((tokenAuthServiceUrl: string | undefined) => {+ if (!tokenAuthServiceUrl) {+ logger.warn('Cannot handle login, token service URL is not set');++ return Promise.reject();+ }++ return dispatch(openTokenAuthUrl(tokenAuthServiceUrl));+ })+ .catch(() => {+ failureCallback();+ });++ return true;+ }++ return false;+}+diff --git a/react/features/app/actions.native.ts b/react/features/app/actions.native.tsindex 6606d3e36d..23ef235c14 100644--- a/react/features/app/actions.native.ts+++ b/react/features/app/actions.native.ts@@ -31,11 +31,11 @@ import { screen } from '../mobile/navigation/routes';import { clearNotifications } from '../notifications/actions';import { isUnsafeRoomWarningEnabled } from '../prejoin/functions';+import { maybeRedirectToTokenAuthUrl } from './actions.any';import { addTrackStateToURL, getDefaultURL } from './functions.native';import logger from './logger';import { IReloadNowOptions, IStore } from './types';-export * from './actions.any';/**@@ -205,10 +205,18 @@ export function reloadNow() {// @ts-ignoreconst newURL = addTrackStateToURL(locationURL, state);- logger.info(`Reloading the conference using URL: ${locationURL}`);+ const reloadAction = () => {+ logger.info(`Reloading the conference using URL: ${locationURL}`);++ dispatch(appNavigate(toURLString(newURL), {+ hidePrejoin: true+ }));+ };++ if (maybeRedirectToTokenAuthUrl(dispatch, getState, reloadAction)) {+ return;+ }- dispatch(appNavigate(toURLString(newURL), {- hidePrejoin: true- }));+ reloadAction();};}diff --git a/react/features/app/actions.web.ts b/react/features/app/actions.web.tsindex 1e87f6f323..476b4148f0 100644--- a/react/features/app/actions.web.ts+++ b/react/features/app/actions.web.ts@@ -15,6 +15,7 @@ import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';import { isWelcomePageEnabled } from '../welcome/functions';import {+ maybeRedirectToTokenAuthUrl,redirectToStaticPage,redirectWithStoredParams,reloadWithStoredParams@@ -170,8 +171,16 @@ export function reloadNow() {const state = getState();const { locationURL } = state['features/base/connection'];- logger.info(`Reloading the conference using URL: ${locationURL}`);+ const reloadAction = () => {+ logger.info(`Reloading the conference using URL: ${locationURL}`);- dispatch(reloadWithStoredParams());+ dispatch(reloadWithStoredParams());+ };++ if (maybeRedirectToTokenAuthUrl(dispatch, getState, reloadAction)) {+ return;+ }++ reloadAction();};}diff --git a/react/features/base/jwt/functions.ts b/react/features/base/jwt/functions.tsindex ec21435f3e..a500103ade 100644--- a/react/features/base/jwt/functions.ts+++ b/react/features/base/jwt/functions.ts@@ -186,3 +186,23 @@ export function validateJwt(jwt: string) {return errors;}++/**+ * Extracts and returns the expiration date of jwt.+ *+ * @param {string|undefined} jwt - The jwt to check.+ * @returns {Date} The expiration date of the jwt.+ */+export function getJwtExpirationDate(jwt: string | undefined) {+ if (!jwt) {+ return;+ }++ const payload = jwtDecode(jwt);++ if (payload) {+ const { exp } = payload;++ return new Date(exp * 1000);+ }+}