CCLoginWeb.swift 6.6 KB

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