Allow registration on SSO only servers.

Stop using the homeserver from user defaults.
This commit is contained in:
Doug
2022-04-28 13:06:36 +01:00
committed by Doug
parent b4d7c7f12b
commit 67a021111c
19 changed files with 169 additions and 85 deletions
@@ -48,21 +48,14 @@ class AuthenticationService: NSObject {
// MARK: - Setup
override init() {
if let homeserverURL = URL(string: RiotSettings.shared.homeserverUrlString) {
// Use the same homeserver that was last used.
state = AuthenticationState(flow: .login, homeserverAddress: RiotSettings.shared.homeserverUrlString)
client = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
} else if let homeserverURL = URL(string: BuildSettings.serverConfigDefaultHomeserverUrlString) {
// Fall back to the default homeserver if the stored one is invalid.
state = AuthenticationState(flow: .login, homeserverAddress: BuildSettings.serverConfigDefaultHomeserverUrlString)
client = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
} else {
guard let homeserverURL = URL(string: BuildSettings.serverConfigDefaultHomeserverUrlString) else {
MXLog.failure("[AuthenticationService]: Failed to create URL from default homeserver URL string.")
fatalError("Invalid default homeserver URL string.")
}
state = AuthenticationState(flow: .login, homeserverAddress: BuildSettings.serverConfigDefaultHomeserverUrlString)
client = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
super.init()
}
@@ -97,10 +90,6 @@ class AuthenticationService: NSObject {
let loginFlows = try await loginFlow(for: homeserverAddress)
// Valid Homeserver, add it to the history.
// Note: we add what the user has input, as the data can contain a different value.
RiotSettings.shared.homeserverUrlString = homeserverAddress
state.homeserver = .init(address: loginFlows.homeserverAddress,
addressFromUser: homeserverAddress,
preferredLoginMode: loginFlows.loginMode,
@@ -110,9 +99,16 @@ class AuthenticationService: NSObject {
self.loginWizard = loginWizard
if flow == .registration {
let registrationWizard = RegistrationWizard(client: client)
state.homeserver.registrationFlow = try await registrationWizard.registrationFlow()
self.registrationWizard = registrationWizard
do {
let registrationWizard = RegistrationWizard(client: client)
state.homeserver.registrationFlow = try await registrationWizard.registrationFlow()
self.registrationWizard = registrationWizard
} catch {
guard state.homeserver.preferredLoginMode.hasSSO, error as? RegistrationError == .registrationDisabled else {
throw error
}
// Continue without throwing when registration is disabled but SSO is available.
}
}
state.flow = flow
@@ -20,7 +20,7 @@ import Foundation
/// A protocol with a default implementation that allows a coordinator to execute and handle registration flow steps.
protocol RegistrationFlowHandling {
var authenticationService: AuthenticationService { get }
var registrationWizard: RegistrationWizard { get }
var registrationWizard: RegistrationWizard? { get }
@MainActor var completion: ((AuthenticationRegistrationCoordinatorResult) -> Void)? { get }
/// Executes a registration step using the `RegistrationWizard` to complete any additional steps automatically.
@@ -31,6 +31,11 @@ protocol RegistrationFlowHandling {
@MainActor extension RegistrationFlowHandling {
func executeRegistrationStep(step: @escaping (RegistrationWizard) async throws -> RegistrationResult) -> Task<Void, Error> {
return Task {
guard let registrationWizard = registrationWizard else {
MXLog.failure("[RegistrationFlowHandling] executeRegistrationStep: The registration wizard is nil.")
return
}
do {
let result = try await step(registrationWizard)
@@ -74,7 +74,15 @@ class RegistrationWizard {
/// See `AuthenticationService.getFallbackUrl`
func registrationFlow() async throws -> RegistrationResult {
let parameters = RegistrationParameters()
return try await performRegistrationRequest(parameters: parameters)
do {
let result = try await performRegistrationRequest(parameters: parameters)
return result
} catch {
// Map M_FORBIDDEN into a registration error.
guard let mxError = MXError(nsError: error), mxError.errcode == kMXErrCodeStringForbidden else { throw error }
throw RegistrationError.registrationDisabled
}
}
/// Can be call to check is the desired username is available for registration on the current homeserver.