From 008bb4f41d80a8cd46febec0795b8c8dfc37ceb0 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 28 Jun 2023 16:42:51 +0100 Subject: [PATCH] Also the other files --- src/livekit/OpenIDLoader.tsx | 55 ++++++++++++++++++++++++++++++++++ src/livekit/openIDSFU.ts | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/livekit/OpenIDLoader.tsx create mode 100644 src/livekit/openIDSFU.ts diff --git a/src/livekit/OpenIDLoader.tsx b/src/livekit/OpenIDLoader.tsx new file mode 100644 index 00000000..9e220034 --- /dev/null +++ b/src/livekit/OpenIDLoader.tsx @@ -0,0 +1,55 @@ +/* +Copyright 2023 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { MatrixClient } from "matrix-js-sdk"; +import React, { useEffect, useState } from "react"; +import { logger } from "matrix-js-sdk/src/logger"; + +import { SFUConfig, getSFUConfigWithOpenID } from "./openIDSFU"; +import { ErrorView, LoadingView } from "../FullScreenView"; +import { ActiveCall, InCallViewProps } from "../room/InCallView"; +import { UserChoices } from "./useLiveKit"; + +interface Props extends Omit { + client: MatrixClient; + roomName: string; + userChoices: UserChoices; +} + +export function OpenIDLoader({ client, roomName, ...rest }: Props) { + const [sfuConfig, setSFUConfig] = useState(); + const [error, setError] = useState(); + + useEffect(() => { + (async () => { + try { + const result = await getSFUConfigWithOpenID(client, roomName); + setSFUConfig(result); + } catch (e) { + logger.error("Failed to fetch SFU config: ", e); + setError(new Error("Failed to fetch SFU config")); + } + })(); + }, [client, roomName]); + + if (error) { + return ; + } else if (sfuConfig) { + return ; + } else { + return ; + } +} diff --git a/src/livekit/openIDSFU.ts b/src/livekit/openIDSFU.ts new file mode 100644 index 00000000..422632a7 --- /dev/null +++ b/src/livekit/openIDSFU.ts @@ -0,0 +1,58 @@ +/* +Copyright 2023 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +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; +} + +export async function getSFUConfigWithOpenID( + client: MatrixClient, + 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", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + room: roomName, + openid_token: openIdToken, + remove_me_user_id: client.getUserId(), // the service will get this from the openid request + device_id: client.getDeviceId(), + }), + }); + if (res.status / 100 !== 2) { + throw new Error("SFO Config fetch failed with status code " + res.status); + } + const sfuConfig = await res.json(); + + return sfuConfig; +}