use super::claims::{validate_jwt_with_config, JwtResult};
use crate::config::Config;
use lambda_http::Request;
pub async fn jwt_middleware(request: &Request) -> Result<JwtResult, String> {
let config = Config::try_get().ok_or("Config not initialized")?;
if let Some(auth_header) = request.headers().get("Authorization") {
if let Ok(auth_header_str) = auth_header.to_str() {
if let Some(token) = auth_header_str.strip_prefix("Bearer ") {
match validate_jwt_with_config(token, config) {
Ok(claims) => {
return Ok(JwtResult {
user_id: Some(claims.sub),
tenant: claims.tenant,
});
}
Err(e) => {
return Err(e);
}
}
}
}
}
Ok(JwtResult::empty())
}
pub fn extract_bearer_token(request: &Request) -> Option<String> {
request
.headers()
.get("Authorization")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.strip_prefix("Bearer "))
.map(|s| s.to_string())
}