diff --git a/src/main.rs b/src/main.rs index 4c909b1..b3dfe86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod config; use config::{Config, ConfigBuilder}; mod api; pub mod crypto; +pub mod utils; type Error = Box; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..3b02593 --- /dev/null +++ b/src/utils.rs @@ -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()) +}