diff --git a/iOSClient/AppDelegate.swift b/iOSClient/AppDelegate.swift index 73e4333534..18b206cf06 100644 --- a/iOSClient/AppDelegate.swift +++ b/iOSClient/AppDelegate.swift @@ -487,6 +487,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD NCKeychain().setClientCertificate(account: account, p12Data: NCNetworking.shared.p12Data, p12Password: NCNetworking.shared.p12Password) self.changeAccount(account, userProfile: userProfile) } else { + NextcloudKit.shared.setup(account: self.account, user: self.user, userId: self.userId, password: self.password, urlBase: self.urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) UIApplication.shared.firstWindow?.rootViewController?.present(alertController, animated: true) diff --git a/iOSClient/Login/NCLogin.swift b/iOSClient/Login/NCLogin.swift index b2fced42f3..f333fa3630 100644 --- a/iOSClient/Login/NCLogin.swift +++ b/iOSClient/Login/NCLogin.swift @@ -164,10 +164,7 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) - NCNetworking.shared.certificateDelegate = self - handleLoginWithAppConfig() - baseUrl.text = urlBase } @@ -200,15 +197,12 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { if let serverUrl = configurationManaged[NCGlobal.shared.configuration_serverUrl] as? String { self.configServerUrl = serverUrl } - if let username = configurationManaged[NCGlobal.shared.configuration_username] as? String, !username.isEmpty, username.lowercased() != "username" { self.configUsername = username } - if let password = configurationManaged[NCGlobal.shared.configuration_password] as? String, !password.isEmpty, password.lowercased() != "password" { self.configPassword = password } - if let apppassword = configurationManaged[NCGlobal.shared.configuration_apppassword] as? String, !apppassword.isEmpty, apppassword.lowercased() != "apppassword" { self.configAppPassword = apppassword } @@ -216,13 +210,15 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } // AppConfig - if let url = configServerUrl { - if let user = self.configUsername, let password = configAppPassword { - appDelegate.createAccount(url: url, user: user, password: password) { _ in } - } else if let user = self.configUsername, let password = configPassword { - getAppPassword(url: url, user: user, password: password) + if let serverUrl = configServerUrl { + if let username = self.configUsername, let password = configAppPassword { + createAccount(server: serverUrl, username: username, password: password) + return + } else if let username = self.configUsername, let password = configPassword { + getAppPassword(serverUrl: serverUrl, username: username, password: password) + return } else { - urlBase = url + urlBase = serverUrl } } } @@ -236,16 +232,13 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } func textFieldDidBeginEditing(_ textField: UITextField) { - self.activeTextField = textField } // MARK: - Keyboard notification @objc internal func keyboardWillShow(_ notification: Notification?) { - activeTextfieldDiff = 0 - if let info = notification?.userInfo, let centerObject = self.activeTextField.superview?.convert(self.activeTextField.center, to: nil) { let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey @@ -288,9 +281,7 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { // MARK: - Share accounts View Controller @objc func openShareAccountsViewController() { - if let shareAccounts = self.shareAccounts, let vc = UIStoryboard(name: "NCShareAccounts", bundle: nil).instantiateInitialViewController() as? NCShareAccounts { - vc.accounts = shareAccounts vc.enableTimerProgress = false vc.dismissDidEnterBackground = false @@ -299,7 +290,6 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { let screenHeighMax = UIScreen.main.bounds.height - (UIScreen.main.bounds.height / 5) let numberCell = shareAccounts.count let height = min(CGFloat(numberCell * Int(vc.heightCell) + 45), screenHeighMax) - let popup = NCPopupViewController(contentController: vc, popupWidth: 300, popupHeight: height + 20) self.present(popup, animated: true) @@ -312,7 +302,6 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { guard var url = baseUrl.text?.trimmingCharacters(in: .whitespacesAndNewlines) else { return } if url.hasSuffix("/") { url = String(url.dropLast()) } if url.isEmpty { return } - // Check whether baseUrl contain protocol. If not add https:// by default. if url.hasPrefix("https") == false && url.hasPrefix("http") == false { url = "https://" + url @@ -322,53 +311,35 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } func isUrlValid(url: String, user: String? = nil) { - loginButton.isEnabled = false - NextcloudKit.shared.getServerStatus(serverUrl: url) { serverInfoResult in - switch serverInfoResult { case .success(let serverInfo): - if let host = URL(string: url)?.host { NCNetworking.shared.writeCertificate(host: host) } - NextcloudKit.shared.getLoginFlowV2(serverUrl: url) { token, endpoint, login, _, error in - self.loginButton.isEnabled = true - // Login Flow V2 if error == .success, let token, let endpoint, let login { let vc = UIHostingController(rootView: NCLoginPoll(loginFlowV2Token: token, loginFlowV2Endpoint: endpoint, loginFlowV2Login: login)) - self.present(vc, animated: true) } else if serverInfo.versionMajor < NCGlobal.shared.nextcloudVersion12 { // No login flow available - let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_webflow_not_available_", comment: ""), preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) - self.present(alertController, animated: true, completion: { }) } } - case .failure(let error): - self.loginButton.isEnabled = true - if error.errorCode == NSURLErrorServerCertificateUntrusted { - let alertController = UIAlertController(title: NSLocalizedString("_ssl_certificate_untrusted_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { _ in if let host = URL(string: url)?.host { NCNetworking.shared.writeCertificate(host: host) } })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { _ in })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_certificate_details_", comment: ""), style: .default, handler: { _ in if let navigationController = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil).instantiateInitialViewController() as? UINavigationController, let viewController = navigationController.topViewController as? NCViewCertificateDetails { @@ -378,15 +349,10 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { self.present(navigationController, animated: true) } })) - self.present(alertController, animated: true) - } else { - let alertController = UIAlertController(title: NSLocalizedString("_connection_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) - self.present(alertController, animated: true, completion: { }) } } @@ -398,7 +364,6 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { func dismissQRCode(_ value: String?, metadataType: String?) { guard var value = value else { return } let protocolLogin = NCBrandOptions.shared.webLoginAutenticationProtocol + "login/" - if value.hasPrefix(protocolLogin) && value.contains("user:") && value.contains("password:") && value.contains("server:") { value = value.replacingOccurrences(of: protocolLogin, with: "") let valueArray = value.components(separatedBy: "&") @@ -421,8 +386,14 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { if let host = URL(string: url)?.host { NCNetworking.shared.writeCertificate(host: host) } - appDelegate.createAccount(url: url, user: user, password: password) { error in - if error == .success { + let urlBase = url + let account = user + " " + user + NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase) + NextcloudKit.shared.getUserProfile { _, userProfile, _, error in + if error == .success, let userProfile { + NCManageDatabase.shared.deleteAccount(account) + NCManageDatabase.shared.addAccount(account, urlBase: url, user: user, userId: userProfile.userId, password: password) + self.appDelegate.changeAccount(account, userProfile: userProfile) let window = UIApplication.shared.firstWindow if window?.rootViewController is NCMainTabBarController { self.dismiss(animated: true) @@ -438,6 +409,7 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } } } else { + NextcloudKit.shared.setup(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, password: self.appDelegate.password, urlBase: self.appDelegate.urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) self.present(alertController, animated: true) @@ -445,23 +417,20 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } } else if error.errorCode == NSURLErrorServerCertificateUntrusted { let alertController = UIAlertController(title: NSLocalizedString("_ssl_certificate_untrusted_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { _ in if let host = URL(string: url)?.host { NCNetworking.shared.writeCertificate(host: host) } })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { _ in })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_certificate_details_", comment: ""), style: .default, handler: { _ in if let navigationController = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil).instantiateInitialViewController() { self.present(navigationController, animated: true) } })) - self.present(alertController, animated: true) } else { + let message = NSLocalizedString("_not_possible_connect_to_server_", comment: "") + ".\n" + error.errorDescription let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: message, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) @@ -469,30 +438,31 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } } - private func getAppPassword(url: String, user: String, password: String) { - NextcloudKit.shared.getAppPassword(url: url, user: user, password: password) { token, _, error in - if error == .success, let password = token { - self.appDelegate.createAccount(url: url, user: user, password: password) { error in - if error == .success { - let window = UIApplication.shared.firstWindow - if window?.rootViewController is NCMainTabBarController { - self.dismiss(animated: true) - } else { - if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { - mainTabBarController.modalPresentationStyle = .fullScreen - mainTabBarController.view.alpha = 0 - window?.rootViewController = mainTabBarController - window?.makeKeyAndVisible() - UIView.animate(withDuration: 0.5) { - mainTabBarController.view.alpha = 1 - } - } + private func createAccount(server: String, username: String, password: String) { + appDelegate.createAccount(url: server, user: username, password: password) { error in + if error == .success { + let window = UIApplication.shared.firstWindow + if window?.rootViewController is NCMainTabBarController { + self.dismiss(animated: true) + } else { + if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { + mainTabBarController.modalPresentationStyle = .fullScreen + mainTabBarController.view.alpha = 0 + window?.rootViewController = mainTabBarController + window?.makeKeyAndVisible() + UIView.animate(withDuration: 0.5) { + mainTabBarController.view.alpha = 1 } - } else { - NCContentPresenter().showError(error: error) - self.dismiss(animated: true, completion: nil) } } + } + } + } + + private func getAppPassword(serverUrl: String, username: String, password: String) { + NextcloudKit.shared.getAppPassword(url: serverUrl, user: username, password: password) { token, _, error in + if error == .success, let password = token { + self.createAccount(server: serverUrl, username: username, password: password) } else { NCContentPresenter().showError(error: error) self.dismiss(animated: true, completion: nil) @@ -510,47 +480,35 @@ extension NCLogin: NCShareAccountsDelegate { extension NCLogin: ClientCertificateDelegate, UIDocumentPickerDelegate { func didAskForClientCertificate() { let alertNoCertFound = UIAlertController(title: NSLocalizedString("_no_client_cert_found_", comment: ""), message: NSLocalizedString("_no_client_cert_found_desc_", comment: ""), preferredStyle: .alert) - alertNoCertFound.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)) - alertNoCertFound.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in let documentProviderMenu = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.pkcs12]) documentProviderMenu.delegate = self - self.present(documentProviderMenu, animated: true, completion: nil) })) - present(alertNoCertFound, animated: true) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { let alertEnterPassword = UIAlertController(title: NSLocalizedString("_client_cert_enter_password_", comment: ""), message: "", preferredStyle: .alert) - alertEnterPassword.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)) - alertEnterPassword.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in // let documentProviderMenu = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.pkcs12]) NCNetworking.shared.p12Data = try? Data(contentsOf: urls[0]) NCNetworking.shared.p12Password = alertEnterPassword.textFields?[0].text - self.login() })) - alertEnterPassword.addTextField { textField in textField.isSecureTextEntry = true } - present(alertEnterPassword, animated: true) } func onIncorrectPassword() { NCNetworking.shared.p12Data = nil NCNetworking.shared.p12Password = nil - let alertWrongPassword = UIAlertController(title: NSLocalizedString("_client_cert_wrong_password_", comment: ""), message: "", preferredStyle: .alert) - alertWrongPassword.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default)) - present(alertWrongPassword, animated: true) } } diff --git a/iOSClient/Login/NCLoginProvider.swift b/iOSClient/Login/NCLoginProvider.swift index 3d7eb65790..d55247fb4f 100644 --- a/iOSClient/Login/NCLoginProvider.swift +++ b/iOSClient/Login/NCLoginProvider.swift @@ -128,32 +128,10 @@ extension NCLoginProvider: WKNavigationDelegate { } if !server.isEmpty, !user.isEmpty, !password.isEmpty { - let url: String = server.replacingOccurrences(of: "/server:", with: "") - let user: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ") + let server: String = server.replacingOccurrences(of: "/server:", with: "") + let username: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ") let password: String = password.replacingOccurrences(of: "password:", with: "") - - appDelegate.createAccount(url: url, user: user, password: password) { error in - if error == .success { - let window = UIApplication.shared.firstWindow - if window?.rootViewController is NCMainTabBarController { - self.dismiss(animated: true) - } else { - if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { - controller.modalPresentationStyle = .fullScreen - controller.view.alpha = 0 - window?.rootViewController = controller - window?.makeKeyAndVisible() - UIView.animate(withDuration: 0.5) { - controller.view.alpha = 1 - } - } - } - } else { - let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) - self.present(alertController, animated: true) - } - } + createAccount(server: server, username: username, password: password) } } } @@ -179,4 +157,40 @@ extension NCLoginProvider: WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { NCActivityIndicator.shared.stop() } + + // MARK: - + + func createAccount(server: String, username: String, password: String) { + var urlBase = server + if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) } + let account: String = "\(username) \(urlBase)" + let user = username + + NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase) + NextcloudKit.shared.getUserProfile { _, userProfile, _, error in + if error == .success, let userProfile { + NCManageDatabase.shared.deleteAccount(account) + NCManageDatabase.shared.addAccount(account, urlBase: urlBase, user: user, userId: userProfile.userId, password: password) + self.appDelegate.changeAccount(account, userProfile: userProfile) + let window = UIApplication.shared.firstWindow + if window?.rootViewController is NCMainTabBarController { + self.dismiss(animated: true) + } else { + if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { + controller.modalPresentationStyle = .fullScreen + controller.view.alpha = 0 + window?.rootViewController = controller + window?.makeKeyAndVisible() + UIView.animate(withDuration: 0.5) { + controller.view.alpha = 1 + } + } + } + } else { + let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) + self.present(alertController, animated: true) + } + } + } } diff --git a/iOSClient/Login/NCLoginQRCode.swift b/iOSClient/Login/NCLoginQRCode.swift index 6ef2805d9b..9712befe4d 100644 --- a/iOSClient/Login/NCLoginQRCode.swift +++ b/iOSClient/Login/NCLoginQRCode.swift @@ -67,7 +67,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate { return try QRCodeReader.supportsMetadataObjectTypes() } catch let error as NSError { let alert: UIAlertController - switch error.code { case -11852: alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_authorized_", comment: ""), preferredStyle: .alert) @@ -84,7 +83,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate { alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_supported_", comment: ""), preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .cancel, handler: nil)) } - delegate?.present(alert, animated: true, completion: nil) return false }