feat: add utils.rs

provides a function that extracts auth header from headers
This commit is contained in:
Radical 2025-05-04 18:11:12 +02:00
parent bcf857d6b2
commit aa865e2ed4
2 changed files with 18 additions and 0 deletions

View file

@ -8,6 +8,7 @@ mod config;
use config::{Config, ConfigBuilder};
mod api;
pub mod crypto;
pub mod utils;
type Error = Box<dyn std::error::Error>;

17
src/utils.rs Normal file
View file

@ -0,0 +1,17 @@
use actix_web::{HttpResponse, http::header::HeaderMap};
pub fn get_auth_header(headers: &HeaderMap) -> Result<&str, HttpResponse> {
let auth_token = headers.get(actix_web::http::header::CONTENT_TYPE);
if let None = auth_token {
return Err(HttpResponse::Unauthorized().finish());
}
let auth = auth_token.unwrap().to_str();
if let Err(error) = auth {
return Err(HttpResponse::Unauthorized().json(format!(r#" {{ "error": "{}" }} "#, error)));
}
Ok(auth.unwrap())
}