NCManageE2EE.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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
  28. class NCManageE2EEInterface: NSObject, NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate {
  29. let endToEndInitialize = NCEndToEndInitialize()
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var passcodeType = ""
  32. override init() {
  33. super.init()
  34. endToEndInitialize.delegate = self
  35. }
  36. @objc func makeShipDetailsUI() -> UIViewController {
  37. let details = NCManageE2EE()
  38. return UIHostingController(rootView: details)
  39. }
  40. func endToEndInitializeSuccess() {
  41. }
  42. // MARK: - Passcode
  43. func requestPasscodeType(_ passcodeType: String) {
  44. let laContext = LAContext()
  45. var error: NSError?
  46. let passcodeViewController = TOPasscodeViewController(passcodeType: .sixDigits, allowCancel: true)
  47. passcodeViewController.delegate = self
  48. passcodeViewController.keypadButtonShowLettering = false
  49. if CCUtility.getEnableTouchFaceID() && laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
  50. if error == nil {
  51. if laContext.biometryType == .faceID {
  52. passcodeViewController.biometryType = .faceID
  53. passcodeViewController.allowBiometricValidation = true
  54. } else if laContext.biometryType == .touchID {
  55. passcodeViewController.biometryType = .touchID
  56. }
  57. passcodeViewController.allowBiometricValidation = true
  58. passcodeViewController.automaticallyPromptForBiometricValidation = true
  59. }
  60. }
  61. self.passcodeType = passcodeType
  62. appDelegate.window?.rootViewController?.present(passcodeViewController, animated: true)
  63. }
  64. func correctPasscode() {
  65. if self.passcodeType == "removeLocallyEncryption" {
  66. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_remove_", comment: ""), message: NSLocalizedString("_e2e_settings_remove_message_", comment: ""), preferredStyle: .alert)
  67. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_", comment: ""), style: .default, handler: { action in
  68. CCUtility.clearAllKeysEnd(toEnd: self.appDelegate.account)
  69. }))
  70. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default, handler: { action in }))
  71. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  72. }
  73. }
  74. func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {
  75. if code == CCUtility.getPasscode() {
  76. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  77. self.correctPasscode()
  78. }
  79. return true
  80. } else {
  81. return false
  82. }
  83. }
  84. func didPerformBiometricValidationRequest(in passcodeViewController: TOPasscodeViewController) {
  85. LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: NCBrandOptions.shared.brand) { (success, error) in
  86. if success {
  87. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  88. passcodeViewController.dismiss(animated: true)
  89. self.correctPasscode()
  90. }
  91. }
  92. }
  93. }
  94. func didTapCancel(in passcodeViewController: TOPasscodeViewController) {
  95. passcodeViewController.dismiss(animated: true)
  96. }
  97. }
  98. struct NCManageE2EE: View {
  99. let manageE2EEInterface = NCManageE2EEInterface()
  100. var body: some View {
  101. VStack {
  102. Text("Hello, world! 1")
  103. Button(action: {
  104. manageE2EEInterface.endToEndInitialize.initEndToEndEncryption()
  105. }, label: {
  106. Text("Start")
  107. })
  108. Button(action: {
  109. if CCUtility.getPasscode().isEmpty {
  110. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  111. } else {
  112. manageE2EEInterface.requestPasscodeType("removeLocallyEncryption")
  113. }
  114. }, label: {
  115. Text(NSLocalizedString("_e2e_settings_remove_", comment: ""))
  116. })
  117. #if DEBUG
  118. Button(action: {
  119. }, label: {
  120. Text("Delete Certificate")
  121. })
  122. Button(action: {
  123. NextcloudKit.shared.deleteE2EEPrivateKey { account, error in
  124. }
  125. }, label: {
  126. Text("Delete PrivateKey")
  127. })
  128. #endif
  129. }
  130. .navigationTitle("Cifratura End-To-End")
  131. }
  132. }
  133. struct NCManageE2EE_Previews: PreviewProvider {
  134. static var previews: some View {
  135. NCManageE2EE()
  136. }
  137. }