NCLoginWeb.swift 9.4 KB

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