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

* Added get JWT for JaaS

* Code review fix
This commit is contained in:
robertpin 2021-07-09 09:30:49 +03:00 committed by GitHub
parent dc60418613
commit bad58f6508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 2 deletions

View File

@ -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.

View File

@ -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);
}
}
}