diff --git a/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.h b/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.h index cfbec2a9ab..7881832e01 100644 --- a/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.h +++ b/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.h @@ -48,6 +48,9 @@ extern NSString *const MXKAccountManagerDataType; */ @interface MXKAccountManager : NSObject +/// Flag indicating that saving accounts enabled. Defaults to `YES`. +@property (nonatomic, assign, getter=isSavingAccountsEnabled) BOOL savingAccountsEnabled; + /** The class of store used to open matrix session for the accounts. This class must be conformed to MXStore protocol. By default this class is MXFileStore. diff --git a/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.m b/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.m index 352e0ad6ee..6a69ee6a7b 100644 --- a/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.m +++ b/Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.m @@ -72,6 +72,7 @@ - (instancetype)init { _storeClass = [MXFileStore class]; _dehydrationService = [MXDehydrationService new]; + _savingAccountsEnabled = YES; // Migrate old account file to new format [self migrateAccounts]; @@ -108,10 +109,15 @@ - (void)prepareSessionForActiveAccounts #pragma clang diagnostic ignored "-Wdeprecated" - (void)saveAccounts { - NSDate *startDate = [NSDate date]; - MXLogDebug(@"[MXKAccountManager] saveAccounts..."); - + + if (!self.isSavingAccountsEnabled) + { + MXLogDebug(@"[MXKAccountManager] saveAccounts: saving disabled."); + return; + } + NSDate *startDate = [NSDate date]; + NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationRestClient.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationRestClient.swift index 10ed719f25..058ac3d184 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationRestClient.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationRestClient.swift @@ -19,7 +19,7 @@ import Foundation protocol AuthenticationRestClient: AnyObject { // MARK: Configuration var homeserver: String! { get } - var identityServer: String! { get } + var identityServer: String! { get set } var credentials: MXCredentials! { get } var acceptableContentTypes: Set! { get set } diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationService.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationService.swift index afbe3feee5..e05c62cc6f 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationService.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/AuthenticationService.swift @@ -191,14 +191,24 @@ class AuthenticationService: NSObject { MXLog.error("[AuthenticationService] Unable to create a URL from the supplied homeserver address when calling loginFlow.") throw AuthenticationError.invalidHomeserver } + + var identityServerURL: URL? - if let wellKnown = try? await wellKnown(for: homeserverURL), - let baseURL = URL(string: wellKnown.homeServer.baseUrl) { - homeserverURL = baseURL + if let wellKnown = try? await wellKnown(for: homeserverURL) { + if let baseURL = URL(string: wellKnown.homeServer.baseUrl) { + homeserverURL = baseURL + } + if let identityServer = wellKnown.identityServer, + let baseURL = URL(string: identityServer.baseUrl) { + identityServerURL = baseURL + } } #warning("Add an unrecognized certificate handler.") let client = clientType.init(homeServer: homeserverURL, unrecognizedCertificateHandler: nil) + if let identityServerURL = identityServerURL { + client.identityServer = identityServerURL.absoluteString + } let loginFlow = try await getLoginFlowResult(client: client) diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift index 660ebe26c5..77add2d69d 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift @@ -27,10 +27,16 @@ protocol SessionCreatorProtocol { /// A struct that provides common functionality to create a new session. struct SessionCreator: SessionCreatorProtocol { + + private let accountManager: MXKAccountManager + + init(withAccountManager accountManager: MXKAccountManager = .shared()) { + self.accountManager = accountManager + } + func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession { // Report the new account in account manager if credentials.identityServer == nil { - #warning("Check that the client is actually updated with this info?") credentials.identityServer = client.identityServer } @@ -40,7 +46,7 @@ struct SessionCreator: SessionCreatorProtocol { account.identityServerURL = identityServer } - MXKAccountManager.shared().addAccount(account, andOpenSession: true) + accountManager.addAccount(account, andOpenSession: true) return account.mxSession } } diff --git a/RiotTests/SessionCreatorTests.swift b/RiotTests/SessionCreatorTests.swift new file mode 100644 index 0000000000..b2a118f65c --- /dev/null +++ b/RiotTests/SessionCreatorTests.swift @@ -0,0 +1,49 @@ +// +// Copyright 2022 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import XCTest +@testable import Riot + +class SessionCreatorTests: XCTestCase { + + func testIdentityServer() throws { + let sessionCreator = SessionCreator(withAccountManager: .mock) + + let mockIS = "mock_identity_server" + + let credentials = MXCredentials(homeServer: "mock_home_server", + userId: "mock_user_id", + accessToken: "mock_access_token") + let client = MXRestClient(credentials: credentials) + client.identityServer = mockIS + let session = sessionCreator.createSession(credentials: credentials, client: client) + + XCTAssertEqual(credentials.identityServer, mockIS) + XCTAssertEqual(session.credentials.identityServer, mockIS) + XCTAssertEqual(session.identityService?.identityServer, mockIS) + } + +} + +private extension MXKAccountManager { + + static var mock: MXKAccountManager { + let result = MXKAccountManager.shared() + result!.isSavingAccountsEnabled = false + return result! + } + +} diff --git a/changelog.d/6177.change b/changelog.d/6177.change new file mode 100644 index 0000000000..56dd5bd2a8 --- /dev/null +++ b/changelog.d/6177.change @@ -0,0 +1 @@ +AuthenticationService: Use identity server from well-known if provided when creating the client.