NCManageE2EE.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, 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. @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
  102. var body: some View {
  103. VStack {
  104. VStack {
  105. 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 ")
  106. .frame(height: 100)
  107. .padding()
  108. Button(action: {}) {
  109. HStack{
  110. Image(systemName: "person.crop.circle.fill")
  111. Text("This is a button")
  112. .padding(.horizontal)
  113. }
  114. .padding()
  115. }
  116. .foregroundColor(Color.white)
  117. .background(Color.blue)
  118. .cornerRadius(.infinity)
  119. .frame(height: 100)
  120. if CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  121. Text("Activated")
  122. } else {
  123. Button(action: {
  124. //manageE2EE.endToEndInitialize.initEndToEndEncryption()
  125. }, label: {
  126. Text("Start E2EE")
  127. })
  128. }
  129. Button(action: {
  130. if CCUtility.getPasscode().isEmpty {
  131. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  132. } else {
  133. //manageE2EE.requestPasscodeType("removeLocallyEncryption")
  134. }
  135. }, label: {
  136. Text(NSLocalizedString("_e2e_settings_remove_", comment: ""))
  137. })
  138. #if DEBUG
  139. Button(action: {
  140. }, label: {
  141. Text("Delete Certificate")
  142. })
  143. Button(action: {
  144. NextcloudKit.shared.deleteE2EEPrivateKey { account, error in
  145. }
  146. }, label: {
  147. Text("Delete PrivateKey")
  148. })
  149. #endif
  150. }
  151. .background(Color.green)
  152. Spacer()
  153. }
  154. .background(Color.gray)
  155. .navigationTitle("Cifratura End-To-End")
  156. }
  157. }