NCNetworkingE2EE.swift 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. let versionE2EE = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion) ?? ""
  33. if NCGlobal.shared.e2eeReadVersions.last == versionE2EE {
  34. return nil
  35. }
  36. return NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_e2e_error_not_versionwriteable_")
  37. }
  38. func generateRandomIdentifier() -> String {
  39. var UUID = NSUUID().uuidString
  40. UUID = "E2EE" + UUID.replacingOccurrences(of: "-", with: "")
  41. return UUID
  42. }
  43. func lock(account: String, serverUrl: String) async -> (fileId: String?, e2eToken: String?, error: NKError) {
  44. var e2eToken: String?
  45. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  46. return (nil, nil, NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_e2e_error_lock_"))
  47. }
  48. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) {
  49. e2eToken = tableLock.e2eToken
  50. }
  51. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, method: "POST")
  52. if lockE2EEFolderResults.error == .success, let e2eToken = lockE2EEFolderResults.e2eToken {
  53. NCManageDatabase.shared.setE2ETokenLock(account: account, serverUrl: serverUrl, fileId: directory.fileId, e2eToken: e2eToken)
  54. }
  55. return (directory.fileId, lockE2EEFolderResults.e2eToken, lockE2EEFolderResults.error)
  56. }
  57. func unlock(account: String, serverUrl: String) async -> () {
  58. guard let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) else {
  59. return
  60. }
  61. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, method: "DELETE")
  62. if lockE2EEFolderResults.error == .success {
  63. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: serverUrl)
  64. }
  65. return
  66. }
  67. func unlockAll(account: String) {
  68. guard CCUtility.isEnd(toEndEnabled: account) else { return }
  69. Task {
  70. for result in NCManageDatabase.shared.getE2EAllTokenLock(account: account) {
  71. let lockE2EEFolderResults = await NextcloudKit.shared.lockE2EEFolder(fileId: result.fileId, e2eToken: result.e2eToken, method: "DELETE")
  72. if lockE2EEFolderResults.error == .success {
  73. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: result.serverUrl)
  74. }
  75. }
  76. }
  77. }
  78. }