http-endpoint : upadate header extraction to not use unwrap
use actix_web::dev::ServiceRequest;
use actix_web::http::header;
+use actix_web::error::ErrorBadRequest;
use actix_web::Error;
use actix_web_httpauth::extractors::basic::{Config, BasicAuth};
+use actix_web_httpauth::extractors::AuthenticationError;
use actix_web::client::Client;
use awc::http::StatusCode;
use log;
-use actix_web_httpauth::extractors::AuthenticationError;
const AUTH_SERVICE_URL: &str = "AUTH_SERVICE_URL";
@@ -27,16 +28,12 @@ pub async fn basic_validator(
let url = format!("http://{}/auth", auth_service_url);
- // the unwrap is safe here because the BasicAuth extractor already check the header.
- // We fetch the encoded header to avoid decoding then re-encoding
- let encoded_basic_header = req.headers().get(header::AUTHORIZATION).unwrap();
- // match {
- // Some(h) => h,
- // None => return Err(ErrorBadRequest(Err("Missing Authorization header"))),
- // };
+ // We fetch the encoded header to avoid re-encoding
+ let encoded_basic_header =
+ req.headers().get(header::AUTHORIZATION)
+ .ok_or_else(|| ErrorBadRequest("Missing Authorization header"))?;
let response = Client::default().get(url)
- // don't bother decoding the header
.header(header::AUTHORIZATION, encoded_basic_header.clone())
.send()
.await;
