|
| 1 | +/* |
| 2 | +Copyright 2024 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import EventEmitter from "events"; |
| 9 | +import { MatrixClient } from "matrix-js-sdk/src/matrix"; |
| 10 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 11 | +import { useEffect, useState } from "react"; |
| 12 | + |
| 13 | +import { createCrossSigning } from "../CreateCrossSigning"; |
| 14 | +import { SdkContextClass } from "../contexts/SDKContext"; |
| 15 | + |
| 16 | +type Status = "in_progress" | "complete" | "error" | undefined; |
| 17 | + |
| 18 | +export const useInitialCryptoSetupStatus = (store: InitialCryptoSetupStore): Status => { |
| 19 | + const [status, setStatus] = useState<Status>(store.getStatus()); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + const update = (): void => { |
| 23 | + setStatus(store.getStatus()); |
| 24 | + }; |
| 25 | + |
| 26 | + store.on("update", update); |
| 27 | + |
| 28 | + return () => { |
| 29 | + store.off("update", update); |
| 30 | + }; |
| 31 | + }, [store]); |
| 32 | + |
| 33 | + return status; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Logic for setting up crypto state that's done immediately after |
| 38 | + * a user registers. Should be transparent to the user, not requiring |
| 39 | + * interaction in most cases. |
| 40 | + * As distinct from SetupEncryptionStore which is for setting up |
| 41 | + * 4S or verifying the device, will always require interaction |
| 42 | + * from the user in some form. |
| 43 | + */ |
| 44 | +export class InitialCryptoSetupStore extends EventEmitter { |
| 45 | + private status: Status = undefined; |
| 46 | + |
| 47 | + private client?: MatrixClient; |
| 48 | + private isTokenLogin?: boolean; |
| 49 | + private stores?: SdkContextClass; |
| 50 | + private onFinished?: (success: boolean) => void; |
| 51 | + |
| 52 | + public static sharedInstance(): InitialCryptoSetupStore { |
| 53 | + if (!window.mxInitialCryptoStore) window.mxInitialCryptoStore = new InitialCryptoSetupStore(); |
| 54 | + return window.mxInitialCryptoStore; |
| 55 | + } |
| 56 | + |
| 57 | + public getStatus(): Status { |
| 58 | + return this.status; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Start the initial crypto setup process. |
| 63 | + * |
| 64 | + * @param {MatrixClient} client The client to use for the setup |
| 65 | + * @param {boolean} isTokenLogin True if the user logged in via a token login, otherwise false |
| 66 | + * @param {SdkContextClass} stores The stores to use for the setup |
| 67 | + */ |
| 68 | + public startInitialCryptoSetup( |
| 69 | + client: MatrixClient, |
| 70 | + isTokenLogin: boolean, |
| 71 | + stores: SdkContextClass, |
| 72 | + onFinished: (success: boolean) => void, |
| 73 | + ): void { |
| 74 | + this.client = client; |
| 75 | + this.isTokenLogin = isTokenLogin; |
| 76 | + this.stores = stores; |
| 77 | + this.onFinished = onFinished; |
| 78 | + |
| 79 | + // We just start this process: it's progress is tracked by the events rather |
| 80 | + // than returning a promise, so we don't bother. |
| 81 | + this.doSetup().catch(() => logger.error("Initial crypto setup failed")); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Retry the initial crypto setup process. |
| 86 | + * |
| 87 | + * If no crypto setup is currently in process, this will return false. |
| 88 | + * |
| 89 | + * @returns {boolean} True if a retry was initiated, otherwise false |
| 90 | + */ |
| 91 | + public retry(): boolean { |
| 92 | + if (this.client === undefined || this.isTokenLogin === undefined || this.stores == undefined) return false; |
| 93 | + |
| 94 | + this.doSetup().catch(() => logger.error("Initial crypto setup failed")); |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + private reset(): void { |
| 100 | + this.client = undefined; |
| 101 | + this.isTokenLogin = undefined; |
| 102 | + this.stores = undefined; |
| 103 | + } |
| 104 | + |
| 105 | + private async doSetup(): Promise<void> { |
| 106 | + if (this.client === undefined || this.isTokenLogin === undefined || this.stores == undefined) { |
| 107 | + throw new Error("No setup is in progress"); |
| 108 | + } |
| 109 | + |
| 110 | + const cryptoApi = this.client.getCrypto(); |
| 111 | + if (!cryptoApi) throw new Error("No crypto module found!"); |
| 112 | + |
| 113 | + this.status = "in_progress"; |
| 114 | + this.emit("update"); |
| 115 | + |
| 116 | + try { |
| 117 | + await createCrossSigning(this.client, this.isTokenLogin, this.stores.accountPasswordStore.getPassword()); |
| 118 | + |
| 119 | + this.reset(); |
| 120 | + |
| 121 | + this.status = "complete"; |
| 122 | + this.emit("update"); |
| 123 | + this.onFinished?.(true); |
| 124 | + } catch (e) { |
| 125 | + if (this.isTokenLogin) { |
| 126 | + // ignore any failures, we are relying on grace period here |
| 127 | + this.reset(); |
| 128 | + |
| 129 | + this.status = "complete"; |
| 130 | + this.emit("update"); |
| 131 | + this.onFinished?.(true); |
| 132 | + |
| 133 | + return; |
| 134 | + } |
| 135 | + logger.error("Error bootstrapping cross-signing", e); |
| 136 | + this.status = "error"; |
| 137 | + this.emit("update"); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments