Simplify authentication logic

matrix.org now supports MAS so we don't need the web registration helper anymore.
This commit is contained in:
Doug
2025-04-10 15:09:40 +01:00
committed by Doug
parent b2c647b1af
commit 1557054753
32 changed files with 86 additions and 709 deletions
@@ -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()
@@ -1,32 +0,0 @@
//
// Copyright 2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import Foundation
import MatrixRustSDK
extension ClientProtocol {
func getElementWellKnown() async -> Result<MatrixRustSDK.ElementWellKnown, ClientProxyError> {
do {
let serverName = if let userIDServerName = try? userIdServerName() {
"https://\(userIDServerName)"
} else {
server()
}
guard let serverName,
let url = URL(string: serverName)?.appending(path: "/.well-known/element/element.json") else {
return .failure(.invalidServerName)
}
let response = try await getUrl(url: url.absoluteString)
let wellKnown = try makeElementWellKnown(string: response)
return .success(wellKnown)
} catch {
return .failure(.sdkError(error))
}
}
}
@@ -703,10 +703,6 @@ class ClientProxy: ClientProxyProtocol {
}
}
func getElementWellKnown() async -> Result<ElementWellKnown?, ClientProxyError> {
await client.getElementWellKnown().map(ElementWellKnown.init)
}
func clearCaches() async -> Result<Void, ClientProxyError> {
do {
return try await .success(client.clearCaches())
@@ -182,8 +182,6 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
func isAliasAvailable(_ alias: String) async -> Result<Bool, ClientProxyError>
func getElementWellKnown() async -> Result<ElementWellKnown?, ClientProxyError>
@discardableResult func clearCaches() async -> Result<Void, ClientProxyError>
// MARK: - Ignored users
@@ -1,17 +0,0 @@
//
// Copyright 2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import Foundation
import MatrixRustSDK
struct ElementWellKnown {
let registrationHelperURL: URL?
init?(_ wellKnown: MatrixRustSDK.ElementWellKnown) {
registrationHelperURL = wellKnown.registrationHelperUrl.flatMap(URL.init)
}
}