CCLoginWeb.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // CCLoginWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 UIKit
  24. @objc protocol CCLoginDelegateWeb: class {
  25. func loginSuccess(_: NSInteger)
  26. func loginWebClose()
  27. }
  28. public class CCLoginWeb: UIViewController {
  29. /*
  30. @objc enum enumLoginTypeWeb : NSInteger {
  31. case loginAdd = 0
  32. case loginAddForced = 1
  33. case loginModifyPasswordUser = 2
  34. }
  35. */
  36. @objc weak var delegate: CCLoginDelegateWeb?
  37. @objc var loginType = loginAdd
  38. @objc var urlBase = ""
  39. var viewController : UIViewController?
  40. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  41. var doneButtonVisible: Bool = false
  42. @objc func presentModalWithDefaultTheme(_ vc: UIViewController) {
  43. var urlString = urlBase
  44. self.viewController = vc
  45. if (loginType == loginAdd || loginType == loginModifyPasswordUser) {
  46. doneButtonVisible = true
  47. }
  48. if (NCBrandOptions.sharedInstance.use_login_web_personalized == false) {
  49. urlString = urlBase+flowEndpoint
  50. }
  51. let webVC = SwiftModalWebVC(urlString: urlString, theme: .custom, color: NCBrandColor.sharedInstance.customer, colorText: NCBrandColor.sharedInstance.customerText, doneButtonVisible: doneButtonVisible, hideToolbar: true)
  52. webVC.delegateWeb = self
  53. vc.present(webVC, animated: false, completion: nil)
  54. }
  55. }
  56. extension CCLoginWeb: SwiftModalWebVCDelegate {
  57. public func didStartLoading() {
  58. print("Started loading.")
  59. }
  60. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  61. let urlString: String = url.absoluteString.lowercased()
  62. if (urlString.hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true) {
  63. let keyValue = url.path.components(separatedBy: "&")
  64. if (keyValue.count == 3) {
  65. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  66. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  67. // Login Flow NC 12
  68. if (NCBrandOptions.sharedInstance.use_login_web_personalized == false && serverUrl.hasPrefix("http://") == false && serverUrl.hasPrefix("https://") == false) {
  69. serverUrl = urlBase
  70. }
  71. if (serverUrl.last == "/") {
  72. serverUrl = String(serverUrl.dropLast())
  73. }
  74. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ")
  75. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  76. let account : String = "\(username) \(serverUrl)"
  77. // Login Flow
  78. if (loginType == loginModifyPasswordUser && NCBrandOptions.sharedInstance.use_login_web_personalized == false) {
  79. // Verify if change the active account
  80. guard let activeAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  81. self.viewController?.dismiss(animated: true, completion: nil)
  82. return
  83. }
  84. if (activeAccount.account != account) {
  85. self.viewController?.dismiss(animated: true, completion: nil)
  86. return
  87. }
  88. // Change Password
  89. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountPassword(account, password: password) else {
  90. self.viewController?.dismiss(animated: true, completion: nil)
  91. return
  92. }
  93. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  94. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  95. self.delegate?.loginWebClose()
  96. self.viewController?.dismiss(animated: true, completion: nil)
  97. }
  98. if (loginType == loginAdd || loginType == loginAddForced) {
  99. // Add new account
  100. NCManageDatabase.sharedInstance.deleteAccount(account)
  101. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password, loginFlow: true)
  102. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  103. self.viewController?.dismiss(animated: true, completion: nil)
  104. return
  105. }
  106. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  107. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  108. self.delegate?.loginWebClose()
  109. self.viewController?.dismiss(animated: true, completion: nil)
  110. }
  111. }
  112. }
  113. }
  114. }
  115. public func didFinishLoading(success: Bool, url: URL) {
  116. print("Finished loading. Success: \(success).")
  117. }
  118. public func loginWebClose() {
  119. self.delegate?.loginWebClose()
  120. }
  121. public func decidePolicyForNavigationAction(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  122. decisionHandler(.allow)
  123. }
  124. }