NCNetworkingE2EE.swift 3.4 KB

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