-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Factor out crypto setup process into a store #28675
Changes from all commits
e7e7331
2ce127c
1c8d7d8
ce19437
c07883e
2bf3723
5b5a7cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import EventEmitter from "events"; | ||
import { MatrixClient } from "matrix-js-sdk/src/matrix"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { createCrossSigning } from "../CreateCrossSigning"; | ||
import { SdkContextClass } from "../contexts/SDKContext"; | ||
|
||
type Status = "in_progress" | "complete" | "error" | undefined; | ||
|
||
export const useInitialCryptoSetupStatus = (store: InitialCryptoSetupStore): Status => { | ||
const [status, setStatus] = useState<Status>(store.getStatus()); | ||
|
||
useEffect(() => { | ||
const update = (): void => { | ||
setStatus(store.getStatus()); | ||
}; | ||
|
||
store.on("update", update); | ||
|
||
return () => { | ||
store.off("update", update); | ||
}; | ||
}, [store]); | ||
|
||
return status; | ||
}; | ||
|
||
/** | ||
* Logic for setting up crypto state that's done immediately after | ||
* a user registers. Should be transparent to the user, not requiring | ||
* interaction in most cases. | ||
* As distinct from SetupEncryptionStore which is for setting up | ||
* 4S or verifying the device, will always require interaction | ||
* from the user in some form. | ||
*/ | ||
export class InitialCryptoSetupStore extends EventEmitter { | ||
private status: Status = undefined; | ||
|
||
private client?: MatrixClient; | ||
private isTokenLogin?: boolean; | ||
private stores?: SdkContextClass; | ||
private onFinished?: (success: boolean) => void; | ||
|
||
public static sharedInstance(): InitialCryptoSetupStore { | ||
if (!window.mxInitialCryptoStore) window.mxInitialCryptoStore = new InitialCryptoSetupStore(); | ||
return window.mxInitialCryptoStore; | ||
} | ||
|
||
public getStatus(): Status { | ||
return this.status; | ||
} | ||
|
||
/** | ||
* Start the initial crypto setup process. | ||
* | ||
* @param {MatrixClient} client The client to use for the setup | ||
* @param {boolean} isTokenLogin True if the user logged in via a token login, otherwise false | ||
* @param {SdkContextClass} stores The stores to use for the setup | ||
*/ | ||
public startInitialCryptoSetup( | ||
client: MatrixClient, | ||
isTokenLogin: boolean, | ||
stores: SdkContextClass, | ||
onFinished: (success: boolean) => void, | ||
): void { | ||
this.client = client; | ||
this.isTokenLogin = isTokenLogin; | ||
this.stores = stores; | ||
this.onFinished = onFinished; | ||
|
||
// We just start this process: it's progress is tracked by the events rather | ||
// than returning a promise, so we don't bother. | ||
this.doSetup().catch(() => logger.error("Initial crypto setup failed")); | ||
} | ||
|
||
/** | ||
* Retry the initial crypto setup process. | ||
* | ||
* If no crypto setup is currently in process, this will return false. | ||
* | ||
* @returns {boolean} True if a retry was initiated, otherwise false | ||
*/ | ||
public retry(): boolean { | ||
if (this.client === undefined || this.isTokenLogin === undefined || this.stores == undefined) return false; | ||
|
||
this.doSetup().catch(() => logger.error("Initial crypto setup failed")); | ||
|
||
return true; | ||
} | ||
|
||
private reset(): void { | ||
this.client = undefined; | ||
this.isTokenLogin = undefined; | ||
this.stores = undefined; | ||
} | ||
|
||
private async doSetup(): Promise<void> { | ||
if (this.client === undefined || this.isTokenLogin === undefined || this.stores == undefined) { | ||
throw new Error("No setup is in progress"); | ||
} | ||
|
||
const cryptoApi = this.client.getCrypto(); | ||
if (!cryptoApi) throw new Error("No crypto module found!"); | ||
|
||
this.status = "in_progress"; | ||
this.emit("update"); | ||
|
||
try { | ||
await createCrossSigning(this.client, this.isTokenLogin, this.stores.accountPasswordStore.getPassword()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The password thing look like something that should be removed. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was hoping we could remove this soon. I wouldn't do that in a refactor though. |
||
|
||
this.reset(); | ||
|
||
this.status = "complete"; | ||
this.emit("update"); | ||
this.onFinished?.(true); | ||
} catch (e) { | ||
if (this.isTokenLogin) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the status is not "error" if there is a login token? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish I knew: this comment is all there was so I moved it over. Possibly because if the user's using token login and the grace period didn't work, nothing else will so it's better to just carry on and bug the user later to set up crypto rather than worry them? Maybe the real answer is that we should drop support for HSes without the key upload grace period. |
||
// ignore any failures, we are relying on grace period here | ||
this.reset(); | ||
|
||
this.status = "complete"; | ||
this.emit("update"); | ||
this.onFinished?.(true); | ||
|
||
return; | ||
} | ||
logger.error("Error bootstrapping cross-signing", e); | ||
this.status = "error"; | ||
this.emit("update"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we are not waiting for
doSetup
to finish?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just seemed unnecessary and would conflate the interface I think. Added a comment.