NCLogin.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // NCLogin.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/02/21.
  6. // Copyright © 2021 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. import NCCommunication
  25. class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {
  26. @IBOutlet weak var imageBrand: UIImageView!
  27. @IBOutlet weak var baseUrl: UITextField!
  28. @IBOutlet weak var user: UITextField!
  29. @IBOutlet weak var password: UITextField!
  30. @IBOutlet weak var imageBaseUrl: UIImageView!
  31. @IBOutlet weak var imageUser: UIImageView!
  32. @IBOutlet weak var imagePassword: UIImageView!
  33. @IBOutlet weak var activity: UIActivityIndicatorView!
  34. @IBOutlet weak var login: UIButton!
  35. @IBOutlet weak var toggleVisiblePassword: UIButton!
  36. @IBOutlet weak var loginTypeView: UIButton!
  37. @IBOutlet weak var qrCode: UIButton!
  38. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  39. var textColor: UIColor = .white
  40. var textColorOpponent: UIColor = .black
  41. var cancelButton: UIBarButtonItem?
  42. // MARK: - Life Cycle
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. view.backgroundColor = NCBrandColor.shared.customer
  46. // Text color
  47. if NCBrandColor.shared.customer.isTooLight() {
  48. textColor = .black
  49. textColorOpponent = .white
  50. } else if NCBrandColor.shared.customer.isTooDark() {
  51. textColor = .white
  52. textColorOpponent = .black
  53. } else {
  54. textColor = .white
  55. textColorOpponent = .black
  56. }
  57. // Image Brand
  58. imageBrand.image = UIImage(named: "logo")
  59. // Cancel Button
  60. cancelButton = UIBarButtonItem.init(barButtonSystemItem: .stop, target: self, action: #selector(self.actionCancel))
  61. cancelButton?.tintColor = textColor
  62. // Url
  63. imageBaseUrl.image = UIImage(named: "loginURL")?.image(color: textColor, size: 50)
  64. baseUrl.textColor = textColor
  65. baseUrl.tintColor = textColor
  66. baseUrl.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("_login_url_", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: textColor.withAlphaComponent(0.5)])
  67. baseUrl.delegate = self
  68. // User
  69. imageUser.image = UIImage(named: "loginUser")?.image(color: textColor, size: 50)
  70. user.textColor = textColor
  71. user.tintColor = textColor
  72. user.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("_username_", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: textColor.withAlphaComponent(0.5)])
  73. user.delegate = self
  74. // password
  75. imagePassword.image = UIImage(named: "loginPassword")?.image(color: textColor, size: 50)
  76. password.textColor = textColor
  77. password.tintColor = textColor
  78. password.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("_password_", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: textColor.withAlphaComponent(0.5)])
  79. password.delegate = self
  80. // toggle visible password
  81. toggleVisiblePassword.setImage(UIImage(named: "visiblePassword")?.image(color: textColor, size: 50), for: .normal)
  82. // login
  83. login.setTitle(NSLocalizedString("_login_", comment: ""), for: .normal)
  84. login.backgroundColor = textColor
  85. login.tintColor = textColor
  86. login.layer.cornerRadius = 20
  87. login.clipsToBounds = true
  88. // type of login
  89. loginTypeView.setTitle(NSLocalizedString("_traditional_login_", comment: ""), for: .normal)
  90. loginTypeView.setTitleColor(textColor.withAlphaComponent(0.5), for: .normal)
  91. // brand
  92. if NCBrandOptions.shared.disable_request_login_url {
  93. baseUrl.text = NCBrandOptions.shared.loginBaseUrl
  94. imageBaseUrl.isHidden = true
  95. baseUrl.isHidden = true
  96. }
  97. // qrcode
  98. qrCode.setImage(UIImage(named: "qrcode")?.image(color: textColor, size: 100), for: .normal)
  99. if NCManageDatabase.shared.getAccounts()?.count ?? 0 == 0 {
  100. imageUser.isHidden = true
  101. user.isHidden = true
  102. imagePassword.isHidden = true
  103. password.isHidden = true
  104. } else {
  105. imageUser.isHidden = true
  106. user.isHidden = true
  107. imagePassword.isHidden = true
  108. password.isHidden = true
  109. navigationItem.leftBarButtonItem = cancelButton
  110. }
  111. }
  112. override func viewDidAppear(_ animated: Bool) {
  113. super.viewDidAppear(animated)
  114. appDelegate.timerErrorNetworking?.invalidate()
  115. }
  116. override func viewDidDisappear(_ animated: Bool) {
  117. super.viewDidDisappear(animated)
  118. appDelegate.startTimerErrorNetworking()
  119. }
  120. // MARK: - TextField
  121. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  122. textField.resignFirstResponder()
  123. return false
  124. }
  125. func textFieldDidBeginEditing(_ textField: UITextField) {
  126. if textField == password {
  127. toggleVisiblePassword.isHidden = false
  128. }
  129. }
  130. func textFieldDidEndEditing(_ textField: UITextField) {
  131. if textField == password {
  132. toggleVisiblePassword.isHidden = true
  133. }
  134. }
  135. // MARK: - Action
  136. @objc func actionCancel() {
  137. dismiss(animated: true) { }
  138. }
  139. // MARK: - Login
  140. func isUrlValid() {
  141. // Check whether baseUrl contain protocol. If not add https:// by default.
  142. if (baseUrl.text?.hasPrefix("https") ?? false) == false && (baseUrl.text?.hasPrefix("http") ?? false) == false {
  143. self.baseUrl.text = "https://" + (self.baseUrl.text ?? "")
  144. }
  145. guard var url = baseUrl.text else { return }
  146. login.isEnabled = false
  147. activity.startAnimating()
  148. if url.hasSuffix("/") {
  149. url = String(url.dropLast())
  150. }
  151. NCCommunication.shared.getServerStatus(serverUrl: url) { (serverProductName, serverVersion, versionMajor, versionMinor, versionMicro, extendedSupport, errorCode ,errorDescription) in
  152. if errorCode == 0 {
  153. NCCommunication.shared.getLoginFlowV2(serverUrl: url) { (token, endpoint, login, errorCode, errorDescription) in
  154. self.login.isEnabled = true
  155. self.activity.stopAnimating()
  156. if errorCode == 0 && NCBrandOptions.shared.use_loginflowv2 && token != nil && endpoint != nil && login != nil {
  157. if let loginWeb = UIStoryboard(name: "NCLogin", bundle: nil).instantiateViewController(withIdentifier: "NCLoginWeb") as? NCLoginWeb {
  158. loginWeb.urlBase = url
  159. loginWeb.loginFlowV2Available = true
  160. loginWeb.loginFlowV2Token = token!
  161. loginWeb.loginFlowV2Endpoint = endpoint!
  162. loginWeb.loginFlowV2Login = login!
  163. self.navigationController?.pushViewController(loginWeb, animated: true)
  164. }
  165. } else if self.user.isHidden && self.password.isHidden && versionMajor >= NCGlobal.shared.nextcloudVersion12 {
  166. if let loginWeb = UIStoryboard(name: "NCLogin", bundle: nil).instantiateViewController(withIdentifier: "NCLoginWeb") as? NCLoginWeb {
  167. loginWeb.urlBase = url
  168. self.navigationController?.pushViewController(loginWeb, animated: true)
  169. }
  170. } else if versionMajor < NCGlobal.shared.nextcloudVersion12 {
  171. self.loginTypeView.isHidden = true
  172. self.imageUser.isHidden = false
  173. self.user.isHidden = false
  174. self.user.becomeFirstResponder()
  175. self.imagePassword.isHidden = false
  176. self.password.isHidden = false
  177. }
  178. }
  179. } else {
  180. self.login.isEnabled = true
  181. self.activity.stopAnimating()
  182. if errorCode == NSURLErrorServerCertificateUntrusted {
  183. let alertController = UIAlertController(title: NSLocalizedString("_ssl_certificate_untrusted_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert)
  184. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { action in
  185. NCNetworking.shared.writeCertificate(directoryCertificate: CCUtility.getDirectoryCerificates())
  186. self.appDelegate.startTimerErrorNetworking()
  187. }))
  188. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { action in
  189. self.appDelegate.startTimerErrorNetworking()
  190. }))
  191. self.present(alertController, animated: true, completion: {
  192. self.appDelegate.timerErrorNetworking?.invalidate()
  193. })
  194. } else {
  195. let alertController = UIAlertController(title: NSLocalizedString("_connection_error_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert)
  196. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in }))
  197. self.present(alertController, animated: true, completion: { })
  198. }
  199. }
  200. }
  201. }
  202. // MARK: - QRCode
  203. func dismissQRCode(_ value: String?, metadataType: String?) {
  204. guard var value = value else { return }
  205. let protocolLogin = NCBrandOptions.shared.webLoginAutenticationProtocol + "login/"
  206. if value.hasPrefix("protocolLogin") && value.contains("user:") && value.contains("password:") && value.contains("server:") {
  207. value = value.replacingOccurrences(of: protocolLogin, with: "")
  208. let valueArray = value.components(separatedBy: "&")
  209. if valueArray.count == 3 {
  210. user.text = valueArray[0].replacingOccurrences(of: "user:", with: "")
  211. password.text = valueArray[1].replacingOccurrences(of: "password:", with: "")
  212. baseUrl.text = valueArray[2].replacingOccurrences(of: "server:", with: "")
  213. // Check whether baseUrl contain protocol. If not add https:// by default.
  214. if (baseUrl.text?.hasPrefix("https") ?? false) == false && (baseUrl.text?.hasPrefix("http") ?? false) == false {
  215. self.baseUrl.text = "https://" + (self.baseUrl.text ?? "")
  216. }
  217. login.isEnabled = false
  218. activity.startAnimating()
  219. let webDAV = NCUtilityFileSystem.shared.getWebDAV(account: appDelegate.account)
  220. let serverUrl = (baseUrl.text ?? "") + "/" + webDAV
  221. NCCommunication.shared.checkServer(serverUrl: serverUrl) { (errorCode, errorDescription) in
  222. self.activity.stopAnimating()
  223. self.login.isEnabled = true
  224. // [self afterLoginWithUrl:url user:user token:token errorCode:errorCode message:errorDescription];
  225. }
  226. }
  227. }
  228. }
  229. }