From 2dc1a44087ca44db3af3f9c5139bdb0520829ff9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 3 Feb 2020 14:33:47 +1100 Subject: [PATCH] Clean --- .../src/Loki/API/LokiFileServerProxy.swift | 41 ++++--------------- .../src/Loki/API/LokiSnodeProxy.swift | 8 ++-- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift index 986c65e89..cd2f6f69e 100644 --- a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift +++ b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift @@ -24,7 +24,7 @@ internal class LokiFileServerProxy : LokiHTTPClient { case .symmetricKeyGenerationFailed: return "Couldn't generate symmetric key." case .endpointParsingFailed: return "Couldn't parse endpoint." case .proxyResponseParsingFailed: return "Couldn't parse proxy response." - case .fileServerHTTPError(let httpStatusCode, let message): return "File server returned error \(httpStatusCode) with description: \(message ?? "no description provided.")." + case .fileServerHTTPError(let httpStatusCode, let message): return "File server returned \(httpStatusCode) with description: \(message ?? "no description provided.")." } } } @@ -87,42 +87,17 @@ internal class LokiFileServerProxy : LokiHTTPClient { task.resume() return promise }.map { rawResponse in - guard let data = rawResponse as? Data, !data.isEmpty else { - print("[Loki] Received an empty response.") - return rawResponse - } - var uncheckedStatusCode: Int? = nil - let uncheckedCipherText: Data? - if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON, let base64EncodedData = json["data"] as? String { - if let meta = json["meta"] as? JSON { uncheckedStatusCode = meta["code"] as? Int } - uncheckedCipherText = Data(base64Encoded: base64EncodedData) - } else { - uncheckedCipherText = data - } - guard let cipherText = uncheckedCipherText else { - print("[Loki] Received an invalid response.") - throw Error.proxyResponseParsingFailed - } - let response = try DiffieHellman.decrypt(cipherText, using: symmetricKey) - let uncheckedJSON = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? JSON - guard let json = uncheckedJSON else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) } - if uncheckedStatusCode == nil { - uncheckedStatusCode = json["status"] as? Int - } - guard let statusCode = uncheckedStatusCode else { + guard let responseAsData = rawResponse as? Data, let responseAsJSON = try? JSONSerialization.jsonObject(with: responseAsData, options: .allowFragments) as? JSON, let base64EncodedCipherText = responseAsJSON["data"] as? String, + let meta = responseAsJSON["meta"] as? JSON, let statusCode = meta["code"] as? Int, let cipherText = Data(base64Encoded: base64EncodedCipherText) else { print("[Loki] Received an invalid response.") throw Error.proxyResponseParsingFailed } let isSuccess = (200..<300).contains(statusCode) - var body: Any? = nil - if let bodyAsString = json["body"] as? String { - body = bodyAsString - if let bodyAsJSON = try? JSONSerialization.jsonObject(with: bodyAsString.data(using: .utf8)!, options: .allowFragments) as? JSON { - body = bodyAsJSON - } - } - guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: body, underlyingError: Error.fileServerHTTPError(code: statusCode, message: body)) } - return body ?? response + guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: nil, underlyingError: Error.fileServerHTTPError(code: statusCode, message: nil)) } + let uncheckedJSONAsData = try DiffieHellman.decrypt(cipherText, using: symmetricKey) + let uncheckedJSON = try? JSONSerialization.jsonObject(with: uncheckedJSONAsData, options: .allowFragments) as? JSON + guard let json = uncheckedJSON else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) } + return json }.recover { error -> Promise in print("[Loki] File server proxy request failed with error: \(error.localizedDescription).") throw HTTPError.from(error: error) ?? error diff --git a/SignalServiceKit/src/Loki/API/LokiSnodeProxy.swift b/SignalServiceKit/src/Loki/API/LokiSnodeProxy.swift index c32747566..d23328ced 100644 --- a/SignalServiceKit/src/Loki/API/LokiSnodeProxy.swift +++ b/SignalServiceKit/src/Loki/API/LokiSnodeProxy.swift @@ -67,14 +67,14 @@ internal class LokiSnodeProxy : LokiHTTPClient { task.resume() return promise }.map { rawResponse in - guard let data = rawResponse as? Data, !data.isEmpty, let cipherText = Data(base64Encoded: data) else { + guard let responseAsData = rawResponse as? Data, let cipherText = Data(base64Encoded: responseAsData) else { print("[Loki] Received a non-string encoded response.") return rawResponse } let response = try DiffieHellman.decrypt(cipherText, using: symmetricKey) let uncheckedJSON = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? JSON - guard let json = uncheckedJSON, let httpStatusCode = json["status"] as? Int else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) } - let isSuccess = (200..<300).contains(httpStatusCode) + guard let json = uncheckedJSON, let statusCode = json["status"] as? Int else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) } + let isSuccess = (200..<300).contains(statusCode) var body: Any? = nil if let bodyAsString = json["body"] as? String { body = bodyAsString @@ -82,7 +82,7 @@ internal class LokiSnodeProxy : LokiHTTPClient { body = bodyAsJSON } } - guard isSuccess else { throw HTTPError.networkError(code: httpStatusCode, response: body, underlyingError: Error.targetSnodeHTTPError(code: httpStatusCode, message: body)) } + guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: body, underlyingError: Error.targetSnodeHTTPError(code: statusCode, message: body)) } return body }.recover { error -> Promise in print("[Loki] Proxy request failed with error: \(error.localizedDescription).")