From bad58f6508b5fde05a293ac0d790f758c5a39a1a Mon Sep 17 00:00:00 2001 From: robertpin Date: Fri, 9 Jul 2021 09:30:49 +0300 Subject: [PATCH] feat(JaaS-JWT) Get JWT for JaaS (#9512) * Added get JWT for JaaS * Code review fix --- connection.js | 13 +++++-- react/features/jaas/functions.js | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/connection.js b/connection.js index 2baf47ca5..095edf1e2 100644 --- a/connection.js +++ b/connection.js @@ -11,11 +11,14 @@ import { connectionFailed } from './react/features/base/connection/actions'; import { openDialog } from './react/features/base/dialog/actions'; +import { setJWT } from './react/features/base/jwt'; import { isFatalJitsiConnectionError, JitsiConnectionErrors, JitsiConnectionEvents } from './react/features/base/lib-jitsi-meet'; +import { isVpaasMeeting } from './react/features/billing-counter/functions'; +import { getJaasJWT } from './react/features/jaas/functions'; import { setPrejoinDisplayNameRequired } from './react/features/prejoin/actions'; const logger = Logger.getLogger(__filename); @@ -82,9 +85,15 @@ function checkForAttachParametersAndConnect(id, password, connection) { * @returns {Promise} connection if * everything is ok, else error. */ -export function connect(id, password, roomName) { +export async function connect(id, password, roomName) { const connectionConfig = Object.assign({}, config); - const { jwt } = APP.store.getState()['features/base/jwt']; + const state = APP.store.getState(); + let { jwt } = state['features/base/jwt']; + + if (!jwt && isVpaasMeeting(state)) { + jwt = await getJaasJWT(state); + APP.store.dispatch(setJWT(jwt)); + } // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified. diff --git a/react/features/jaas/functions.js b/react/features/jaas/functions.js index 627b7b1a0..375cf2078 100644 --- a/react/features/jaas/functions.js +++ b/react/features/jaas/functions.js @@ -1,5 +1,9 @@ // @flow +import { getVpaasTenant } from '../billing-counter/functions'; + +import logger from './logger'; + /** * Sends a request for retrieving jaas customer details. * @@ -46,3 +50,59 @@ export async function sendGetDetailsRequest({ appId, baseUrl, jwt }: { export function isFeatureDisabled(state: Object, feature: string) { return state['features/jaas'].disabledFeatures.includes(feature); } + +/** + * Sends a request for retrieving jaas JWT. + * + * @param {Object} reqData - The request info. + * @param {string} reqData.appId - The client appId. + * @param {string} reqData.baseUrl - The base url for the request. + * @returns {void} + */ +export async function sendGetJWTRequest({ appId, baseUrl }: { + appId: string, + baseUrl: string +}) { + const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`; + + try { + const res = await fetch(fullUrl, { + method: 'GET' + }); + + if (res.ok) { + return res.json(); + } + + throw new Error('Request not successful'); + } catch (err) { + throw new Error(err); + + } +} + +/** + * Gets a jaas JWT. + * + * @param {Object} state - Redux state. + * @returns {string} The JWT. + */ +export async function getJaasJWT(state: Object) { + const baseUrl = state['features/base/config'].jaasTokenUrl; + const appId = getVpaasTenant(state); + + const shouldSendRequest = Boolean(baseUrl && appId); + + if (shouldSendRequest) { + try { + const jwt = await sendGetJWTRequest({ + appId, + baseUrl + }); + + return jwt.token; + } catch (err) { + logger.error('Could not send request', err); + } + } +}