Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 48ce94c8 authored by Pierre Jarriges's avatar Pierre Jarriges
Browse files

wip auth admin

parent 0a94dcf7
No related branches found
No related tags found
No related merge requests found
......@@ -28,25 +28,50 @@ pub struct AuthenticatedMiddleware<S> {
service: S,
}
#[derive(serde::Deserialize)]
struct Credentials {
id: String,
password: String,
}
async fn auth(req: &mut ServiceRequest) -> Result<(), Box<dyn actix_web::ResponseError>> {
let cookie = req.cookie("auth");
match cookie {
Some(cookie) => Ok(()),
None => match req.extract::<actix_web::web::Form<Credentials>>().await {
Ok(credentials) => Ok(()),
Err(_) => Err(Box::new(actix_web::ResponseError::status_code(
actix_web::http::StatusCode::UNAUTHORIZED,
))),
},
}
}
impl<S, B> Service<ServiceRequest> for AuthenticatedMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse<actix_web::body::EitherBody<B>>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
forward_ready!(service);
fn call(&self, req: ServiceRequest) -> Self::Future {
println!("AUTH MW");
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
println!("RESP");
Ok(res)
let credentials = req.extract::<actix_web::web::Form<Credentials>>().await;
let authenticated = auth(&mut req).await;
if let Err(msg) = authenticated {
return Ok(req.error_response(Error::from(msg)).map_into_right_body());
}
self.service
.call(req)
.await
.map(|res| res.map_into_left_body())
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment