diff --git a/services/identity/src/http/handlers.rs b/services/identity/src/http/handlers.rs --- a/services/identity/src/http/handlers.rs +++ b/services/identity/src/http/handlers.rs @@ -1,12 +1,35 @@ use super::{ - errors::{create_error_response, http400}, - ErrorResponse, HttpRequest, + errors::{create_error_response, http400, http404}, + utils::{RequestExt, ResponseExt}, + ErrorResponse, HttpRequest, HttpResponse, }; use comm_lib::auth::UserIdentity; use hyper::header::AUTHORIZATION; use hyper::StatusCode; use tracing::error; +#[tracing::instrument(skip_all)] +pub async fn inbound_keys_handler( + req: HttpRequest, + db_client: crate::DatabaseClient, +) -> Result { + verify_csat(&req, &db_client).await?; + + // Device ID query string arg is base64-url encoded + let query_args = req.query_string_args(); + let device_id_param = query_args + .get("device_id") + .ok_or_else(|| http400("missing device_id query param"))?; + let device_id = device_id_param.replace('-', "+").replace('_', "/"); + + let device_data = db_client + .find_device_by_id(&device_id) + .await? + .ok_or_else(|| http404("device not found"))?; + + HttpResponse::json(&device_data) +} + #[tracing::instrument(skip_all)] async fn verify_csat( req: &HttpRequest, diff --git a/services/identity/src/http/mod.rs b/services/identity/src/http/mod.rs --- a/services/identity/src/http/mod.rs +++ b/services/identity/src/http/mod.rs @@ -1,7 +1,8 @@ -use hyper::{Body, Request, Response}; +use hyper::{Body, Method, Request, Response}; mod errors; mod handlers; +mod utils; type HttpRequest = Request; type HttpResponse = Response; @@ -12,14 +13,23 @@ #[tracing::instrument(skip_all, name = "http_request", fields(request_id))] pub(super) async fn handle_http_request( req: HttpRequest, - _db_client: crate::DatabaseClient, + db_client: crate::DatabaseClient, ) -> Result { + use utils::IntoServerResponse; + tracing::Span::current() .record("request_id", uuid::Uuid::new_v4().to_string()); let response = match req.uri().path() { "/health" => Response::new(Body::from("OK")), + "/inbound_keys" => match req.method() { + &Method::GET => handlers::inbound_keys_handler(req, db_client) + .await + .into_response()?, + _ => errors::http405()?, + }, _ => errors::http404("Not found")?, }; + Ok(response) } diff --git a/services/identity/src/http/utils.rs b/services/identity/src/http/utils.rs new file mode 100644 --- /dev/null +++ b/services/identity/src/http/utils.rs @@ -0,0 +1,64 @@ +use hyper::{header::CONTENT_TYPE, Body, Response}; +use std::collections::HashMap; +use tracing::error; + +use super::{ + errors::{http500, BoxedError}, + ErrorResponse, HttpRequest, HttpResponse, +}; + +pub trait RequestExt { + fn query_string_args(&self) -> HashMap; +} + +pub trait ResponseExt { + fn json( + response: &T, + ) -> Result; +} + +impl RequestExt for HttpRequest { + fn query_string_args(&self) -> HashMap { + let Some(uri_str) = self.uri().query() else { + return HashMap::new(); + }; + let params: HashMap<_, _> = url::form_urlencoded::parse(uri_str.as_bytes()) + .into_owned() + .collect(); + + tracing::trace!("Found query string args: {:?}", params); + params + } +} + +impl ResponseExt for Response { + fn json( + body: &T, + ) -> Result { + let json_string = serde_json::to_string(&body).map_err(|err| { + error!("JSON serialization error: {err:?}"); + http500() + })?; + let response = Response::builder() + .header(CONTENT_TYPE, "application/json") + .body(Body::from(json_string)) + .map_err(|err| ErrorResponse::Err(Box::new(err)))?; + Ok(response) + } +} + +pub trait IntoServerResponse { + /// Convenience helper for converting handler return value + /// into response returned by server future + fn into_response(self) -> Result; +} + +impl IntoServerResponse for Result { + fn into_response(self) -> Result { + let response = match self { + Ok(ok_response) => ok_response, + Err(err_response) => err_response?, + }; + Ok(response) + } +}