Add resetOthers parameter to session creator method

This commit is contained in:
ismailgulek
2022-06-08 17:22:47 +03:00
parent 7f5a329080
commit 9103ccc5d7
6 changed files with 31 additions and 19 deletions

View File

@@ -54,7 +54,7 @@ class LoginWizard {
/// - initialDeviceName: The initial device name.
/// - deviceID: The device ID, optional. If not provided or nil, the server will generate one.
/// - Returns: An `MXSession` if the login is successful.
func login(login: String, password: String, initialDeviceName: String, deviceID: String? = nil) async throws -> MXSession {
func login(login: String, password: String, initialDeviceName: String, deviceID: String? = nil, resetOthers: Bool = false) async throws -> MXSession {
let parameters: LoginPasswordParameters
if MXTools.isEmailAddress(login) {
@@ -70,16 +70,16 @@ class LoginWizard {
}
let credentials = try await client.login(parameters: parameters)
return sessionCreator.createSession(credentials: credentials, client: client)
return sessionCreator.createSession(credentials: credentials, client: client, resetOthers: resetOthers)
}
/// Exchange a login token to an access token.
/// - Parameter loginToken: A login token, obtained when login has happened in a WebView, using SSO.
/// - Returns: An `MXSession` if the login is successful.
func login(with token: String) async throws -> MXSession {
func login(with token: String, resetOthers: Bool = false) async throws -> MXSession {
let parameters = LoginTokenParameters(token: token)
let credentials = try await client.login(parameters: parameters)
return sessionCreator.createSession(credentials: credentials, client: client)
return sessionCreator.createSession(credentials: credentials, client: client, resetOthers: resetOthers)
}
// /// Login to the homeserver by sending a custom JsonDict.

View File

@@ -255,7 +255,7 @@ class RegistrationWizard {
do {
let response = try await client.register(parameters: parameters)
let credentials = MXCredentials(loginResponse: response, andDefaultCredentials: client.credentials)
return .success(sessionCreator.createSession(credentials: credentials, client: client))
return .success(sessionCreator.createSession(credentials: credentials, client: client, resetOthers: false))
} catch {
let nsError = error as NSError

View File

@@ -21,8 +21,9 @@ protocol SessionCreatorProtocol {
/// - Parameters:
/// - credentials: The `MXCredentials` for the account.
/// - client: The client that completed the authentication.
/// - resetOthers: Flag to reset other accounts.
/// - Returns: A new `MXSession` for the account.
func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession
func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession
}
/// A struct that provides common functionality to create a new session.
@@ -34,19 +35,29 @@ struct SessionCreator: SessionCreatorProtocol {
self.accountManager = accountManager
}
func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession {
// Report the new account in account manager
func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession {
// Use identity server provided in the client
if credentials.identityServer == nil {
credentials.identityServer = client.identityServer
}
let account = MXKAccount(credentials: credentials)
if let identityServer = credentials.identityServer {
account.identityServerURL = identityServer
if let account = accountManager.account(forUserId: credentials.userId) {
accountManager.hydrateAccount(account, with: credentials)
return account.mxSession
} else {
if resetOthers {
accountManager.logout(completion: nil)
}
let account = MXKAccount(credentials: credentials)
// set identity server of the new account
if let identityServer = credentials.identityServer {
account.identityServerURL = identityServer
}
accountManager.addAccount(account, andOpenSession: true)
return account.mxSession
}
accountManager.addAccount(account, andOpenSession: true)
return account.mxSession
}
}

View File

@@ -178,7 +178,8 @@ final class AuthenticationSoftLogoutCoordinator: Coordinator, Presentable {
let session = try await loginWizard.login(login: userId,
password: password,
initialDeviceName: UIDevice.current.initialDisplayName,
deviceID: deviceId)
deviceID: deviceId,
resetOthers: true)
guard !Task.isCancelled else { return }
callback?(.success(session: session, password: password))

View File

@@ -20,7 +20,7 @@ import Foundation
struct MockSessionCreator: SessionCreatorProtocol {
/// Returns a basic session created from the supplied credentials. This prevents the app from setting up the account during tests.
func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession {
func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession {
let client = MXRestClient(credentials: credentials,
unauthenticatedHandler: { _,_,_,_ in }) // The handler is expected if credentials are set.
return MXSession(matrixRestClient: client)

View File

@@ -29,7 +29,7 @@ class SessionCreatorTests: XCTestCase {
accessToken: "mock_access_token")
let client = MXRestClient(credentials: credentials)
client.identityServer = mockIS
let session = sessionCreator.createSession(credentials: credentials, client: client)
let session = sessionCreator.createSession(credentials: credentials, client: client, resetOthers: false)
XCTAssertEqual(credentials.identityServer, mockIS)
XCTAssertEqual(session.credentials.identityServer, mockIS)