CCLoginWeb.swift 6.9 KB

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