NCManageE2EE.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // NCManageE2EE.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/11/22.
  6. // Copyright © 2022 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 SwiftUI
  24. import NextcloudKit
  25. import TOPasscodeViewController
  26. import LocalAuthentication
  27. @objc class NCManageE2EEInterface: NSObject {
  28. @objc func makeShipDetailsUI(account: String) -> UIViewController {
  29. let details = NCViewE2EE()
  30. return UIHostingController(rootView: details)
  31. }
  32. }
  33. class NCManageE2EE: NSObject, ObservableObject, NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate {
  34. let endToEndInitialize = NCEndToEndInitialize()
  35. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. private var passcodeType = ""
  37. @Published var isEndToEndEnabled: Bool = false
  38. override init() {
  39. super.init()
  40. endToEndInitialize.delegate = self
  41. isEndToEndEnabled = CCUtility.isEnd(toEndEnabled: appDelegate.account)
  42. }
  43. func endToEndInitializeSuccess() {
  44. isEndToEndEnabled = true
  45. }
  46. // MARK: - Passcode
  47. @objc func requestPasscodeType(_ passcodeType: String) {
  48. let laContext = LAContext()
  49. var error: NSError?
  50. let passcodeViewController = TOPasscodeViewController(passcodeType: .sixDigits, allowCancel: true)
  51. passcodeViewController.delegate = self
  52. passcodeViewController.keypadButtonShowLettering = false
  53. if CCUtility.getEnableTouchFaceID() && laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
  54. if error == nil {
  55. if laContext.biometryType == .faceID {
  56. passcodeViewController.biometryType = .faceID
  57. passcodeViewController.allowBiometricValidation = true
  58. } else if laContext.biometryType == .touchID {
  59. passcodeViewController.biometryType = .touchID
  60. }
  61. passcodeViewController.allowBiometricValidation = true
  62. passcodeViewController.automaticallyPromptForBiometricValidation = true
  63. }
  64. }
  65. self.passcodeType = passcodeType
  66. appDelegate.window?.rootViewController?.present(passcodeViewController, animated: true)
  67. }
  68. @objc func correctPasscode() {
  69. if self.passcodeType == "removeLocallyEncryption" {
  70. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_remove_", comment: ""), message: NSLocalizedString("_e2e_settings_remove_message_", comment: ""), preferredStyle: .alert)
  71. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_", comment: ""), style: .default, handler: { action in
  72. CCUtility.clearAllKeysEnd(toEnd: self.appDelegate.account)
  73. }))
  74. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default, handler: { action in }))
  75. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  76. }
  77. }
  78. func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {
  79. if code == CCUtility.getPasscode() {
  80. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  81. self.correctPasscode()
  82. }
  83. return true
  84. } else {
  85. return false
  86. }
  87. }
  88. func didPerformBiometricValidationRequest(in passcodeViewController: TOPasscodeViewController) {
  89. LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: NCBrandOptions.shared.brand) { (success, error) in
  90. if success {
  91. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  92. passcodeViewController.dismiss(animated: true)
  93. self.correctPasscode()
  94. }
  95. }
  96. }
  97. }
  98. func didTapCancel(in passcodeViewController: TOPasscodeViewController) {
  99. passcodeViewController.dismiss(animated: true)
  100. }
  101. }
  102. struct NCViewE2EE: View {
  103. @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
  104. @ObservedObject var manageE2EE = NCManageE2EE()
  105. var body: some View {
  106. VStack {
  107. VStack {
  108. Text("Hello, world! 1 come stai spero bene ma secondo te quanto è lunga questa cosa, Hello, world! 1 come stai spero bene ma secondo te quanto è lunga questa cosa, versione 2 perchè la versione 1 e poi altro testo ")
  109. .frame(height: 100)
  110. .padding()
  111. Button(action: {}) {
  112. HStack{
  113. Image(systemName: "person.crop.circle.fill")
  114. Text("This is a button")
  115. .padding(.horizontal)
  116. }
  117. .padding()
  118. }
  119. .foregroundColor(Color.white)
  120. .background(Color.blue)
  121. .cornerRadius(.infinity)
  122. .frame(height: 100)
  123. if manageE2EE.isEndToEndEnabled {
  124. Text("Activated")
  125. } else {
  126. Button(action: {
  127. manageE2EE.endToEndInitialize.initEndToEndEncryption()
  128. }, label: {
  129. Text("Start E2EE")
  130. })
  131. }
  132. Button(action: {
  133. if CCUtility.getPasscode().isEmpty {
  134. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  135. } else {
  136. manageE2EE.requestPasscodeType("removeLocallyEncryption")
  137. }
  138. }, label: {
  139. Text(NSLocalizedString("_e2e_settings_remove_", comment: ""))
  140. })
  141. #if DEBUG
  142. Button(action: {
  143. }, label: {
  144. Text("Delete Certificate")
  145. })
  146. Button(action: {
  147. NextcloudKit.shared.deleteE2EEPrivateKey { account, error in
  148. }
  149. }, label: {
  150. Text("Delete PrivateKey")
  151. })
  152. #endif
  153. }
  154. .background(Color.green)
  155. Spacer()
  156. }
  157. .background(Color.gray)
  158. .navigationTitle("Cifratura End-To-End")
  159. }
  160. }