2022-04-02 13:44:09 +00:00
|
|
|
//! # Authentication middleware
|
|
|
|
|
2022-04-02 13:58:31 +00:00
|
|
|
use std::future::{Future, ready, Ready};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
2022-04-02 13:44:09 +00:00
|
|
|
|
2022-04-02 13:58:31 +00:00
|
|
|
use actix_web::{dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, Error, HttpResponse};
|
|
|
|
use actix_web::body::EitherBody;
|
2022-04-02 13:44:09 +00:00
|
|
|
|
|
|
|
// There are two steps in middleware processing.
|
|
|
|
// 1. Middleware initialization, middleware factory gets called with
|
|
|
|
// next service in chain as parameter.
|
|
|
|
// 2. Middleware's call method gets called with normal request.
|
|
|
|
pub struct AuthMiddleware;
|
|
|
|
|
|
|
|
// Middleware factory is `Transform` trait
|
|
|
|
// `S` - type of the next service
|
|
|
|
// `B` - type of response's body
|
|
|
|
impl<S, B> Transform<S, ServiceRequest> for AuthMiddleware
|
|
|
|
where
|
2022-04-02 13:58:31 +00:00
|
|
|
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error> + 'static,
|
2022-04-02 13:44:09 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
2022-04-02 13:58:31 +00:00
|
|
|
type Response = ServiceResponse<EitherBody<B>>;
|
2022-04-02 13:44:09 +00:00
|
|
|
type Error = Error;
|
2022-04-02 13:58:31 +00:00
|
|
|
type Transform = AuthInnerMiddleware<S>;
|
2022-04-02 13:44:09 +00:00
|
|
|
type InitError = ();
|
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2022-04-02 13:58:31 +00:00
|
|
|
ready(Ok(AuthInnerMiddleware { service: Rc::new(service) }))
|
2022-04-02 13:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 13:58:31 +00:00
|
|
|
pub struct AuthInnerMiddleware<S> {
|
|
|
|
service: Rc<S>,
|
2022-04-02 13:44:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 13:58:31 +00:00
|
|
|
impl<S, B> Service<ServiceRequest> for AuthInnerMiddleware<S>
|
2022-04-02 13:44:09 +00:00
|
|
|
where
|
2022-04-02 13:58:31 +00:00
|
|
|
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error> + 'static,
|
2022-04-02 13:44:09 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
2022-04-02 13:58:31 +00:00
|
|
|
type Response = ServiceResponse<EitherBody<B>>;
|
2022-04-02 13:44:09 +00:00
|
|
|
type Error = Error;
|
2022-04-02 13:58:31 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output=Result<Self::Response, Self::Error>>>>;
|
2022-04-02 13:44:09 +00:00
|
|
|
|
|
|
|
forward_ready!(service);
|
|
|
|
|
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
|
|
|
println!("Hi from start. You requested: {}", req.path());
|
|
|
|
|
2022-04-02 13:58:31 +00:00
|
|
|
let service = Rc::clone(&self.service);
|
2022-04-02 13:44:09 +00:00
|
|
|
|
|
|
|
// Forward request
|
2022-04-02 13:58:31 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
if req.path().starts_with("/.git") {
|
|
|
|
return Ok(req.into_response(
|
|
|
|
HttpResponse::Unauthorized()
|
|
|
|
.body("Hey don't touch this!")
|
|
|
|
.map_into_right_body()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
service
|
|
|
|
.call(req)
|
|
|
|
.await
|
|
|
|
.map(ServiceResponse::map_into_left_body)
|
|
|
|
})
|
2022-04-02 13:44:09 +00:00
|
|
|
}
|
|
|
|
}
|