mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Enable OIDC token refresh in the NSE. (#1711)
Update the SDK and handle API changes.
This commit is contained in:
@@ -527,8 +527,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
|
||||
switch callback {
|
||||
case .didReceiveAuthError(let isSoftLogout):
|
||||
stateMachine.processEvent(.signOut(isSoft: isSoftLogout))
|
||||
case .updateRestorationToken:
|
||||
userSessionStore.refreshRestorationToken(for: userSession)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
@@ -44,7 +44,9 @@ class AuthenticationServiceProxy: AuthenticationServiceProxyProtocol {
|
||||
passphrase: nil,
|
||||
userAgent: UserAgentBuilder.makeASCIIUserAgent(),
|
||||
oidcConfiguration: oidcConfiguration,
|
||||
customSlidingSyncProxy: appSettings.slidingSyncProxyURL?.absoluteString)
|
||||
customSlidingSyncProxy: appSettings.slidingSyncProxyURL?.absoluteString,
|
||||
sessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
crossProcessRefreshLockId: InfoPlistReader.main.bundleIdentifier)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
@@ -86,8 +86,6 @@ class ClientProxy: ClientProxyProtocol {
|
||||
client.setDelegate(delegate: ClientDelegateWrapper { [weak self] isSoftLogout in
|
||||
self?.hasEncounteredAuthError = true
|
||||
self?.callbacks.send(.receivedAuthError(isSoftLogout: isSoftLogout))
|
||||
} tokenRefreshCallback: { [weak self] in
|
||||
self?.callbacks.send(.updateRestorationToken)
|
||||
})
|
||||
|
||||
await configureAppService()
|
||||
@@ -536,12 +534,9 @@ private class RoomListServiceSyncIndicatorListenerProxy: RoomListServiceSyncIndi
|
||||
|
||||
private class ClientDelegateWrapper: ClientDelegate {
|
||||
private let authErrorCallback: (Bool) -> Void
|
||||
private let tokenRefreshCallback: () -> Void
|
||||
|
||||
init(authErrorCallback: @escaping (Bool) -> Void,
|
||||
tokenRefreshCallback: @escaping () -> Void) {
|
||||
init(authErrorCallback: @escaping (Bool) -> Void) {
|
||||
self.authErrorCallback = authErrorCallback
|
||||
self.tokenRefreshCallback = tokenRefreshCallback
|
||||
}
|
||||
|
||||
// MARK: - ClientDelegate
|
||||
@@ -552,7 +547,6 @@ private class ClientDelegateWrapper: ClientDelegate {
|
||||
}
|
||||
|
||||
func didRefreshTokens() {
|
||||
MXLog.info("The session has updated tokens.")
|
||||
tokenRefreshCallback()
|
||||
MXLog.info("Delegating session updates to the ClientSessionDelegate.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import MatrixRustSDK
|
||||
enum ClientProxyCallback {
|
||||
case receivedSyncUpdate
|
||||
case receivedAuthError(isSoftLogout: Bool)
|
||||
case updateRestorationToken
|
||||
|
||||
var isSyncUpdate: Bool {
|
||||
if case .receivedSyncUpdate = self {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
import KeychainAccess
|
||||
import MatrixRustSDK
|
||||
|
||||
enum KeychainControllerService: String {
|
||||
case sessions
|
||||
@@ -86,4 +87,20 @@ class KeychainController: KeychainControllerProtocol {
|
||||
MXLog.error("Failed removing all tokens")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ClientSessionDelegate
|
||||
|
||||
func retrieveSessionFromKeychain(userId: String) throws -> Session {
|
||||
MXLog.info("Retrieving an updated Session from the keychain.")
|
||||
guard let session = restorationTokenForUsername(userId)?.session else {
|
||||
throw ClientError.Generic(msg: "Failed to find RestorationToken in the Keychain.")
|
||||
}
|
||||
return session
|
||||
}
|
||||
|
||||
func saveSessionInKeychain(session: Session) {
|
||||
MXLog.info("Saving session changes in the keychain.")
|
||||
let restorationToken = RestorationToken(session: session)
|
||||
setRestorationToken(restorationToken, forUsername: session.userId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,14 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
struct KeychainCredentials {
|
||||
let userID: String
|
||||
let restorationToken: RestorationToken
|
||||
}
|
||||
|
||||
protocol KeychainControllerProtocol {
|
||||
protocol KeychainControllerProtocol: ClientSessionDelegate {
|
||||
func setRestorationToken(_ restorationToken: RestorationToken, forUsername: String)
|
||||
func restorationTokenForUsername(_ username: String) -> RestorationToken?
|
||||
func restorationTokens() -> [KeychainCredentials]
|
||||
|
||||
@@ -65,7 +65,7 @@ extension NotificationItemProxyProtocol {
|
||||
return false
|
||||
case let .messageLike(content):
|
||||
switch content {
|
||||
case let .roomMessage(messageType):
|
||||
case let .roomMessage(messageType, _):
|
||||
switch messageType {
|
||||
case .image, .video, .audio:
|
||||
return true
|
||||
|
||||
@@ -229,7 +229,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
|
||||
inviterProxy = RoomMemberProxy(member: inviter, backgroundTaskService: backgroundTaskService)
|
||||
}
|
||||
|
||||
let notificationMode = roomInfo.notificationMode.flatMap { RoomNotificationModeProxy.from(roomNotificationMode: $0) }
|
||||
let notificationMode = roomInfo.userDefinedNotificationMode.flatMap { RoomNotificationModeProxy.from(roomNotificationMode: $0) }
|
||||
|
||||
let details = RoomSummaryDetails(id: roomInfo.id,
|
||||
name: roomInfo.name ?? roomInfo.id,
|
||||
|
||||
@@ -94,8 +94,6 @@ class UserSession: UserSessionProtocol {
|
||||
case .receivedAuthError(let isSoftLogout):
|
||||
callbacks.send(.didReceiveAuthError(isSoftLogout: isSoftLogout))
|
||||
tearDownAuthErrorWatchdog()
|
||||
case .updateRestorationToken:
|
||||
callbacks.send(.updateRestorationToken)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ enum UserSessionCallback {
|
||||
case sessionVerificationNeeded
|
||||
case didVerifySession
|
||||
case didReceiveAuthError(isSoftLogout: Bool)
|
||||
case updateRestorationToken
|
||||
}
|
||||
|
||||
protocol UserSessionProtocol {
|
||||
|
||||
@@ -31,6 +31,8 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
/// The base directory where all session data is stored.
|
||||
let baseDirectory: URL
|
||||
|
||||
var clientSessionDelegate: ClientSessionDelegate { keychainController }
|
||||
|
||||
init(backgroundTaskService: BackgroundTaskServiceProtocol) {
|
||||
keychainController = KeychainController(service: .sessions,
|
||||
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
||||
@@ -76,16 +78,6 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
|
||||
func refreshRestorationToken(for userSession: UserSessionProtocol) -> Result<Void, UserSessionStoreError> {
|
||||
guard let restorationToken = userSession.clientProxy.restorationToken else {
|
||||
return .failure(.failedRefreshingRestoreToken)
|
||||
}
|
||||
|
||||
keychainController.setRestorationToken(restorationToken, forUsername: userSession.clientProxy.userID)
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
func logout(userSession: UserSessionProtocol) {
|
||||
let userID = userSession.clientProxy.userID
|
||||
@@ -115,6 +107,8 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
.username(username: credentials.userID)
|
||||
.homeserverUrl(url: credentials.restorationToken.session.homeserverUrl)
|
||||
.userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent())
|
||||
.enableCrossProcessRefreshLock(processId: InfoPlistReader.main.bundleIdentifier,
|
||||
sessionDelegate: keychainController)
|
||||
.serverVersions(versions: ["v1.0", "v1.1", "v1.2", "v1.3", "v1.4", "v1.5"]) // FIXME: Quick and dirty fix for stopping version requests on startup https://github.com/matrix-org/matrix-rust-sdk/pull/1376
|
||||
|
||||
do {
|
||||
@@ -133,9 +127,9 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
private func setupProxyForClient(_ client: Client) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
||||
do {
|
||||
let session = try client.session()
|
||||
let userId = try client.userId()
|
||||
let userID = try client.userId()
|
||||
|
||||
keychainController.setRestorationToken(RestorationToken(session: session), forUsername: userId)
|
||||
keychainController.setRestorationToken(RestorationToken(session: session), forUsername: userID)
|
||||
} catch {
|
||||
MXLog.error("Failed setting up user session with error: \(error)")
|
||||
return .failure(.failedSettingUpSession)
|
||||
|
||||
@@ -37,15 +37,14 @@ protocol UserSessionStoreProtocol {
|
||||
/// Returns the location to store user data for a particular username.
|
||||
var baseDirectory: URL { get }
|
||||
|
||||
/// Returns the delegate that should handle any changes to a `Client`'s `Session`.
|
||||
var clientSessionDelegate: ClientSessionDelegate { get }
|
||||
|
||||
/// Restores an existing user session.
|
||||
func restoreUserSession() async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
|
||||
/// Creates a user session for a new client from the SDK.
|
||||
func userSession(for client: Client) async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
|
||||
/// Refresh the restore token of the client for a given session.
|
||||
@discardableResult
|
||||
func refreshRestorationToken(for userSession: UserSessionProtocol) -> Result<Void, UserSessionStoreError>
|
||||
|
||||
/// Logs out of the specified session.
|
||||
func logout(userSession: UserSessionProtocol)
|
||||
|
||||
Reference in New Issue
Block a user