NCLoginWeb.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. @objc protocol NCLoginDelegateWeb: class {
  25. func loginSuccess(_: NSInteger)
  26. @objc optional func webDismiss()
  27. }
  28. class NCLoginWeb: UIViewController {
  29. var webView: WKWebView?
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. @objc var urlBase = ""
  32. @objc var loginType: Int = 0
  33. @objc weak var delegate: CCLoginDelegateWeb?
  34. @IBOutlet weak var buttonExit: UIButton!
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. webView = WKWebView(frame: CGRect.zero)
  38. webView!.navigationDelegate = self
  39. view.addSubview(webView!)
  40. webView!.translatesAutoresizingMaskIntoConstraints = false
  41. webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  42. webView!.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  43. webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  44. webView!.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  45. // ADD k_flowEndpoint for Web Flow
  46. if NCBrandOptions.sharedInstance.use_login_web_personalized == false && urlBase != NCBrandOptions.sharedInstance.linkloginPreferredProviders {
  47. urlBase = urlBase + k_flowEndpoint
  48. }
  49. // button exit
  50. buttonExit.layer.zPosition = .greatestFiniteMagnitude
  51. if loginType == k_login_Add_Forced {
  52. buttonExit.isHidden = true
  53. }
  54. loadWebPage(webView: webView!, url: URL(string: urlBase)!)
  55. }
  56. func loadWebPage(webView: WKWebView, url: URL) {
  57. let language = NSLocale.preferredLanguages[0] as String
  58. var request = URLRequest(url: url)
  59. request.setValue(CCUtility.getUserAgent(), forHTTPHeaderField: "User-Agent")
  60. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  61. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  62. webView.load(request)
  63. }
  64. @IBAction func touchUpInsideButtonExit(_ sender: UIButton) {
  65. self.dismiss(animated: true) {
  66. self.delegate?.webDismiss?()
  67. }
  68. }
  69. }
  70. extension NCLoginWeb: WKNavigationDelegate {
  71. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  72. guard let url = webView.url else { return }
  73. let urlString: String = url.absoluteString.lowercased()
  74. if (urlString.hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true) {
  75. let keyValue = url.path.components(separatedBy: "&")
  76. if (keyValue.count >= 3) {
  77. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  78. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  79. // Login Flow NC 12
  80. if (NCBrandOptions.sharedInstance.use_login_web_personalized == false && serverUrl.hasPrefix("http://") == false && serverUrl.hasPrefix("https://") == false) {
  81. serverUrl = urlBase
  82. }
  83. if (serverUrl.last == "/") {
  84. serverUrl = String(serverUrl.dropLast())
  85. }
  86. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ")
  87. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  88. let account : String = "\(username) \(serverUrl)"
  89. // Login Flow
  90. if (loginType == k_login_Modify_Password && NCBrandOptions.sharedInstance.use_login_web_personalized == false) {
  91. // Verify if change the active account
  92. guard let activeAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  93. self.dismiss(animated: true, completion: nil)
  94. return
  95. }
  96. if (activeAccount.account != account) {
  97. self.dismiss(animated: true, completion: nil)
  98. return
  99. }
  100. // Change Password & setting active account
  101. CCUtility.setPassword(account, password: password)
  102. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: appDelegate.activeUserID, activePassword: password)
  103. self.dismiss(animated: true) {
  104. self.delegate?.loginSuccess(NSInteger(self.loginType))
  105. self.delegate?.webDismiss?()
  106. }
  107. }
  108. if (loginType == k_login_Add || loginType == k_login_Add_Forced) {
  109. // NO account found, clear
  110. if NCManageDatabase.sharedInstance.getAccounts() == nil {
  111. NCUtility.sharedInstance.removeAllSettings()
  112. }
  113. // STOP Intro
  114. CCUtility.setIntro(true)
  115. // Add new account
  116. NCManageDatabase.sharedInstance.deleteAccount(account)
  117. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password, loginFlow: true)
  118. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  119. self.dismiss(animated: true, completion: nil)
  120. return
  121. }
  122. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  123. self.dismiss(animated: true) {
  124. self.delegate?.loginSuccess(NSInteger(self.loginType))
  125. self.delegate?.webDismiss?()
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  133. if let serverTrust = challenge.protectionSpace.serverTrust {
  134. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  135. } else {
  136. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  137. }
  138. }
  139. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  140. guard let url = navigationAction.request.url else {
  141. decisionHandler(.allow)
  142. return
  143. }
  144. if String(describing: url).hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) {
  145. decisionHandler(.allow)
  146. return
  147. } else if navigationAction.request.httpMethod != "GET" || navigationAction.request.value(forHTTPHeaderField: "OCS-APIRequest") != nil {
  148. decisionHandler(.allow)
  149. return
  150. }
  151. decisionHandler(.cancel)
  152. let language = NSLocale.preferredLanguages[0] as String
  153. var request = URLRequest(url: url)
  154. request.setValue(CCUtility.getUserAgent(), forHTTPHeaderField: "User-Agent")
  155. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  156. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  157. webView.load(request)
  158. }
  159. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  160. print("didStartProvisionalNavigation");
  161. }
  162. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  163. print("didFinishProvisionalNavigation");
  164. }
  165. }