Add NIP-98 authentication to push notification client
Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Reviewed-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -33,30 +33,37 @@ struct PushNotificationClient {
|
|||||||
|
|
||||||
// create post request
|
// create post request
|
||||||
let url = self.settings.send_device_token_to_localhost ? Constants.DEVICE_TOKEN_RECEIVER_TEST_URL : Constants.DEVICE_TOKEN_RECEIVER_PRODUCTION_URL
|
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)
|
let json_data = try JSONSerialization.data(withJSONObject: json)
|
||||||
request.httpMethod = "POST"
|
|
||||||
|
|
||||||
// insert json data to the request
|
let (data, response) = try await make_nip98_authenticated_request(
|
||||||
request.httpBody = try? JSONSerialization.data(withJSONObject: json, options: [])
|
method: .post,
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
url: url,
|
||||||
|
payload: json_data,
|
||||||
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
payload_type: .json,
|
||||||
guard let data = data, error == nil else {
|
auth_keypair: self.keypair
|
||||||
print(error?.localizedDescription ?? "No data")
|
)
|
||||||
return
|
|
||||||
}
|
if let httpResponse = response as? HTTPURLResponse {
|
||||||
|
switch httpResponse.statusCode {
|
||||||
if let response = response as? HTTPURLResponse, !(200...299).contains(response.statusCode) {
|
case 200:
|
||||||
print("Unexpected status code: \(response.statusCode)")
|
Log.info("Sent device token to Damus push notification server successfully", for: .push_notifications)
|
||||||
return
|
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)
|
||||||
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
|
|
||||||
if let responseJSON = responseJSON as? [String: Any] {
|
|
||||||
print(responseJSON)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
task.resume()
|
// MARK: Helper structures
|
||||||
|
|
||||||
|
extension PushNotificationClient {
|
||||||
|
enum ClientError: Error {
|
||||||
|
case http_response_error(status_code: Int, response: Data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user