pub mod error;
pub mod middleware;
#[cfg(feature = "ipc")]
pub mod proxy_adapter;
#[cfg(test)]
mod tests;
use axum::Router;
use middleware::JwtAuthenticationLayer;
#[cfg(feature = "ipc")]
pub use proxy_adapter::{
JwtProxyConfig, JwtProxyService, generate_token_proxy, validate_token_proxy,
};
#[inline]
fn is_running_as_module() -> bool {
std::env::var("PYWATT_MODULE_ID").is_ok()
}
pub trait RouterJwtExt {
fn with_jwt<T>(self, secret_key: String) -> Self
where
T: serde::de::DeserializeOwned + Send + Sync + Clone + 'static;
}
impl RouterJwtExt for Router {
fn with_jwt<T>(self, secret_key: String) -> Self
where
T: serde::de::DeserializeOwned + Send + Sync + Clone + 'static,
{
self.layer(JwtAuthenticationLayer::<T>::new(secret_key))
}
}
pub type JwtAuthLayer<T = serde_json::Value> = JwtAuthenticationLayer<T>;
use axum::body::Bytes;
use http_body_util::BodyExt;
pub async fn body_to_string<B>(body: B) -> String
where
B: axum::body::HttpBody<Data = Bytes> + Send + 'static,
B::Error: std::fmt::Debug,
{
match body.collect().await {
Ok(collected) => String::from_utf8_lossy(&collected.to_bytes()).to_string(),
Err(_) => String::new(),
}
}