2023-08-08 13:46:51 +02:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-08-11 16:57:55 +02:00
|
|
|
import { useEffect, useMemo } from "react";
|
2023-11-16 16:32:25 +00:00
|
|
|
import { Room } from "matrix-js-sdk";
|
2023-08-08 13:46:51 +02:00
|
|
|
|
2023-10-09 17:43:50 +01:00
|
|
|
import { setLocalStorageItem, useLocalStorage } from "../useLocalStorage";
|
2023-09-22 18:20:29 +01:00
|
|
|
import { useClient } from "../ClientContext";
|
2023-11-16 16:32:25 +00:00
|
|
|
import { UrlParams, getUrlParams, useUrlParams } from "../UrlParams";
|
2023-09-18 20:47:47 -04:00
|
|
|
import { widget } from "../widget";
|
2023-08-09 13:29:45 +02:00
|
|
|
|
2023-11-16 16:32:25 +00:00
|
|
|
export function saveKeyForRoom(roomId: string, password: string): void {
|
|
|
|
|
setLocalStorageItem(getRoomSharedKeyLocalStorageKey(roomId), password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getRoomSharedKeyLocalStorageKey = (roomId: string): string =>
|
2023-08-09 13:29:45 +02:00
|
|
|
`room-shared-key-${roomId}`;
|
|
|
|
|
|
2023-10-09 17:43:50 +01:00
|
|
|
const useInternalRoomSharedKey = (roomId: string): string | null => {
|
2023-10-11 12:53:33 +01:00
|
|
|
const key = getRoomSharedKeyLocalStorageKey(roomId);
|
2023-10-09 17:43:50 +01:00
|
|
|
const roomSharedKey = useLocalStorage(key)[0];
|
2023-08-09 13:29:45 +02:00
|
|
|
|
2023-10-13 15:26:30 +01:00
|
|
|
return roomSharedKey;
|
2023-08-09 13:29:45 +02:00
|
|
|
};
|
2023-08-11 16:57:55 +02:00
|
|
|
|
2023-11-16 16:32:25 +00:00
|
|
|
export function getKeyForRoom(roomId: string): string | null {
|
|
|
|
|
saveKeyFromUrlParams(getUrlParams());
|
|
|
|
|
const key = getRoomSharedKeyLocalStorageKey(roomId);
|
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveKeyFromUrlParams(urlParams: UrlParams): void {
|
|
|
|
|
if (!urlParams.password || !urlParams.roomId) return;
|
|
|
|
|
|
2023-11-21 09:59:07 +00:00
|
|
|
// Take the key from the URL and save it.
|
|
|
|
|
// It's important to always use the room ID specified in the URL
|
|
|
|
|
// when saving keys rather than whatever the current room ID might be,
|
|
|
|
|
// in case we've moved to a different room but the URL hasn't changed.
|
2023-11-16 16:32:25 +00:00
|
|
|
saveKeyForRoom(urlParams.roomId, urlParams.password);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 12:53:33 +01:00
|
|
|
/**
|
|
|
|
|
* Extracts the room password from the URL if one is present, saving it in localstorage
|
|
|
|
|
* and returning it in a tuple with the corresponding room ID from the URL.
|
|
|
|
|
* @returns A tuple of the roomId and password from the URL if the URL has both,
|
|
|
|
|
* otherwise [undefined, undefined]
|
|
|
|
|
*/
|
|
|
|
|
const useKeyFromUrl = (): [string, string] | [undefined, undefined] => {
|
2023-09-20 16:25:02 +01:00
|
|
|
const urlParams = useUrlParams();
|
|
|
|
|
|
2023-11-16 16:32:25 +00:00
|
|
|
useEffect(() => saveKeyFromUrlParams(urlParams), [urlParams]);
|
2023-09-20 17:05:10 +01:00
|
|
|
|
2023-10-11 12:53:33 +01:00
|
|
|
return urlParams.roomId && urlParams.password
|
|
|
|
|
? [urlParams.roomId, urlParams.password]
|
|
|
|
|
: [undefined, undefined];
|
2023-09-20 16:25:02 +01:00
|
|
|
};
|
|
|
|
|
|
2023-10-11 12:53:33 +01:00
|
|
|
export const useRoomSharedKey = (roomId: string): string | undefined => {
|
2023-09-20 16:25:02 +01:00
|
|
|
// make sure we've extracted the key from the URL first
|
2023-09-20 17:05:10 +01:00
|
|
|
// (and we still need to take the value it returns because
|
|
|
|
|
// the effect won't run in time for it to save to localstorage in
|
|
|
|
|
// time for us to read it out again).
|
2023-10-11 12:53:33 +01:00
|
|
|
const [urlRoomId, passwordFormUrl] = useKeyFromUrl();
|
2023-09-20 16:25:02 +01:00
|
|
|
|
2023-10-11 12:53:33 +01:00
|
|
|
const storedPassword = useInternalRoomSharedKey(roomId);
|
|
|
|
|
|
|
|
|
|
if (storedPassword) return storedPassword;
|
|
|
|
|
if (urlRoomId === roomId) return passwordFormUrl;
|
|
|
|
|
return undefined;
|
2023-08-11 16:57:55 +02:00
|
|
|
};
|
|
|
|
|
|
2023-09-22 18:20:29 +01:00
|
|
|
export const useIsRoomE2EE = (roomId: string): boolean | null => {
|
|
|
|
|
const { client } = useClient();
|
2023-11-16 16:32:25 +00:00
|
|
|
const room = useMemo(() => client?.getRoom(roomId), [roomId, client]);
|
|
|
|
|
|
|
|
|
|
return useMemo(() => !room || isRoomE2EE(room), [room]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function isRoomE2EE(room: Room): boolean {
|
2023-09-18 20:47:47 -04:00
|
|
|
// For now, rooms in widget mode are never considered encrypted.
|
|
|
|
|
// In the future, when widget mode gains encryption support, then perhaps we
|
|
|
|
|
// should inspect the e2eEnabled URL parameter here?
|
2023-11-16 16:32:25 +00:00
|
|
|
return widget === null && !room.getCanonicalAlias();
|
|
|
|
|
}
|