SafeApiService.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  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 Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc class SafeApiService: NSObject {
  22. enum SafeApiError: Error {
  23. case invalidServerUrl
  24. case requestFailed(message: String)
  25. case uploadTempFileCouldNotBeSaved(error: NSError)
  26. }
  27. func testServer(server: URL, user: String?, password: String?) -> (serverConfig: Data?, errorMessage: String?) {
  28. var serverConfig: Data? = nil
  29. var errorMessage: String? = nil
  30. //validate and test safe server
  31. let queue = DispatchQueue.global()
  32. let semaphore = DispatchSemaphore(value: 0)
  33. queue.async {
  34. let client = HttpClient(user: user, password: password)
  35. client.downloadData(url: server.appendingPathComponent("config"), contentType: .json) { (data, response, error) in
  36. let result = self.processResponse(data: data, response: response, error: error)
  37. if let responseData = result.data {
  38. serverConfig = responseData
  39. }
  40. if let responseErrorMessage = result.errorMessage {
  41. errorMessage = responseErrorMessage
  42. }
  43. semaphore.signal()
  44. }
  45. }
  46. semaphore.wait()
  47. return (serverConfig, errorMessage)
  48. }
  49. func delete(server: URL, user: String?, password: String?) -> String? {
  50. var errorMessage: String?
  51. let dispatch = DispatchGroup()
  52. dispatch.enter()
  53. DispatchQueue.global(qos: .background).async {
  54. let client = HttpClient(user: user, password: password)
  55. client.delete(url: server) { (data, response, error) in
  56. let result = self.processResponse(data: data, response: response, error: error)
  57. if let responseErrorMessage = result.errorMessage {
  58. errorMessage = responseErrorMessage
  59. }
  60. dispatch.leave()
  61. }
  62. }
  63. dispatch.wait()
  64. return errorMessage
  65. }
  66. func upload(backup: URL, user: String?, password: String?, encryptedData: [UInt8], completionHandler: @escaping (Data?, String?) -> Void) {
  67. let client = HttpClient(user: user, password: password)
  68. client.uploadData(url: backup, data: Data(encryptedData)) { (data, response, error) in
  69. let result = self.processResponse(data: data, response: response, error: error)
  70. completionHandler(result.data, result.errorMessage)
  71. }
  72. }
  73. func download(backup: URL, user: String?, password: String?) throws -> Data? {
  74. var errorMessage: String? = nil
  75. var encryptedData: Data? = nil
  76. let dispatch = DispatchGroup()
  77. dispatch.enter()
  78. DispatchQueue.global(qos: .background).async {
  79. let client = HttpClient(user: user, password: password)
  80. client.downloadData(url: backup, contentType: .octetStream) { (data, response, error) in
  81. let result = self.processResponse(data: data, response: response, error: error)
  82. if let responseData = result.data {
  83. encryptedData = responseData
  84. }
  85. if let responseErrorMessage = result.errorMessage {
  86. errorMessage = responseErrorMessage
  87. }
  88. dispatch.leave()
  89. }
  90. }
  91. dispatch.wait()
  92. if let message = errorMessage {
  93. throw SafeApiError.requestFailed(message: message)
  94. }
  95. return encryptedData
  96. }
  97. private func processResponse(data: Data?, response: URLResponse?, error: Error?) -> (data: Data?, errorMessage: String?) {
  98. var errorMessage: String?
  99. if let error = error {
  100. errorMessage = error.localizedDescription
  101. return (nil, errorMessage)
  102. }
  103. guard let response = response as? HTTPURLResponse else {
  104. errorMessage = "response is missing"
  105. if let data = data {
  106. print("\(String(describing: String(data: data, encoding: .utf8)))")
  107. }
  108. return(nil, errorMessage)
  109. }
  110. if !(200...299).contains(response.statusCode) {
  111. errorMessage = "response code \(response.statusCode)"
  112. if let data = data {
  113. print("\(String(describing: String(data: data, encoding: .utf8)))")
  114. }
  115. return(nil, errorMessage)
  116. }
  117. guard let data = data else {
  118. errorMessage = "response data/config is missing"
  119. return (nil, errorMessage)
  120. }
  121. return (data, nil)
  122. }
  123. }