fix authentication cors bug

This commit is contained in:
mpostma 2021-01-12 18:08:16 +01:00
parent 60c636738b
commit e5c220b82c
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
1 changed files with 16 additions and 12 deletions

View File

@ -6,6 +6,8 @@ use std::task::{Context, Poll};
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, web}; use actix_web::{dev::ServiceRequest, dev::ServiceResponse, web};
use futures::future::{err, ok, Future, Ready}; use futures::future::{err, ok, Future, Ready};
use actix_web::error::ResponseError as _;
use actix_web::dev::Body;
use crate::error::{Error, ResponseError}; use crate::error::{Error, ResponseError};
use crate::Data; use crate::Data;
@ -17,14 +19,13 @@ pub enum Authentication {
Admin, Admin,
} }
impl<S: 'static, B> Transform<S> for Authentication impl<S: 'static> Transform<S> for Authentication
where where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>, S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = actix_web::Error>,
S::Future: 'static, S::Future: 'static,
B: 'static,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<Body>;
type Error = actix_web::Error; type Error = actix_web::Error;
type InitError = (); type InitError = ();
type Transform = LoggingMiddleware<S>; type Transform = LoggingMiddleware<S>;
@ -44,14 +45,13 @@ pub struct LoggingMiddleware<S> {
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
impl<S, B> Service for LoggingMiddleware<S> impl<S> Service for LoggingMiddleware<S>
where where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static, S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = actix_web::Error> + 'static,
S::Future: 'static, S::Future: 'static,
B: 'static,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<Body>;
type Error = actix_web::Error; type Error = actix_web::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>; type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
@ -72,7 +72,11 @@ where
let auth_header = match req.headers().get("X-Meili-API-Key") { let auth_header = match req.headers().get("X-Meili-API-Key") {
Some(auth) => match auth.to_str() { Some(auth) => match auth.to_str() {
Ok(auth) => auth, Ok(auth) => auth,
Err(_) => return Box::pin(err(ResponseError::from(Error::MissingAuthorizationHeader).into())), Err(_) => {
let error = ResponseError::from(Error::MissingAuthorizationHeader).error_response();
let (request, _) = req.into_parts();
return Box::pin(ok(ServiceResponse::new(request, error)))
}
}, },
None => { None => {
return Box::pin(err(ResponseError::from(Error::MissingAuthorizationHeader).into())); return Box::pin(err(ResponseError::from(Error::MissingAuthorizationHeader).into()));
@ -95,9 +99,9 @@ where
if authenticated { if authenticated {
Box::pin(svc.call(req)) Box::pin(svc.call(req))
} else { } else {
Box::pin(err( let error = ResponseError::from(Error::InvalidToken(auth_header.to_string())).error_response();
ResponseError::from(Error::InvalidToken(auth_header.to_string())).into() let (request, _) = req.into_parts();
)) return Box::pin(ok(ServiceResponse::new(request, error)))
} }
} }
} }