#40: Use the Rust AuthenticationService.

* Update SDK package to 1.0.12-alpha.
* Use an app group for storage and stop stripping the http from the homeserver when configuring the service.
* Rename access token to restore token.
* Remove matrix.org server description inline with latest FTUE changes.
This commit is contained in:
Doug
2022-07-27 10:57:16 +01:00
committed by GitHub
parent 03d6097c72
commit 1878a16496
29 changed files with 284 additions and 204 deletions
@@ -9,42 +9,61 @@
import Foundation
import MatrixRustSDK
class AuthenticationService: AuthenticationServiceProtocol {
class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
// MARK: - Properties
// MARK: Private
private(set) var homeserver = LoginHomeserver(address: BuildSettings.defaultHomeserverURLString)
private let authenticationService: AuthenticationService
private let userSessionStore: UserSessionStoreProtocol
// MARK: Public
private(set) var homeserver = LoginHomeserver(address: BuildSettings.defaultHomeserverAddress, loginMode: .unknown)
// MARK: - Setup
init(userSessionStore: UserSessionStoreProtocol) {
self.userSessionStore = userSessionStore
authenticationService = AuthenticationService(basePath: userSessionStore.baseDirectoryPath)
}
// MARK: - Public
func startLogin(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError> {
homeserver = LoginHomeserver(address: homeserverAddress)
return .success(())
func configure(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError> {
let task = Task.detached { () -> LoginHomeserver in
var homeserver = LoginHomeserver(address: homeserverAddress, loginMode: .unknown)
try self.authenticationService.configureHomeserver(serverName: homeserverAddress)
guard let details = self.authenticationService.homeserverDetails() else { return homeserver }
if let issuer = details.authenticationIssuer(), let issuerURL = URL(string: issuer) {
homeserver.loginMode = .oidc(issuerURL)
} else if details.supportsPasswordLogin() {
homeserver.loginMode = .password
} else {
homeserver.loginMode = .unsupported
}
return homeserver
}
switch await task.result {
case .success(let homeserver):
self.homeserver = homeserver
return .success(())
case .failure(let error):
MXLog.error("Failed configuring a server: \(error)")
return .failure(.invalidHomeserverAddress)
}
}
func login(username: String, password: String) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
Benchmark.startTrackingForIdentifier("Login", message: "Started new login")
// Workaround whilst the SDK requires a full MXID.
let username = username.isMatrixUserID ? username : "@\(username):\(homeserver.address)"
let basePath = userSessionStore.baseDirectoryPath(for: username)
let builder = ClientBuilder()
.basePath(path: basePath)
.username(username: username)
let loginTask: Task<Client, Error> = Task.detached {
let client = try builder.build()
try client.login(username: username, password: password)
return client
try self.authenticationService.login(username: username, password: password)
}
switch await loginTask.result {
@@ -55,7 +74,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
Benchmark.endTrackingForIdentifier("Login", message: "Login failed")
MXLog.error("Failed logging in with error: \(error)")
guard let error = error as? ClientError else { return .failure(.failedLoggingIn) }
guard let error = error as? AuthenticationError else { return .failure(.failedLoggingIn) }
switch error.code {
case .forbidden:
@@ -1,5 +1,5 @@
//
// AuthenticationServiceProtocol.swift
// AuthenticationServiceProxyProtocol.swift
// ElementX
//
// Created by Doug on 29/06/2022.
@@ -11,16 +11,17 @@ import Foundation
enum AuthenticationServiceError: Error {
case invalidServer
case invalidCredentials
case invalidHomeserverAddress
case accountDeactivated
case failedLoggingIn
}
@MainActor
protocol AuthenticationServiceProtocol {
protocol AuthenticationServiceProxyProtocol {
var homeserver: LoginHomeserver { get }
/// Sets up the service for login on the specified homeserver address.
func startLogin(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError>
func configure(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError>
/// Performs a password login using the current homeserver.
func login(username: String, password: String) async -> Result<UserSessionProtocol, AuthenticationServiceError>
}
@@ -8,11 +8,11 @@
import Foundation
class MockAuthenticationService: AuthenticationServiceProtocol {
class MockAuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
let validCredentials = (username: "alice", password: "12345678")
private(set) var homeserver: LoginHomeserver = .mockMatrixDotOrg
func startLogin(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError> {
func configure(for homeserverAddress: String) async -> Result<Void, AuthenticationServiceError> {
// Map the address to the mock homeservers
if LoginHomeserver.mockMatrixDotOrg.address.contains(homeserverAddress) {
homeserver = .mockMatrixDotOrg