From 8feb228ea038abc84835b2bd7e45cdaff365d982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Mon, 20 May 2024 14:26:24 -0700 Subject: [PATCH] Add NIP-98 authentication to push notification client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel D’Aquino Reviewed-by: William Casarin --- damus/Models/PushNotificationClient.swift | 51 +++++++++++++---------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/damus/Models/PushNotificationClient.swift b/damus/Models/PushNotificationClient.swift index 9f862896..ed3fad9a 100644 --- a/damus/Models/PushNotificationClient.swift +++ b/damus/Models/PushNotificationClient.swift @@ -33,30 +33,37 @@ struct PushNotificationClient { // create post request let url = self.settings.send_device_token_to_localhost ? Constants.DEVICE_TOKEN_RECEIVER_TEST_URL : Constants.DEVICE_TOKEN_RECEIVER_PRODUCTION_URL - var request = URLRequest(url: url) - request.httpMethod = "POST" - - // insert json data to the request - request.httpBody = try? JSONSerialization.data(withJSONObject: json, options: []) - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - - let task = URLSession.shared.dataTask(with: request) { data, response, error in - guard let data = data, error == nil else { - print(error?.localizedDescription ?? "No data") - return - } - - if let response = response as? HTTPURLResponse, !(200...299).contains(response.statusCode) { - print("Unexpected status code: \(response.statusCode)") - return - } - - let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) - if let responseJSON = responseJSON as? [String: Any] { - print(responseJSON) + let json_data = try JSONSerialization.data(withJSONObject: json) + + + let (data, response) = try await make_nip98_authenticated_request( + method: .post, + url: url, + payload: json_data, + payload_type: .json, + auth_keypair: self.keypair + ) + + if let httpResponse = response as? HTTPURLResponse { + switch httpResponse.statusCode { + case 200: + Log.info("Sent device token to Damus push notification server successfully", for: .push_notifications) + default: + Log.error("Error in sending device_token to Damus push notification server. HTTP status code: %d; Response: %s", for: .push_notifications, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown") + throw ClientError.http_response_error(status_code: httpResponse.statusCode, response: data) } } + + return + } + + +} - task.resume() +// MARK: Helper structures + +extension PushNotificationClient { + enum ClientError: Error { + case http_response_error(status_code: Int, response: Data) } }