Enable OIDC support (#1541)

Notification content is bypassed for now.
This commit is contained in:
Doug
2023-08-22 15:53:27 +01:00
committed by GitHub
parent d633b87609
commit 5e3a32157a
27 changed files with 239 additions and 70 deletions
@@ -0,0 +1,51 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import AuthenticationServices
/// Presents a web authentication session that will display the user's account settings page.
///
/// A web authentication session is used so that the same session used for login is available
/// meaning that the user doesn't need to sign in again. `SFSafariViewController` doesn't
/// have access to this session, and for some reason `prefersEphemeralWebBrowserSession`
/// isn't sharing the session back to Safari.
@MainActor
class OIDCAccountSettingsPresenter: NSObject {
private let accountURL: URL
private let presentationAnchor: UIWindow
private let oidcRedirectURL: URL
init(accountURL: URL, presentationAnchor: UIWindow) {
self.accountURL = accountURL
self.presentationAnchor = presentationAnchor
oidcRedirectURL = ServiceLocator.shared.settings.oidcRedirectURL
super.init()
}
/// Presents a web authentication session for the supplied data.
func start() {
let session = ASWebAuthenticationSession(url: accountURL, callbackURLScheme: oidcRedirectURL.scheme) { _, _ in }
session.prefersEphemeralWebBrowserSession = false
session.presentationContextProvider = self
session.start()
}
}
// MARK: ASWebAuthenticationPresentationContextProviding
extension OIDCAccountSettingsPresenter: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { presentationAnchor }
}
@@ -48,6 +48,8 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
switch action {
case .close:
self.callback?(.dismiss)
case .account:
self.presentAccountSettings()
case .analytics:
self.presentAnalyticsScreen()
case .reportBug:
@@ -74,6 +76,26 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
// MARK: - Private
private var accountSettingsPresenter: OIDCAccountSettingsPresenter?
private func presentAccountSettings() {
guard let accountURL = viewModel.context.viewState.accountURL else {
MXLog.error("Account URL is missing.")
return
}
guard let window = viewModel.context.viewState.window else {
MXLog.error("The window is missing.")
return
}
// Safari never works in the simulator, use a Web Authentication Session instead.
accountSettingsPresenter = OIDCAccountSettingsPresenter(accountURL: accountURL, presentationAnchor: window)
accountSettingsPresenter?.start()
// Safari isn't working with the shared browser session 😕
// UIApplication.shared.open(accountURL)
}
private func presentAnalyticsScreen() {
let coordinator = AnalyticsSettingsScreenCoordinator(parameters: .init(appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics))
@@ -19,6 +19,7 @@ import UIKit
enum SettingsScreenViewModelAction {
case close
case account
case analytics
case reportBug
case about
@@ -32,11 +33,15 @@ struct SettingsScreenViewState: BindableState {
var bindings: SettingsScreenViewStateBindings
var deviceID: String?
var userID: String
var accountURL: URL?
var userAvatarURL: URL?
var userDisplayName: String?
var showSessionVerificationSection: Bool
var showNotificationSettings: Bool
var showDeveloperOptions: Bool
/// The presentation anchor used to display the OIDC account URL.
var window: UIWindow?
}
struct SettingsScreenViewStateBindings {
@@ -45,6 +50,7 @@ struct SettingsScreenViewStateBindings {
enum SettingsScreenViewAction {
case close
case account
case analytics
case reportBug
case about
@@ -53,4 +59,7 @@ enum SettingsScreenViewAction {
case changedTimelineStyle
case developerOptions
case notifications
/// Updates the window used for the OIDC account URL anchor.
case updateWindow(UIWindow)
}
@@ -39,6 +39,7 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
super.init(initialViewState: .init(bindings: bindings,
deviceID: userSession.deviceID,
userID: userSession.userID,
accountURL: userSession.clientProxy.accountURL,
showSessionVerificationSection: showSessionVerificationSection,
showNotificationSettings: appSettings.notificationSettingsEnabled,
showDeveloperOptions: appSettings.canShowDeveloperOptions),
@@ -85,6 +86,8 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
switch viewAction {
case .close:
callback?(.close)
case .account:
callback?(.account)
case .analytics:
callback?(.analytics)
case .reportBug:
@@ -101,6 +104,12 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
callback?(.notifications)
case .developerOptions:
callback?(.developerOptions)
case .updateWindow(let window):
Task {
guard state.window != window else { return }
state.window = window
}
}
}
}
@@ -46,6 +46,9 @@ struct SettingsScreen: View {
doneButton
}
}
.introspect(.window, on: .iOS(.v16)) { window in
context.send(viewAction: .updateWindow(window))
}
}
private var versionText: Text {
@@ -100,6 +103,17 @@ struct SettingsScreen: View {
private var simplifiedSection: some View {
Section {
// Account
if context.viewState.accountURL != nil {
ListRow(label: .default(title: L10n.screenSettingsOidcAccount,
systemIcon: .person),
kind: .button {
context.send(viewAction: .account)
})
.accessibilityIdentifier("notificationsButton")
}
// Message layout
ListRow(label: .default(title: L10n.commonMessageLayout,
systemIcon: .rectangleGrid1x2),
kind: .picker(selection: $context.timelineStyle,
@@ -207,7 +221,8 @@ struct SettingsScreen_Previews: PreviewProvider {
verificationController.isVerified = false
let userSession = MockUserSession(sessionVerificationController: verificationController,
clientProxy: MockClientProxy(userID: "@userid:example.com",
deviceID: "AAAAAAAAAAA"),
deviceID: "AAAAAAAAAAA",
accountURL: "https://matrix.org/account"),
mediaProvider: MockMediaProvider())
ServiceLocator.shared.settings.notificationSettingsEnabled = true
return SettingsScreenViewModel(userSession: userSession,