NCEndToEndMetadata.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // NCEndToEndMetadata.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/17.
  6. // Copyright © 2017 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 UIKit
  24. import NextcloudKit
  25. class NCEndToEndMetadata: NSObject {
  26. // --------------------------------------------------------------------------------------------
  27. // MARK: Encode JSON Metadata Bridge
  28. // --------------------------------------------------------------------------------------------
  29. func encoderMetadata(account: String, serverUrl: String, userId: String, addUserId: String? = nil, addCertificate: String? = nil, removeUserId: String? = nil) -> (metadata: String?, signature: String?) {
  30. let e2EEApiVersion = NCGlobal.shared.capabilityE2EEApiVersion
  31. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  32. return (nil, nil)
  33. }
  34. switch e2EEApiVersion {
  35. case NCGlobal.shared.e2eeVersionV12:
  36. return encoderMetadataV12(account: account, serverUrl: serverUrl, ocIdServerUrl: directory.ocId)
  37. case NCGlobal.shared.e2eeVersionV20:
  38. return encoderMetadataV20(account: account, serverUrl: serverUrl, ocIdServerUrl: directory.ocId, userId: userId, addUserId: addUserId, addCertificate: addCertificate, removeUserId: removeUserId)
  39. default:
  40. return (nil, nil)
  41. }
  42. }
  43. // --------------------------------------------------------------------------------------------
  44. // MARK: Decode JSON Metadata Bridge
  45. // --------------------------------------------------------------------------------------------
  46. func decoderMetadata(_ json: String, signature: String?, serverUrl: String, account: String, urlBase: String, userId: String, ownerId: String?) -> NKError {
  47. guard let data = json.data(using: .utf8), let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  48. return (NKError(errorCode: NCGlobal.shared.errorE2EE, errorDescription: "Error decoding E2EE metadata"))
  49. }
  50. data.printJson()
  51. let decoder = JSONDecoder()
  52. if (try? decoder.decode(E2eeV1.self, from: data)) != nil {
  53. return decoderMetadataV1(json, serverUrl: serverUrl, account: account, ocIdServerUrl: directory.ocId, urlBase: urlBase, userId: userId)
  54. } else if (try? decoder.decode(E2eeV12.self, from: data)) != nil {
  55. return decoderMetadataV12(json, serverUrl: serverUrl, account: account, ocIdServerUrl: directory.ocId, urlBase: urlBase, userId: userId, ownerId: ownerId)
  56. } else if (try? decoder.decode(E2eeV20.self, from: data)) != nil {
  57. return decoderMetadataV20(json, signature: signature, serverUrl: serverUrl, account: account, ocIdServerUrl: directory.ocId, urlBase: urlBase, userId: userId, ownerId: ownerId)
  58. } else {
  59. return NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Server E2EE version " + NCGlobal.shared.capabilityE2EEApiVersion + ", not compatible")
  60. }
  61. }
  62. }