Skip to content

Commit

Permalink
Merge pull request matrix-org#1774 from matrix-org/andy/cross_signing
Browse files Browse the repository at this point in the history
Setup cross-signing even without auth session
  • Loading branch information
Anderas committed Apr 18, 2023
2 parents 4f724c9 + 45b277f commit 68bbc3e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
5 changes: 3 additions & 2 deletions MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
61 changes: 59 additions & 2 deletions MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -104,6 +159,8 @@ class MXCrossSigningV2UnitTests: XCTestCase {
}
}

// MARK: - Devices

func test_crossSignDevice_verifiesUntrustedDevice() async throws {
let userId = "Alice"
let deviceId = "ABCD"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1774.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cross-signing: Setup cross-signing with empty auth session

0 comments on commit 68bbc3e

Please sign in to comment.