Files
bundesmessenger-ios/Riot/Modules/Authentication/SSO/SSOAuthenticationPresenter.swift
T
JanNiklas Grabowski b298dedc22 chore: update from foss 1.11.19 (MESSENGER-6656)
Merge commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1' into feature/foss_update_1_11_19

* commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1':
  finish version++
  version++
  comments
  update submodule
  remove obsolete tests
  removed unused code
  update submodule
  fix
  Libolm removal
  update license macro
  update license
  Prepare for new sprint

# Conflicts:
#	Config/AppVersion.xcconfig
#	IDETemplateMacros.plist
#	LICENSE
#	README.md
#	Riot/Categories/MXSession+Riot.m
#	Riot/Managers/EncryptionKeyManager/EncryptionKeyManager.swift
#	Riot/Managers/KeyValueStorage/Extensions/Keychain.swift
#	Riot/Managers/KeyValueStorage/KeyValueStore.swift
#	Riot/Managers/KeyValueStorage/KeychainStore.swift
#	Riot/Managers/KeyValueStorage/MemoryStore.swift
#	Riot/Managers/PushNotification/PushNotificationService.m
#	Riot/Managers/Settings/RiotSettings.swift
#	Riot/Managers/Settings/Shared/RiotSharedSettings.swift
#	Riot/Modules/Analytics/AnalyticsUIElement.swift
#	Riot/Modules/Application/AppCoordinator.swift
#	Riot/Modules/Application/LegacyAppDelegate.h
#	Riot/Modules/Application/LegacyAppDelegate.m
#	Riot/Modules/Authentication/Legacy/AuthenticationViewController.h
#	Riot/Modules/Authentication/Legacy/AuthenticationViewController.m
#	Riot/Modules/Authentication/Legacy/Views/AuthInputsView.h
#	Riot/Modules/Authentication/Legacy/Views/AuthInputsView.m
#	Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m
#	Riot/Modules/Common/Recents/RecentsViewController.m
#	Riot/Modules/Common/WebViewController/WebViewViewController.m
#	Riot/Modules/Contacts/Details/ContactDetailsViewController.m
#	Riot/Modules/Contacts/Views/ContactTableViewCell.m
#	Riot/Modules/Favorites/FavouritesViewController.h
#	Riot/Modules/Favorites/FavouritesViewController.m
#	Riot/Modules/GlobalSearch/UnifiedSearchViewController.m
#	Riot/Modules/People/PeopleViewController.h
#	Riot/Modules/People/PeopleViewController.m
#	Riot/Modules/Room/ContextualMenu/ReactionsMenu/ReactionsMenuViewModel.swift
#	Riot/Modules/Room/DataSources/RoomDataSource.m
#	Riot/Modules/Room/Files/RoomFilesViewController.m
#	Riot/Modules/Room/Members/Detail/RoomMemberDetailsViewController.m
#	Riot/Modules/Room/Members/RoomParticipantsViewController.m
#	Riot/Modules/Room/RoomViewController.m
#	Riot/Modules/Room/Settings/RoomSettingsViewController.m
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCell.swift
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCellContentView.swift
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroViewData.swift
#	Riot/Modules/Room/TimelineCells/RoomTimelineCellIdentifier.h
#	Riot/Modules/Rooms/RoomsViewController.h
#	Riot/Modules/Rooms/ShowDirectory/Cells/Network/DirectoryNetworkTableHeaderFooterView.swift
#	Riot/Modules/Rooms/ShowDirectory/Cells/Room/DirectoryRoomTableViewCell.swift
#	Riot/Modules/Rooms/ShowDirectory/PublicRoomsDirectoryViewModel.swift
#	Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyCoordinator.swift
#	Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyViewController.swift
#	Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseCoordinator.swift
#	Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseViewController.swift
#	Riot/Modules/Secrets/Recover/SecretsRecoveryCoordinator.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewController.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModel.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModelType.swift
#	Riot/Modules/SetPinCode/PinCodePreferences.swift
#	Riot/Modules/SetPinCode/SetupBiometrics/BiometricsAuthenticationPresenter.swift
#	Riot/Modules/Settings/Security/ManageSession/ManageSessionViewController.m
#	Riot/Modules/Settings/Security/SecurityViewController.m
#	Riot/Modules/Settings/SettingsViewController.m
#	Riot/Modules/SplitView/SplitViewCoordinator.swift
#	Riot/Modules/SplitView/SplitViewCoordinatorType.swift
#	Riot/Modules/StartChat/StartChatViewController.m
#	Riot/Modules/TabBar/MasterTabBarController.h
#	Riot/Modules/TabBar/MasterTabBarController.m
#	Riot/Utils/EventFormatter.m
#	Riot/Utils/HTMLFormatter.swift
#	Riot/Utils/Tools.m
#	RiotNSE/NotificationService.swift
2024-10-18 15:45:54 +02:00

168 lines
7.1 KiB
Swift

