Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(JaaS-JWT) Get JWT for JaaS (#9512)

robertpinbad58f6508b5
diff --git a/connection.js b/connection.js
index 2baf47ca5b..095edf1e20 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<JitsiConnection>} 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 627b7b1a07..375cf2078c 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);
+ }
+ }
+}