From f2eabec382b803e4be4642075f43f99191d224a4 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 3 Jul 2023 16:21:56 +0100 Subject: [PATCH] Be stricter with what is passed in to the openid components --- src/livekit/OpenIDLoader.tsx | 25 +++++++++++++++++++------ src/livekit/openIDSFU.ts | 19 +++++++++---------- src/room/GroupCallView.tsx | 13 ++++++++++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/livekit/OpenIDLoader.tsx b/src/livekit/OpenIDLoader.tsx index 34aa5eca..df692f6d 100644 --- a/src/livekit/OpenIDLoader.tsx +++ b/src/livekit/OpenIDLoader.tsx @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { MatrixClient } from "matrix-js-sdk"; import React, { ReactNode, createContext, @@ -24,11 +23,16 @@ import React, { } from "react"; import { logger } from "matrix-js-sdk/src/logger"; -import { SFUConfig, getSFUConfigWithOpenID } from "./openIDSFU"; +import { + OpenIDClientParts, + SFUConfig, + getSFUConfigWithOpenID, +} from "./openIDSFU"; import { ErrorView, LoadingView } from "../FullScreenView"; interface Props { - client: MatrixClient; + client: OpenIDClientParts; + livekitServiceURL: string; roomName: string; children: ReactNode; } @@ -37,21 +41,30 @@ const SFUConfigContext = createContext(undefined); export const useSFUConfig = () => useContext(SFUConfigContext); -export function OpenIDLoader({ client, roomName, children }: Props) { +export function OpenIDLoader({ + client, + livekitServiceURL, + roomName, + children, +}: Props) { const [sfuConfig, setSFUConfig] = useState(); const [error, setError] = useState(); useEffect(() => { (async () => { try { - const result = await getSFUConfigWithOpenID(client, roomName); + const result = await getSFUConfigWithOpenID( + client, + livekitServiceURL, + roomName + ); setSFUConfig(result); } catch (e) { logger.error("Failed to fetch SFU config: ", e); setError(new Error("Failed to fetch SFU config")); } })(); - }, [client, roomName]); + }, [client, livekitServiceURL, roomName]); if (error) { return ; diff --git a/src/livekit/openIDSFU.ts b/src/livekit/openIDSFU.ts index 59a5da52..9d46c518 100644 --- a/src/livekit/openIDSFU.ts +++ b/src/livekit/openIDSFU.ts @@ -17,27 +17,26 @@ limitations under the License. import { MatrixClient } from "matrix-js-sdk"; import { logger } from "matrix-js-sdk/src/logger"; -import { Config } from "../config/Config"; - export interface SFUConfig { url: string; jwt: string; } +// The bits we need from MatrixClient +export type OpenIDClientParts = Pick< + MatrixClient, + "getOpenIdToken" | "getDeviceId" +>; + export async function getSFUConfigWithOpenID( - client: MatrixClient, + client: OpenIDClientParts, + livekitServiceURL: string, roomName: string ): Promise { const openIdToken = await client.getOpenIdToken(); logger.debug("Got openID token", openIdToken); - const livekitCfg = Config.get().livekit; - - if (!livekitCfg?.livekit_service_url) { - throw new Error("No livekit service URL defined"); - } - - const res = await fetch(livekitCfg.livekit_service_url + "/sfu/get", { + const res = await fetch(livekitServiceURL + "/sfu/get", { method: "POST", headers: { "Content-Type": "application/json", diff --git a/src/room/GroupCallView.tsx b/src/room/GroupCallView.tsx index 888dcbf4..6df1246a 100644 --- a/src/room/GroupCallView.tsx +++ b/src/room/GroupCallView.tsx @@ -36,6 +36,7 @@ import { UserChoices } from "../livekit/useLiveKit"; import { findDeviceByName } from "../media-utils"; import { OpenIDLoader } from "../livekit/OpenIDLoader"; import { ActiveCall } from "./InCallView"; +import { Config } from "../config/Config"; declare global { interface Window { @@ -219,11 +220,21 @@ export function GroupCallView({ undefined ); + const lkServiceURL = Config.get().livekit?.livekit_service_url; + + if (!lkServiceURL) { + return ; + } + if (error) { return ; } else if (state === GroupCallState.Entered && userChoices) { return ( - +