NCManageE2EE.swift 6.9 KB

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