2016-11-02 15:10:21 +00:00
|
|
|
/*
|
2016-11-03 11:47:57 +00:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-12-18 17:40:30 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-03-02 14:59:54 +00:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2016-11-02 15:10:21 +00:00
|
|
|
|
|
|
|
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-04 07:36:16 +00:00
|
|
|
import { MatrixClient, MatrixEvent, Room, SSOAction } from "matrix-js-sdk/src/matrix";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { encodeUnpaddedBase64 } from "matrix-js-sdk/src/crypto/olmlib";
|
2021-12-03 11:02:47 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2019-11-19 11:52:12 +00:00
|
|
|
import BaseEventIndexManager from "./indexing/BaseEventIndexManager";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
|
|
|
import { CheckUpdatesPayload } from "./dispatcher/payloads/CheckUpdatesPayload";
|
|
|
|
import { Action } from "./dispatcher/actions";
|
|
|
|
import { hideToast as hideUpdateToast } from "./toasts/UpdateToast";
|
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
|
|
|
import { idbLoad, idbSave, idbDelete } from "./utils/StorageManager";
|
2022-02-10 14:29:55 +00:00
|
|
|
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
|
2022-03-18 16:12:36 +00:00
|
|
|
import { IConfigOptions } from "./IConfigOptions";
|
2020-05-29 17:24:45 +00:00
|
|
|
|
2020-06-25 20:59:46 +00:00
|
|
|
export const SSO_HOMESERVER_URL_KEY = "mx_sso_hs_url";
|
|
|
|
export const SSO_ID_SERVER_URL_KEY = "mx_sso_is_url";
|
2021-02-01 16:25:50 +00:00
|
|
|
export const SSO_IDP_ID_KEY = "mx_sso_idp_id";
|
2020-06-02 15:26:07 +00:00
|
|
|
|
2020-05-29 17:24:45 +00:00
|
|
|
export enum UpdateCheckStatus {
|
|
|
|
Checking = "CHECKING",
|
|
|
|
Error = "ERROR",
|
|
|
|
NotAvailable = "NOTAVAILABLE",
|
|
|
|
Downloading = "DOWNLOADING",
|
|
|
|
Ready = "READY",
|
|
|
|
}
|
2017-06-20 17:47:35 +00:00
|
|
|
|
2022-06-10 21:38:50 +00:00
|
|
|
export interface UpdateStatus {
|
|
|
|
/**
|
|
|
|
* The current phase of the manual update check.
|
|
|
|
*/
|
|
|
|
status: UpdateCheckStatus;
|
|
|
|
/**
|
|
|
|
* Detail string relating to the current status, typically for error details.
|
|
|
|
*/
|
|
|
|
detail?: string;
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:59:47 +00:00
|
|
|
const UPDATE_DEFER_KEY = "mx_defer_update";
|
|
|
|
|
2016-11-02 15:10:21 +00:00
|
|
|
/**
|
|
|
|
* Base class for classes that provide platform-specific functionality
|
|
|
|
* eg. Setting an application badge or displaying notifications
|
|
|
|
*
|
|
|
|
* Instances of this class are provided by the application.
|
|
|
|
*/
|
2020-05-21 17:06:36 +00:00
|
|
|
export default abstract class BasePlatform {
|
2020-05-21 17:11:21 +00:00
|
|
|
protected notificationCount = 0;
|
|
|
|
protected errorDidOccur = false;
|
2017-06-20 17:47:35 +00:00
|
|
|
|
2023-08-15 08:43:15 +00:00
|
|
|
protected constructor() {
|
2020-05-21 17:06:36 +00:00
|
|
|
dis.register(this.onAction);
|
2020-06-02 15:26:07 +00:00
|
|
|
this.startUpdateCheck = this.startUpdateCheck.bind(this);
|
2017-06-20 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 07:29:01 +00:00
|
|
|
public abstract getConfig(): Promise<IConfigOptions | undefined>;
|
2020-07-02 22:14:31 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public abstract getDefaultDeviceDisplayName(): string;
|
2020-07-02 22:14:31 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
protected onAction = (payload: ActionPayload): void => {
|
2017-06-20 17:47:35 +00:00
|
|
|
switch (payload.action) {
|
2019-07-03 22:46:37 +00:00
|
|
|
case "on_client_not_viable":
|
2022-05-26 08:56:53 +00:00
|
|
|
case Action.OnLoggedOut:
|
2017-06-20 17:47:35 +00:00
|
|
|
this.setNotificationCount(0);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-21 17:06:36 +00:00
|
|
|
};
|
2016-11-02 15:10:21 +00:00
|
|
|
|
2017-05-29 18:50:04 +00:00
|
|
|
// Used primarily for Analytics
|
2022-06-06 06:31:20 +00:00
|
|
|
public abstract getHumanReadableName(): string;
|
2017-05-29 18:50:04 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public setNotificationCount(count: number): void {
|
2016-11-02 15:10:21 +00:00
|
|
|
this.notificationCount = count;
|
|
|
|
}
|
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public setErrorStatus(errorDidOccur: boolean): void {
|
2016-11-02 15:10:21 +00:00
|
|
|
this.errorDidOccur = errorDidOccur;
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:24:45 +00:00
|
|
|
/**
|
|
|
|
* Whether we can call checkForUpdate on this platform build
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public async canSelfUpdate(): Promise<boolean> {
|
2020-05-29 17:24:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public startUpdateCheck(): void {
|
2020-05-29 18:59:47 +00:00
|
|
|
hideUpdateToast();
|
|
|
|
localStorage.removeItem(UPDATE_DEFER_KEY);
|
2020-05-29 17:24:45 +00:00
|
|
|
dis.dispatch<CheckUpdatesPayload>({
|
|
|
|
action: Action.CheckUpdates,
|
|
|
|
status: UpdateCheckStatus.Checking,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:59:47 +00:00
|
|
|
/**
|
|
|
|
* Update the currently running app to the latest available version
|
|
|
|
* and replace this instance of the app with the new version.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public installUpdate(): void {}
|
2020-05-29 18:59:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the version update has been deferred and that deferment is still in effect
|
|
|
|
* @param newVersion the version string to check
|
|
|
|
*/
|
|
|
|
protected shouldShowUpdate(newVersion: string): boolean {
|
2020-11-02 17:25:48 +00:00
|
|
|
// If the user registered on this client in the last 24 hours then do not show them the update toast
|
|
|
|
if (MatrixClientPeg.userRegisteredWithinLastHours(24)) return false;
|
|
|
|
|
2020-05-29 18:59:47 +00:00
|
|
|
try {
|
2023-02-03 15:27:47 +00:00
|
|
|
const [version, deferUntil] = JSON.parse(localStorage.getItem(UPDATE_DEFER_KEY)!);
|
2020-05-29 18:59:47 +00:00
|
|
|
return newVersion !== version || Date.now() > deferUntil;
|
|
|
|
} catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignore the pending update and don't prompt about this version
|
|
|
|
* until the next morning (8am).
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public deferUpdate(newVersion: string): void {
|
2020-05-29 18:59:47 +00:00
|
|
|
const date = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
|
|
date.setHours(8, 0, 0, 0); // set to next 8am
|
|
|
|
localStorage.setItem(UPDATE_DEFER_KEY, JSON.stringify([newVersion, date.getTime()]));
|
|
|
|
hideUpdateToast();
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:14:58 +00:00
|
|
|
/**
|
|
|
|
* Return true if platform supports multi-language
|
|
|
|
* spell-checking, otherwise false.
|
|
|
|
*/
|
2022-07-28 08:10:04 +00:00
|
|
|
public supportsSpellCheckSettings(): boolean {
|
2020-12-02 19:14:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:22:59 +00:00
|
|
|
/**
|
|
|
|
* Returns true if platform allows overriding native context menus
|
|
|
|
*/
|
|
|
|
public allowOverridingNativeContextMenus(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 17:35:31 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the platform supports displaying
|
|
|
|
* notifications, otherwise false.
|
2017-07-01 13:15:26 +00:00
|
|
|
* @returns {boolean} whether the platform supports displaying notifications
|
2016-11-02 17:35:31 +00:00
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public supportsNotifications(): boolean {
|
2016-11-02 17:35:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the application currently has permission
|
|
|
|
* to display notifications. Otherwise false.
|
2017-07-01 13:15:26 +00:00
|
|
|
* @returns {boolean} whether the application has permission to display notifications
|
2016-11-02 17:35:31 +00:00
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public maySendNotifications(): boolean {
|
2016-11-02 17:35:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests permission to send notifications. Returns
|
|
|
|
* a promise that is resolved when the user has responded
|
|
|
|
* to the request. The promise has a single string argument
|
|
|
|
* that is 'granted' if the user allowed the request or
|
|
|
|
* 'denied' otherwise.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public abstract requestNotificationPermission(): Promise<string>;
|
2016-11-02 17:35:31 +00:00
|
|
|
|
2021-12-15 08:34:52 +00:00
|
|
|
public displayNotification(
|
|
|
|
title: string,
|
|
|
|
msg: string,
|
2023-02-15 13:36:22 +00:00
|
|
|
avatarUrl: string | null,
|
2021-12-15 08:34:52 +00:00
|
|
|
room: Room,
|
|
|
|
ev?: MatrixEvent,
|
|
|
|
): Notification {
|
2023-02-13 11:39:16 +00:00
|
|
|
const notifBody: NotificationOptions = {
|
2021-12-15 08:34:52 +00:00
|
|
|
body: msg,
|
|
|
|
silent: true, // we play our own sounds
|
|
|
|
};
|
|
|
|
if (avatarUrl) notifBody["icon"] = avatarUrl;
|
|
|
|
const notification = new window.Notification(title, notifBody);
|
|
|
|
|
|
|
|
notification.onclick = () => {
|
2022-02-10 14:29:55 +00:00
|
|
|
const payload: ViewRoomPayload = {
|
2021-12-15 08:34:52 +00:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
2022-02-17 18:03:27 +00:00
|
|
|
metricsTrigger: "Notification",
|
2021-12-15 08:34:52 +00:00
|
|
|
};
|
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
if (ev?.getThread()) {
|
2021-12-15 08:34:52 +00:00
|
|
|
payload.event_id = ev.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
dis.dispatch(payload);
|
|
|
|
window.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
2016-11-08 10:45:19 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public loudNotification(ev: MatrixEvent, room: Room): void {}
|
2020-08-12 11:16:28 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public clearNotification(notif: Notification): void {
|
2020-08-12 11:16:28 +00:00
|
|
|
// Some browsers don't support this, e.g Safari on iOS
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Notification/close
|
|
|
|
if (notif.close) {
|
|
|
|
notif.close();
|
|
|
|
}
|
2020-08-05 10:07:10 +00:00
|
|
|
}
|
2017-05-31 22:49:55 +00:00
|
|
|
|
2022-07-06 09:43:30 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the platform requires URL previews in tooltips, otherwise false.
|
|
|
|
* @returns {boolean} whether the platform requires URL previews in tooltips
|
|
|
|
*/
|
|
|
|
public needsUrlTooltips(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:45:19 +00:00
|
|
|
/**
|
2020-05-21 17:06:36 +00:00
|
|
|
* Returns a promise that resolves to a string representing the current version of the application.
|
2016-11-08 10:45:19 +00:00
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public abstract getAppVersion(): Promise<string>;
|
2017-01-10 18:37:57 +00:00
|
|
|
|
2017-04-10 16:39:27 +00:00
|
|
|
/**
|
2022-05-09 22:52:05 +00:00
|
|
|
* Restarts the application, without necessarily reloading
|
2017-04-10 16:39:27 +00:00
|
|
|
* any application code
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public abstract reload(): void;
|
2019-02-24 01:06:53 +00:00
|
|
|
|
2022-06-10 21:38:50 +00:00
|
|
|
public supportsSetting(settingName?: string): boolean {
|
2022-05-23 10:23:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
public async getSettingValue(settingName: string): Promise<any> {
|
2022-06-10 21:38:50 +00:00
|
|
|
return undefined;
|
2022-05-23 10:23:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 21:38:50 +00:00
|
|
|
public setSettingValue(settingName: string, value: any): Promise<void> {
|
2022-05-23 10:23:40 +00:00
|
|
|
throw new Error("Unimplemented");
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:21:26 +00:00
|
|
|
/**
|
|
|
|
* Get our platform specific EventIndexManager.
|
|
|
|
*
|
|
|
|
* @return {BaseEventIndexManager} The EventIndex manager for our platform,
|
|
|
|
* can be null if the platform doesn't support event indexing.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public getEventIndexingManager(): BaseEventIndexManager | null {
|
2019-11-13 11:25:16 +00:00
|
|
|
return null;
|
2019-10-11 14:07:59 +00:00
|
|
|
}
|
2020-02-24 17:11:08 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public setLanguage(preferredLangs: string[]): void {}
|
2020-03-02 14:59:54 +00:00
|
|
|
|
2022-07-28 08:10:04 +00:00
|
|
|
public setSpellCheckEnabled(enabled: boolean): void {}
|
|
|
|
|
|
|
|
public async getSpellCheckEnabled(): Promise<boolean> {
|
2023-02-03 15:27:47 +00:00
|
|
|
return false;
|
2022-07-28 08:10:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public setSpellCheckLanguages(preferredLangs: string[]): void {}
|
2020-12-01 19:17:24 +00:00
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public getSpellCheckLanguages(): Promise<string[]> | null {
|
2021-02-18 19:12:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public async getDesktopCapturerSources(options: GetSourcesOptions): Promise<Array<DesktopCapturerSource>> {
|
2022-02-11 10:41:15 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public supportsDesktopCapturer(): boolean {
|
2022-02-11 10:41:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:23:35 +00:00
|
|
|
public supportsJitsiScreensharing(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-23 09:12:04 +00:00
|
|
|
public overrideBrowserShortcuts(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public navigateForwardBack(back: boolean): void {}
|
|
|
|
|
2022-06-06 06:31:20 +00:00
|
|
|
public getAvailableSpellCheckLanguages(): Promise<string[]> | null {
|
2020-11-29 19:46:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-27 11:06:10 +00:00
|
|
|
protected getSSOCallbackUrl(fragmentAfterLogin = ""): URL {
|
2020-03-02 14:59:54 +00:00
|
|
|
const url = new URL(window.location.href);
|
2023-01-27 11:06:10 +00:00
|
|
|
url.hash = fragmentAfterLogin;
|
2020-03-02 14:59:54 +00:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin Single Sign On flows.
|
2020-03-02 15:05:56 +00:00
|
|
|
* @param {MatrixClient} mxClient the matrix client using which we should start the flow
|
|
|
|
* @param {"sso"|"cas"} loginType the type of SSO it is, CAS/SSO.
|
2020-05-13 05:24:04 +00:00
|
|
|
* @param {string} fragmentAfterLogin the hash to pass to the app during sso callback.
|
2023-01-27 11:06:10 +00:00
|
|
|
* @param {SSOAction} action the SSO flow to indicate to the IdP, optional.
|
2020-11-19 11:07:44 +00:00
|
|
|
* @param {string} idpId The ID of the Identity Provider being targeted, optional.
|
2020-03-02 14:59:54 +00:00
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public startSingleSignOn(
|
|
|
|
mxClient: MatrixClient,
|
|
|
|
loginType: "sso" | "cas",
|
2023-01-27 11:06:10 +00:00
|
|
|
fragmentAfterLogin?: string,
|
2022-06-06 06:31:20 +00:00
|
|
|
idpId?: string,
|
2023-01-27 11:06:10 +00:00
|
|
|
action?: SSOAction,
|
2022-06-06 06:31:20 +00:00
|
|
|
): void {
|
2020-06-25 20:59:46 +00:00
|
|
|
// persist hs url and is url for when the user is returned to the app with the login token
|
|
|
|
localStorage.setItem(SSO_HOMESERVER_URL_KEY, mxClient.getHomeserverUrl());
|
|
|
|
if (mxClient.getIdentityServerUrl()) {
|
2023-02-03 15:27:47 +00:00
|
|
|
localStorage.setItem(SSO_ID_SERVER_URL_KEY, mxClient.getIdentityServerUrl()!);
|
2020-06-25 20:59:46 +00:00
|
|
|
}
|
2021-02-01 16:25:50 +00:00
|
|
|
if (idpId) {
|
|
|
|
localStorage.setItem(SSO_IDP_ID_KEY, idpId);
|
|
|
|
}
|
2020-06-02 15:26:07 +00:00
|
|
|
const callbackUrl = this.getSSOCallbackUrl(fragmentAfterLogin);
|
2023-01-27 11:06:10 +00:00
|
|
|
window.location.href = mxClient.getSsoLoginUrl(callbackUrl.toString(), loginType, idpId, action); // redirect to SSO
|
2020-03-02 14:59:54 +00:00
|
|
|
}
|
2020-04-11 17:57:59 +00:00
|
|
|
|
2020-05-28 04:05:45 +00:00
|
|
|
/**
|
|
|
|
* Get a previously stored pickle key. The pickle key is used for
|
|
|
|
* encrypting libolm objects.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
|
|
|
* @param {string} userId the device ID that the pickle key is for.
|
|
|
|
* @returns {string|null} the previously stored pickle key, or null if no
|
|
|
|
* pickle key has been stored.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public async getPickleKey(userId: string, deviceId: string): Promise<string | null> {
|
2020-12-09 23:40:31 +00:00
|
|
|
if (!window.crypto || !window.crypto.subtle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await idbLoad("pickleKey", [userId, deviceId]);
|
2021-11-17 22:01:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
logger.error("idbLoad for pickleKey failed", e);
|
|
|
|
}
|
2020-12-09 23:40:31 +00:00
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!data.encrypted || !data.iv || !data.cryptoKey) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Badly formatted pickle key");
|
2020-12-09 23:40:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-08-15 08:43:15 +00:00
|
|
|
const additionalData = this.getPickleAdditionalData(userId, deviceId);
|
2020-12-09 23:40:31 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const key = await crypto.subtle.decrypt(
|
2021-06-29 12:11:58 +00:00
|
|
|
{ name: "AES-GCM", iv: data.iv, additionalData },
|
|
|
|
data.cryptoKey,
|
2020-12-09 23:40:31 +00:00
|
|
|
data.encrypted,
|
|
|
|
);
|
|
|
|
return encodeUnpaddedBase64(key);
|
|
|
|
} catch (e) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Error decrypting pickle key");
|
2020-12-09 23:40:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-05-28 04:05:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 08:43:15 +00:00
|
|
|
private getPickleAdditionalData(userId: string, deviceId: string): Uint8Array {
|
|
|
|
const additionalData = new Uint8Array(userId.length + deviceId.length + 1);
|
|
|
|
for (let i = 0; i < userId.length; i++) {
|
|
|
|
additionalData[i] = userId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
additionalData[userId.length] = 124; // "|"
|
|
|
|
for (let i = 0; i < deviceId.length; i++) {
|
|
|
|
additionalData[userId.length + 1 + i] = deviceId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return additionalData;
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:05:45 +00:00
|
|
|
/**
|
|
|
|
* Create and store a pickle key for encrypting libolm objects.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
2021-06-24 18:20:02 +00:00
|
|
|
* @param {string} deviceId the device ID that the pickle key is for.
|
2020-05-28 04:05:45 +00:00
|
|
|
* @returns {string|null} the pickle key, or null if the platform does not
|
|
|
|
* support storing pickle keys.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public async createPickleKey(userId: string, deviceId: string): Promise<string | null> {
|
2020-12-09 23:40:31 +00:00
|
|
|
if (!window.crypto || !window.crypto.subtle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const crypto = window.crypto;
|
|
|
|
const randomArray = new Uint8Array(32);
|
|
|
|
crypto.getRandomValues(randomArray);
|
|
|
|
const cryptoKey = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, false, [
|
2021-06-29 12:11:58 +00:00
|
|
|
"encrypt",
|
|
|
|
"decrypt",
|
2020-12-09 23:40:31 +00:00
|
|
|
]);
|
|
|
|
const iv = new Uint8Array(32);
|
|
|
|
crypto.getRandomValues(iv);
|
|
|
|
|
2023-08-15 08:43:15 +00:00
|
|
|
const additionalData = this.getPickleAdditionalData(userId, deviceId);
|
2021-06-29 12:11:58 +00:00
|
|
|
const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData }, cryptoKey, randomArray);
|
2020-12-09 23:40:31 +00:00
|
|
|
|
2020-12-11 02:52:18 +00:00
|
|
|
try {
|
2021-06-29 12:11:58 +00:00
|
|
|
await idbSave("pickleKey", [userId, deviceId], { encrypted, iv, cryptoKey });
|
2020-12-11 02:52:18 +00:00
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-09 23:40:31 +00:00
|
|
|
return encodeUnpaddedBase64(randomArray);
|
2020-05-28 04:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a previously stored pickle key from storage.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
|
|
|
* @param {string} userId the device ID that the pickle key is for.
|
|
|
|
*/
|
2022-06-06 06:31:20 +00:00
|
|
|
public async destroyPickleKey(userId: string, deviceId: string): Promise<void> {
|
2020-12-09 23:40:31 +00:00
|
|
|
try {
|
|
|
|
await idbDelete("pickleKey", [userId, deviceId]);
|
2021-11-17 22:01:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
logger.error("idbDelete failed in destroyPickleKey", e);
|
|
|
|
}
|
2020-05-28 04:05:45 +00:00
|
|
|
}
|
2023-03-15 15:56:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear app storage, called when logging out to perform data clean up.
|
|
|
|
*/
|
|
|
|
public async clearStorage(): Promise<void> {
|
|
|
|
window.sessionStorage.clear();
|
|
|
|
window.localStorage.clear();
|
|
|
|
}
|
2016-11-02 15:10:21 +00:00
|
|
|
}
|