Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F33312149
D12304.1768813872.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D12304.1768813872.diff
View Options
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<HttpResponse, ErrorResponse> {
+ 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<Body>;
type HttpResponse = Response<Body>;
@@ -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<HttpResponse, crate::websockets::errors::BoxedError> {
+ 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")),
+ "/device_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<String, String>;
+}
+
+pub trait ResponseExt {
+ fn json<T: ?Sized + serde::Serialize>(
+ response: &T,
+ ) -> Result<HttpResponse, ErrorResponse>;
+}
+
+impl RequestExt for HttpRequest {
+ fn query_string_args(&self) -> HashMap<String, String> {
+ 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<Body> {
+ fn json<T: ?Sized + serde::Serialize>(
+ body: &T,
+ ) -> Result<HttpResponse, ErrorResponse> {
+ 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<HttpResponse, BoxedError>;
+}
+
+impl IntoServerResponse for Result<HttpResponse, ErrorResponse> {
+ fn into_response(self) -> Result<HttpResponse, BoxedError> {
+ let response = match self {
+ Ok(ok_response) => ok_response,
+ Err(err_response) => err_response?,
+ };
+ Ok(response)
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Jan 19, 9:11 AM (10 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5955559
Default Alt Text
D12304.1768813872.diff (4 KB)
Attached To
Mode
D12304: [identity] Add /inbound_keys HTTP handler
Attached
Detach File
Event Timeline
Log In to Comment