Skip to content

Commit a2fd06b

Browse files
authored
Add API to withdraw verification requirement CryptoAPI.withdrawVerificationRequirement (#4646)
* API to withdraw verification `CryptoAPi.withdrawVerificationRequirement` * review: use set up function instead of beforeEach
1 parent 7d8cbd6 commit a2fd06b

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

+51
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,57 @@ describe("RustCrypto", () => {
15821582
});
15831583
});
15841584

1585+
describe("withdraw verification", () => {
1586+
function createTestSetup(): { olmMachine: Mocked<RustSdkCryptoJs.OlmMachine>; rustCrypto: RustCrypto } {
1587+
const olmMachine = {
1588+
getIdentity: jest.fn(),
1589+
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>;
1590+
const rustCrypto = new RustCrypto(
1591+
logger,
1592+
olmMachine,
1593+
{} as MatrixClient["http"],
1594+
TEST_USER,
1595+
TEST_DEVICE_ID,
1596+
{} as ServerSideSecretStorage,
1597+
{} as CryptoCallbacks,
1598+
);
1599+
return { olmMachine, rustCrypto };
1600+
}
1601+
1602+
it("throws an error for an unknown user", async () => {
1603+
const { rustCrypto } = createTestSetup();
1604+
await expect(rustCrypto.withdrawVerificationRequirement("@alice:example.com")).rejects.toThrow(
1605+
"Cannot withdraw verification of unknown user",
1606+
);
1607+
});
1608+
1609+
it("Calls withdraw for other identity", async () => {
1610+
const { olmMachine, rustCrypto } = createTestSetup();
1611+
const identity = {
1612+
withdrawVerification: jest.fn(),
1613+
} as unknown as Mocked<RustSdkCryptoJs.OtherUserIdentity>;
1614+
1615+
olmMachine.getIdentity.mockResolvedValue(identity);
1616+
1617+
await rustCrypto.withdrawVerificationRequirement("@bob:example.com");
1618+
1619+
expect(identity.withdrawVerification).toHaveBeenCalled();
1620+
});
1621+
1622+
it("Calls withdraw for own identity", async () => {
1623+
const { olmMachine, rustCrypto } = createTestSetup();
1624+
const ownIdentity = {
1625+
withdrawVerification: jest.fn(),
1626+
} as unknown as Mocked<RustSdkCryptoJs.OwnUserIdentity>;
1627+
1628+
olmMachine.getIdentity.mockResolvedValue(ownIdentity);
1629+
1630+
await rustCrypto.withdrawVerificationRequirement("@alice:example.com");
1631+
1632+
expect(ownIdentity.withdrawVerification).toHaveBeenCalled();
1633+
});
1634+
});
1635+
15851636
describe("key backup", () => {
15861637
it("is started when rust crypto is created", async () => {
15871638
// `RustCrypto.checkKeyBackupAndEnable` async call is made in background in the RustCrypto constructor.

src/crypto-api/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ export interface CryptoApi {
221221
*/
222222
pinCurrentUserIdentity(userId: string): Promise<void>;
223223

224+
/**
225+
* Remove the requirement for this identity to be verified, and pin it.
226+
*
227+
* This is useful if the user was previously verified but is not anymore
228+
* ({@link UserVerificationStatus.wasCrossSigningVerified}) and it is not possible to verify him again now.
229+
*
230+
*/
231+
withdrawVerificationRequirement(userId: string): Promise<void>;
232+
224233
/**
225234
* Get the verification status of a given device.
226235
*

src/crypto/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,13 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
15761576
throw new Error("not implemented");
15771577
}
15781578

1579+
/**
1580+
* Implementation of {@link Crypto.CryptoApi.withdrawVerificationRequirement}.
1581+
*/
1582+
public async withdrawVerificationRequirement(userId: string): Promise<void> {
1583+
throw new Error("not implemented");
1584+
}
1585+
15791586
/**
15801587
* Check whether a given device is trusted.
15811588
*

src/rust-crypto/rust-crypto.ts

+14
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,20 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
699699
await userIdentity.pinCurrentMasterKey();
700700
}
701701

702+
/**
703+
* Implementation of {@link CryptoApi#withdrawVerificationRequirement}.
704+
*/
705+
public async withdrawVerificationRequirement(userId: string): Promise<void> {
706+
const userIdentity: RustSdkCryptoJs.OtherUserIdentity | RustSdkCryptoJs.OwnUserIdentity | undefined =
707+
await this.getOlmMachineOrThrow().getIdentity(new RustSdkCryptoJs.UserId(userId));
708+
709+
if (userIdentity === undefined) {
710+
throw new Error("Cannot withdraw verification of unknown user");
711+
}
712+
713+
await userIdentity.withdrawVerification();
714+
}
715+
702716
/**
703717
* Implementation of {@link CryptoApi#isCrossSigningReady}
704718
*/

0 commit comments

Comments
 (0)