mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 20:09:04 +00:00
Simplify authentication logic
matrix.org now supports MAS so we don't need the web registration helper anymore.
This commit is contained in:
@@ -48,27 +48,21 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
|
||||
let client = try await makeClientBuilder().build(homeserverAddress: homeserverAddress)
|
||||
let loginDetails = await client.homeserverLoginDetails()
|
||||
let elementWellKnown = await client.getElementWellKnown()
|
||||
|
||||
MXLog.info("Sliding sync: \(client.slidingSyncVersion())")
|
||||
|
||||
homeserver.loginMode = if loginDetails.supportsOidcLogin() {
|
||||
.oidc
|
||||
.oidc(supportsCreatePrompt: loginDetails.supportedOidcPrompts().contains(.create))
|
||||
} else if loginDetails.supportsPasswordLogin() {
|
||||
.password
|
||||
} else {
|
||||
.unsupported
|
||||
}
|
||||
|
||||
homeserver.registrationHelperURL = switch elementWellKnown {
|
||||
case .success(let wellKnown): wellKnown.registrationHelperUrl.flatMap(URL.init)
|
||||
case .failure: nil
|
||||
}
|
||||
|
||||
if flow == .login, homeserver.loginMode == .unsupported {
|
||||
return .failure(.loginNotSupported)
|
||||
}
|
||||
if flow == .register, !homeserver.supportsRegistration {
|
||||
if flow == .register, !homeserver.loginMode.supportsOIDCFlow {
|
||||
return .failure(.registrationNotSupported)
|
||||
}
|
||||
|
||||
@@ -91,6 +85,8 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
func urlForOIDCLogin() async -> Result<OIDCAuthorizationDataProxy, AuthenticationServiceError> {
|
||||
guard let client else { return .failure(.oidcError(.urlFailure)) }
|
||||
do {
|
||||
// The create prompt is broken: https://github.com/element-hq/matrix-authentication-service/issues/3429
|
||||
// let prompt: OidcPrompt = flow == .register ? .create : .consent
|
||||
let oidcData = try await client.urlForOidc(oidcConfiguration: appSettings.oidcConfiguration.rustValue,
|
||||
prompt: .consent)
|
||||
return .success(OIDCAuthorizationDataProxy(underlyingData: oidcData))
|
||||
@@ -148,25 +144,6 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
func completeWebRegistration(using credentials: WebRegistrationCredentials) async -> Result<any UserSessionProtocol, AuthenticationServiceError> {
|
||||
guard let client else { return .failure(.failedLoggingIn) }
|
||||
let session = Session(accessToken: credentials.accessToken,
|
||||
refreshToken: nil,
|
||||
userId: credentials.userID,
|
||||
deviceId: credentials.deviceID,
|
||||
homeserverUrl: client.homeserver(),
|
||||
oidcData: nil,
|
||||
slidingSyncVersion: client.slidingSyncVersion())
|
||||
|
||||
do {
|
||||
try await client.restoreSession(session: session)
|
||||
return await userSession(for: client)
|
||||
} catch {
|
||||
MXLog.error("Failed restoring the client using the provided credentials.")
|
||||
return .failure(.failedUsingWebCredentials)
|
||||
}
|
||||
}
|
||||
|
||||
func reset() {
|
||||
homeserverSubject.send(LoginHomeserver(address: appSettings.defaultHomeserverAddress, loginMode: .unknown))
|
||||
flow = .login
|
||||
|
||||
@@ -48,8 +48,6 @@ protocol AuthenticationServiceProtocol {
|
||||
func loginWithOIDCCallback(_ callbackURL: URL) async -> Result<UserSessionProtocol, AuthenticationServiceError>
|
||||
/// Performs a password login using the current homeserver.
|
||||
func login(username: String, password: String, initialDeviceName: String?, deviceID: String?) async -> Result<UserSessionProtocol, AuthenticationServiceError>
|
||||
/// Completes registration using the credentials obtained via the helper URL.
|
||||
func completeWebRegistration(using credentials: WebRegistrationCredentials) async -> Result<UserSessionProtocol, AuthenticationServiceError>
|
||||
|
||||
/// Resets the current configuration requiring `configure(for:flow:)` to be called again.
|
||||
func reset()
|
||||
|
||||
Reference in New Issue
Block a user