actix / actix-web-httpauth

Moved to actix-extras repo.
https://github.com/actix/actix-extras/tree/HEAD/actix-web-httpauth
Apache License 2.0
82 stars 37 forks source link

Own ServiceRequest in middleware #8

Closed asonix closed 5 years ago

asonix commented 5 years ago

Hello!

I'm trying to use the middleware you've provided in a crate to implement OAuth2, and I've come to the realization I need access to the ServiceRequest in my validator_fn in order to set an extension. Since ServiceRequest is only borrowed, I can't move it into the future returned by the validator_fn.

You could enable this just by making validator_fn own the ServiceRequest and requiring that it return an IntoFuture<Item = ServiceRequest, ...>

svartalf commented 5 years ago

Lucky us, it is still in alpha, so we a free to make any changes if required.

Can you provide an example of the code you are trying to make? I had never used extensions in actix-web and I'm not sure why mutable reference to ServiceRequest is not enough in that case.

asonix commented 5 years ago

Here's the contents of the closure that I'm passing to the middleware:

if let Some(data) = request.app_data::<SledEndpoint>() {
    let bearer = bearer.clone();
    let scopes = scopes.clone();

    let f = actix_threadpool::run(move || {
        let bearer = bearer.clone();
        let mut resource = data.resource(scopes.clone());

        protect(&mut resource, &WrappedResource(&bearer)).map_err(|_| AccessError)
    })
    .map(move |grant| {
        // I move the request into the future here
        request.extensions_mut().insert(Grant(grant));
        request
    })
    .map_err(|_| AccessError.into());

    Box::new(f)
} else {
    Box::new(err(AccessError.into()))
}
svartalf commented 5 years ago

Ah, the futures 0.1 limitations, got it.

I've made the owned-middleware branch (#9 also), you can check it out. I'll merge and publish it after your test results.

svartalf commented 5 years ago

Somehow I've forgot about merging it, sorry about that. Same to #7 I'm closing this issue, because changes from the linked and this one issues were merged and published as a 0.3.0 version.