Hi all,
I'm starting working with actix-web v2.0 for publishing a small web app at work.
One of my needs is to authenticate a session token received from another service.
I thought I can do that with a middleware, but I have an issue recovering the Identity.
Here is the server code:
let server = HttpServer::new(move || {
App::new()
.data(AppOptions {
root: root_folder.clone(),
})
.wrap(Logger::default())
.wrap(Logger::new("%a %{User-Agent}i"))
.wrap(IdentityService::new(
CookieIdentityPolicy::new(SECRET_KEY.as_bytes())
.name("auth")
.path("/")
.max_age_time(chrono::Duration::days(1))
.secure(!dev_mode),
))
.wrap(authorization::Authorization::new(
auth_service.clone().to_string(),
))
.route("/api", web::to(ws_proxy))
.service(static_site)
})
.keep_alive(keep_alive);
I set a CookieIdentityPolicy
before my middleware that is authorization::Authorization
.
I can access to the Identity from my service static_site
without a problem.
From the middleware I just can't:
impl<S, B> Service for AuthorizationService<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: ServiceRequest) -> Self::Future {
println!("Hi from start. You requested: {:?}", req.path());
if req.get_identity().is_none() {
// Should never reach this branch
println!("Error not identity found");
return Box::pin(async { Err(actix_web::error::ErrorUnauthorized("Unauthorized")) });
} else {
// Here I should check the identity first
Box::pin( self.service.call(req) )
}
}
}
At the time being the code is just for understanding how a middleware works, the line with req.get_identity()
always return a None, and that should be incorrect.
On documentations looks like it should be fine:
Not sure what I'm doing wrong, any help would be appreciated.
Thanks.