Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 10778cd

Browse files
committed
Use cached password
1 parent 9500b61 commit 10778cd

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/stores/SetupEncryptionStore.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { AccessCancelledError, accessSecretStorage } from "../SecurityManager";
3030
import Modal from "../Modal";
3131
import InteractiveAuthDialog from "../components/views/dialogs/InteractiveAuthDialog";
3232
import { _t } from "../languageHandler";
33+
import { SdkContextClass } from "../contexts/SDKContext";
3334

3435
export enum Phase {
3536
Loading = 0,
@@ -224,6 +225,21 @@ export class SetupEncryptionStore extends EventEmitter {
224225
const cli = MatrixClientPeg.get();
225226
await cli.bootstrapCrossSigning({
226227
authUploadDeviceSigningKeys: async (makeRequest): Promise<void> => {
228+
const cachedPassword = SdkContextClass.instance.accountPasswordStore.getPassword();
229+
230+
if (cachedPassword) {
231+
await makeRequest({
232+
type: "m.login.password",
233+
identifier: {
234+
type: "m.id.user",
235+
user: cli.getUserId(),
236+
},
237+
user: cli.getUserId(),
238+
password: cachedPassword,
239+
});
240+
return;
241+
}
242+
227243
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
228244
title: _t("Setting up keys"),
229245
matrixClient: cli,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { mocked, Mocked } from "jest-mock";
18+
import { IBootstrapCrossSigningOpts } from "matrix-js-sdk/src/crypto";
19+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
20+
21+
import { SdkContextClass } from "../../src/contexts/SDKContext";
22+
import { accessSecretStorage } from "../../src/SecurityManager";
23+
import { SetupEncryptionStore } from "../../src/stores/SetupEncryptionStore";
24+
import { stubClient } from "../test-utils";
25+
26+
jest.mock("../../src/SecurityManager", () => ({
27+
accessSecretStorage: jest.fn(),
28+
}));
29+
30+
describe("SetupEncryptionStore", () => {
31+
const cachedPassword = "p4assword";
32+
let client: Mocked<MatrixClient>;
33+
let setupEncryptionStore: SetupEncryptionStore;
34+
35+
beforeEach(() => {
36+
client = mocked(stubClient());
37+
setupEncryptionStore = new SetupEncryptionStore();
38+
SdkContextClass.instance.accountPasswordStore.setPassword(cachedPassword);
39+
});
40+
41+
afterEach(() => {
42+
SdkContextClass.instance.accountPasswordStore.clearPassword();
43+
});
44+
45+
it("resetConfirm should work with a cached account password", async () => {
46+
const makeRequest = jest.fn();
47+
client.hasSecretStorageKey.mockResolvedValue(true);
48+
client.bootstrapCrossSigning.mockImplementation(async (opts: IBootstrapCrossSigningOpts) => {
49+
await opts.authUploadDeviceSigningKeys(makeRequest);
50+
});
51+
mocked(accessSecretStorage).mockImplementation(async (func) => {
52+
await func();
53+
});
54+
55+
await setupEncryptionStore.resetConfirm();
56+
57+
expect(mocked(accessSecretStorage)).toHaveBeenCalledWith(expect.any(Function), true);
58+
expect(makeRequest).toHaveBeenCalledWith({
59+
identifier: {
60+
type: "m.id.user",
61+
user: "@userId:matrix.org",
62+
},
63+
password: cachedPassword,
64+
type: "m.login.password",
65+
user: "@userId:matrix.org",
66+
});
67+
});
68+
});

test/test-utils/test-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export function createTestClient(): MatrixClient {
102102
getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }),
103103
getSessionId: jest.fn().mockReturnValue("iaszphgvfku"),
104104
credentials: { userId: "@userId:matrix.org" },
105+
bootstrapCrossSigning: jest.fn(),
106+
hasSecretStorageKey: jest.fn(),
105107

106108
store: {
107109
getPendingEvents: jest.fn().mockResolvedValue([]),

0 commit comments

Comments
 (0)