CCLoginWeb.swift 6.2 KB

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