So I'm altering some authentication middleware I've written in actix-web to perform a database lookup, if the session id that is provided isn't in the cache. The problem is that in order to do the lookup I need to await a future so that I can continue to use the data in my middleware to perform the rest of my authentication.
Hopefully there's a simple solution here!
Heres my code:
fn call(&self, request: ServiceRequest) -> Self::Future {
let cache = request.app_data::<Cache>().unwrap();
let pool = request.app_data::<Pool>().unwrap();
//sessions type is: impl Future<Output = Result<SessionObj, Error>>
let session = session_lookup(Cache, Pool, request)
[...do some stuff with the session]
[...do some stuff to get a csrf token]
if *csrf_token == *session_token{
let res = self.service.call(request);
Box::pin(async move {
// forwarded responses map to "left" body
res.await.map(ServiceResponse::map_into_left_body) })
}else{
let (request, _pl) = request.into_parts();
let response = HttpResponse::Forbidden()
.body(body)
.map_into_right_body();
return Box::pin(async { Ok(ServiceResponse::new(request, response)) })
}
}
}
Thanks!