NCLoginWeb.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // NCLoginWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/08/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class NCLoginWeb: UIViewController {
  25. var webView: WKWebView?
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. @objc var urlBase = ""
  28. @IBOutlet weak var buttonExit: UIButton!
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. let config = WKWebViewConfiguration()
  32. config.websiteDataStore = WKWebsiteDataStore.nonPersistent()
  33. webView = WKWebView(frame: CGRect.zero, configuration: config)
  34. webView!.navigationDelegate = self
  35. view.addSubview(webView!)
  36. webView!.translatesAutoresizingMaskIntoConstraints = false
  37. webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  38. webView!.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  39. webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  40. webView!.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  41. // ADD k_flowEndpoint for Web Flow
  42. if NCBrandOptions.sharedInstance.use_login_web_personalized == false && urlBase != NCBrandOptions.sharedInstance.linkloginPreferredProviders {
  43. urlBase = urlBase + k_flowEndpoint
  44. }
  45. // buttonExitVisible
  46. self.view.bringSubviewToFront(buttonExit)
  47. loadWebPage(webView: webView!, url: URL(string: urlBase)!)
  48. }
  49. override func viewDidAppear(_ animated: Bool) {
  50. super.viewDidAppear(animated)
  51. // Stop timer error network
  52. appDelegate.timerErrorNetworking.invalidate()
  53. }
  54. override func viewDidDisappear(_ animated: Bool) {
  55. super.viewDidDisappear(animated)
  56. // Start timer error network
  57. appDelegate.startTimerErrorNetworking()
  58. }
  59. func loadWebPage(webView: WKWebView, url: URL) {
  60. let language = NSLocale.preferredLanguages[0] as String
  61. var request = URLRequest(url: url)
  62. request.setValue(CCUtility.getUserAgent(), forHTTPHeaderField: "User-Agent")
  63. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  64. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  65. webView.load(request)
  66. }
  67. @IBAction func touchUpInsideButtonExit(_ sender: UIButton) {
  68. self.dismiss(animated: true) {
  69. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissCCLogin"), object: nil, userInfo: nil)
  70. }
  71. }
  72. }
  73. extension NCLoginWeb: WKNavigationDelegate {
  74. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  75. guard let url = webView.url else { return }
  76. let urlString: String = url.absoluteString.lowercased()
  77. if (urlString.hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true) {
  78. var server: String = ""
  79. var user: String = ""
  80. var password: String = ""
  81. let keyValue = url.path.components(separatedBy: "&")
  82. for value in keyValue {
  83. if value.contains("server:") { server = value }
  84. if value.contains("user:") { user = value }
  85. if value.contains("password:") { password = value }
  86. }
  87. if server != "" && user != "" && password != "" {
  88. var serverUrl: String = server.replacingOccurrences(of: "/server:", with: "")
  89. // Login Flow NC 12
  90. if (NCBrandOptions.sharedInstance.use_login_web_personalized == false && serverUrl.hasPrefix("http://") == false && serverUrl.hasPrefix("https://") == false) {
  91. serverUrl = urlBase
  92. }
  93. if (serverUrl.last == "/") {
  94. serverUrl = String(serverUrl.dropLast())
  95. }
  96. let username: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ")
  97. let token: String = password.replacingOccurrences(of: "password:", with: "")
  98. let account : String = "\(username) \(serverUrl)"
  99. // NO account found, clear
  100. if NCManageDatabase.sharedInstance.getAccounts() == nil { NCUtility.sharedInstance.removeAllSettings() }
  101. // STOP Intro
  102. CCUtility.setIntro(true)
  103. // Add new account
  104. NCManageDatabase.sharedInstance.deleteAccount(account)
  105. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: token)
  106. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  107. self.dismiss(animated: true, completion: nil)
  108. return
  109. }
  110. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: token)
  111. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil, userInfo: nil)
  112. self.dismiss(animated: true) {
  113. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissCCLogin"), object: nil, userInfo: nil)
  114. }
  115. }
  116. }
  117. }
  118. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  119. if let serverTrust = challenge.protectionSpace.serverTrust {
  120. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  121. } else {
  122. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  123. }
  124. }
  125. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  126. decisionHandler(.allow)
  127. /* TEST NOT GOOD DON'T WORKS
  128. guard let url = navigationAction.request.url else {
  129. decisionHandler(.allow)
  130. return
  131. }
  132. if String(describing: url).hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) {
  133. decisionHandler(.allow)
  134. return
  135. } else if navigationAction.request.httpMethod != "GET" || navigationAction.request.value(forHTTPHeaderField: "OCS-APIRequest") != nil {
  136. decisionHandler(.allow)
  137. return
  138. }
  139. decisionHandler(.cancel)
  140. let language = NSLocale.preferredLanguages[0] as String
  141. var request = URLRequest(url: url)
  142. request.setValue(CCUtility.getUserAgent(), forHTTPHeaderField: "User-Agent")
  143. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  144. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  145. webView.load(request)
  146. */
  147. }
  148. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  149. print("didStartProvisionalNavigation");
  150. }
  151. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  152. print("didFinishProvisionalNavigation");
  153. }
  154. }