From 45b277f6be71b6ca19d58ce5b963c7aebbf30a4d Mon Sep 17 00:00:00 2001 From: Andy Uhnak Date: Tue, 18 Apr 2023 14:04:55 +0100 Subject: [PATCH] Setup cross-signing even without auth session --- .../CrossSigning/MXCrossSigningV2.swift | 5 +- .../MXCrossSigningV2UnitTests.swift | 61 ++++++++++++++++++- .../CryptoMachine/MXCryptoProtocolStubs.swift | 2 + changelog.d/pr-1774.bugfix | 1 + 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 changelog.d/pr-1774.bugfix diff --git a/MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift b/MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift index 2c165967ce..d46a648376 100644 --- a/MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift +++ b/MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift @@ -217,8 +217,9 @@ class MXCrossSigningV2: NSObject, MXCrossSigning { let session = authSession.session, let userId = restClient.credentials?.userId else { - log.error("Missing parameters") - throw Error.missingAuthSession + // Try to setup cross-signing without authentication parameters in case if a grace period is enabled + log.warning("Setting up cross-signing without authentication parameters") + return [:] } return [ diff --git a/MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift b/MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift index 617eb44869..2de42cdb8c 100644 --- a/MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift +++ b/MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift @@ -21,19 +21,74 @@ import MatrixSDKCrypto class MXCrossSigningV2UnitTests: XCTestCase { + class RestClientStub: MXRestClientStub { + var stubbedAuthSession: MXAuthenticationSession? + + override var credentials: MXCredentials! { + let cred = MXCredentials() + cred.userId = "Alice" + cred.deviceId = "XYZ" + return cred + } + + override func authSession( + toUploadDeviceSigningKeys success: ((MXAuthenticationSession?) -> Void)!, + failure: ((Error?) -> Void)! + ) -> MXHTTPOperation! { + success(stubbedAuthSession) + return MXHTTPOperation() + } + } + var crypto: CryptoCrossSigningStub! var crossSigning: MXCrossSigningV2! - var restClient: MXRestClientStub! + var restClient: RestClientStub! override func setUp() { crypto = CryptoCrossSigningStub() - restClient = MXRestClientStub() + restClient = RestClientStub() crossSigning = MXCrossSigningV2( crossSigning: crypto, restClient: restClient ) } + // MARK: - Setup + + func test_setup_canSetupWithoutAuthSession() { + let exp = expectation(description: "exp") + crossSigning.setup(withPassword: "pass") { + XCTAssertEqual(self.crypto.spyAuthParams as? [String: String], [:]) + exp.fulfill() + } failure: { + XCTFail("Failed setting up cross signing with error - \($0)") + exp.fulfill() + } + waitForExpectations(timeout: 1) + } + + func test_setup_canSetupWitAuthSession() { + let exp = expectation(description: "exp") + restClient.stubbedAuthSession = MXAuthenticationSession() + restClient.stubbedAuthSession?.session = "123" + + crossSigning.setup(withPassword: "pass") { + XCTAssertEqual(self.crypto.spyAuthParams as? [String: String], [ + "session": "123", + "user": "Alice", + "password": "pass", + "type": kMXLoginFlowTypePassword + ]) + exp.fulfill() + } failure: { + XCTFail("Failed setting up cross signing with error - \($0)") + exp.fulfill() + } + waitForExpectations(timeout: 1) + } + + // MARK: - State + func test_state_notBootstrapped() { XCTAssertEqual(crossSigning.state, .notBootstrapped) } @@ -104,6 +159,8 @@ class MXCrossSigningV2UnitTests: XCTestCase { } } + // MARK: - Devices + func test_crossSignDevice_verifiesUntrustedDevice() async throws { let userId = "Alice" let deviceId = "ABCD" diff --git a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift index 79069835ae..570e35d392 100644 --- a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift +++ b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift @@ -89,7 +89,9 @@ class CryptoCrossSigningStub: CryptoIdentityStub, MXCryptoCrossSigning { return stubbedStatus } + var spyAuthParams: [AnyHashable: Any]? func bootstrapCrossSigning(authParams: [AnyHashable : Any]) async throws { + self.spyAuthParams = authParams } func exportCrossSigningKeys() -> CrossSigningKeyExport? { diff --git a/changelog.d/pr-1774.bugfix b/changelog.d/pr-1774.bugfix new file mode 100644 index 0000000000..cf3d65fe97 --- /dev/null +++ b/changelog.d/pr-1774.bugfix @@ -0,0 +1 @@ +Cross-signing: Setup cross-signing with empty auth session