NCNetworkingE2EE.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NCNetworkingE2EE.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/05/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import UIKit
  22. import OpenSSL
  23. import NextcloudKit
  24. import CFNetwork
  25. import Alamofire
  26. class NCNetworkingE2EE: NSObject {
  27. public static let shared: NCNetworkingE2EE = {
  28. let instance = NCNetworkingE2EE()
  29. return instance
  30. }()
  31. func isE2EEVersionWriteable(account: String) -> NKError? {
  32. if NCGlobal.shared.e2eeReadVersions.last == NCGlobal.shared.capabilityE2EEApiVersion {
  33. return nil
  34. }
  35. return NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_e2e_error_not_versionwriteable_")
  36. }
  37. func generateRandomIdentifier() -> String {
  38. var UUID = NSUUID().uuidString
  39. UUID = "E2EE" + UUID.replacingOccurrences(of: "-", with: "")
  40. return UUID
  41. }
  42. func lock(account: String, serverUrl: String) async -> (fileId: String?, e2eToken: String?, error: NKError) {
  43. var e2eToken: String?
  44. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  45. return (nil, nil, NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_e2e_error_lock_"))
  46. }
  47. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) {
  48. e2eToken = tableLock.e2eToken
  49. }
  50. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, method: "POST")
  51. if lockE2EEFolderResults.error == .success, let e2eToken = lockE2EEFolderResults.e2eToken {
  52. NCManageDatabase.shared.setE2ETokenLock(account: account, serverUrl: serverUrl, fileId: directory.fileId, e2eToken: e2eToken)
  53. }
  54. return (directory.fileId, lockE2EEFolderResults.e2eToken, lockE2EEFolderResults.error)
  55. }
  56. func unlock(account: String, serverUrl: String) async -> () {
  57. guard let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) else {
  58. return
  59. }
  60. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, method: "DELETE")
  61. if lockE2EEFolderResults.error == .success {
  62. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: serverUrl)
  63. }
  64. return
  65. }
  66. func unlockAll(account: String) {
  67. guard CCUtility.isEnd(toEndEnabled: account) else { return }
  68. Task {
  69. for result in NCManageDatabase.shared.getE2EAllTokenLock(account: account) {
  70. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: result.fileId, e2eToken: result.e2eToken, method: "DELETE")
  71. if lockE2EEFolderResults.error == .success {
  72. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: result.serverUrl)
  73. }
  74. }
  75. }
  76. }
  77. }