//
// Copyright 2020-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Foundation
import SafariServices
@objc protocol SSOAuthenticationPresenterDelegate {
func ssoAuthenticationPresenterDidCancel(_ presenter: SSOAuthenticationPresenter)
func ssoAuthenticationPresenter(_ presenter: SSOAuthenticationPresenter, authenticationDidFailWithError error: Error)
func ssoAuthenticationPresenter(_ presenter: SSOAuthenticationPresenter,
authenticationSucceededWithToken token: String,
usingIdentityProvider identityProvider: SSOIdentityProvider?)
}
enum SSOAuthenticationPresenterError: Error {
case failToLoadAuthenticationURL
}
/// SSOAuthenticationPresenter enables to present single sign-on authentication
@objcMembers
final class SSOAuthenticationPresenter: NSObject {
// MARK: - Constants
// MARK: - Properties
private let ssoAuthenticationService: SSOAuthenticationServiceProtocol
// MARK: Private
private weak var presentingViewController: UIViewController?
private var authenticationSession: SSOAuthentificationSessionProtocol?
private weak var safariViewController: SFSafariViewController?
// MARK: Public
private(set) var identityProvider: SSOIdentityProvider?
weak var delegate: SSOAuthenticationPresenterDelegate?
// MARK: - Setup
init(ssoAuthenticationService: SSOAuthenticationServiceProtocol) {
self.ssoAuthenticationService = ssoAuthenticationService
super.init()
}
// MARK: - Public
func present(forIdentityProvider identityProvider: SSOIdentityProvider?,
with transactionId: String,
from presentingViewController: UIViewController,
animated: Bool) {
guard let authenticationURL = self.ssoAuthenticationService.authenticationURL(for: identityProvider?.id, transactionId: transactionId) else {
self.delegate?.ssoAuthenticationPresenter(self, authenticationDidFailWithError: SSOAuthenticationPresenterError.failToLoadAuthenticationURL)
return
}
self.identityProvider = identityProvider
self.presentingViewController = presentingViewController
// bwi #5308 if there is a url scheme call sso directly with that scheme
if let urlScheme = AppConfigService.shared.externalUrlScheme() {
startUrlSchemeAuthentication(with: authenticationURL)
} else if #unavailable(iOS 15.0), UIAccessibility.isGuidedAccessEnabled {
// SFAuthenticationSession and ASWebAuthenticationSession doesn't work with guided access (rdar://48376122)
// Confirmed to be fixed on iOS 15, haven't been able to test on iOS 14.
presentSafariViewController(with: authenticationURL, animated: animated)
} else {
startAuthenticationSession(with: authenticationURL)
}
}
func dismiss(animated: Bool, completion: (() -> Void)?) {
if let safariViewController = self.safariViewController {
safariViewController.dismiss(animated: animated, completion: completion)
}
self.authenticationSession?.cancel()
}
// MARK: - Private
private func presentSafariViewController(with authenticationURL: URL, animated: Bool) {
guard let presentingViewController = self.presentingViewController else {
return
}
let safariViewController = SFSafariViewController(url: authenticationURL)
safariViewController.dismissButtonStyle = .cancel
safariViewController.delegate = self
presentingViewController.present(safariViewController, animated: animated, completion: nil)
self.safariViewController = safariViewController
}
// bwi #5303 when there is an url scheme use that to open authentication
private func startUrlSchemeAuthentication(with authenticationURL: URL) {
guard let presentingViewController = self.presentingViewController else {
return
}
if UIApplication.shared.canOpenURL(authenticationURL) {
UIApplication.shared.open(authenticationURL)
} else {
let alert = UIAlertController(title: BWIL10n.ssoAuthenticationUrlSchemeErrorTitle, message: BWIL10n.ssoAuthenticationUrlSchemeErrorMessage(AppConfigService.shared.externalUrlScheme() ?? "none"), preferredStyle: .alert)
alert.addAction(UIAlertAction(title: VectorL10n.cancel, style: .cancel))
presentingViewController.present(alert, animated: true, completion: nil)
}
}
private func startAuthenticationSession(with authenticationURL: URL) {
guard let presentingViewController = self.presentingViewController else {
return
}
let authenticationSession = SSOAuthentificationSession()
if let presentingWindow = presentingViewController.view.window {
let contextProvider = SSOAuthenticationSessionContextProvider(window: presentingWindow)
authenticationSession.setContextProvider(contextProvider)
}
authenticationSession.authenticate(with: authenticationURL, callbackURLScheme: self.ssoAuthenticationService.callBackURLScheme) { [weak self] (callBackURL, error) in
guard let self = self else {
return
}
if let error = error {
if case SSOAuthentificationSessionError.userCanceled = error {
self.delegate?.ssoAuthenticationPresenterDidCancel(self)
} else {
self.delegate?.ssoAuthenticationPresenter(self, authenticationDidFailWithError: error)
}
} else if let successURL = callBackURL {
if let loginToken = self.ssoAuthenticationService.loginToken(from: successURL) {
self.delegate?.ssoAuthenticationPresenter(self, authenticationSucceededWithToken: loginToken, usingIdentityProvider: self.identityProvider)
} else {
MXLog.debug("SSOAuthenticationPresenter: Login token not found")
self.delegate?.ssoAuthenticationPresenter(self, authenticationDidFailWithError: SSOAuthenticationServiceError.tokenNotFound)
}
}
}
self.authenticationSession = authenticationSession
}
}
// MARK: - SFSafariViewControllerDelegate
extension SSOAuthenticationPresenter: SFSafariViewControllerDelegate {
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
self.delegate?.ssoAuthenticationPresenterDidCancel(self)
}
func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
if !didLoadSuccessfully {
self.delegate?.ssoAuthenticationPresenter(self, authenticationDidFailWithError: SSOAuthenticationPresenterError.failToLoadAuthenticationURL)
}
}
}