5 unstable releases

0.11.1 Apr 5, 2024
0.11.0 Mar 15, 2024
0.10.0 Sep 22, 2023
0.1.0 Sep 14, 2022
0.1.0-alpha.0 Aug 29, 2022

#1607 in Network programming

Download history 4/week @ 2023-12-24 11/week @ 2024-01-07 21/week @ 2024-01-14 2/week @ 2024-01-21 10/week @ 2024-01-28 13/week @ 2024-02-04 43/week @ 2024-02-11 71/week @ 2024-02-18 116/week @ 2024-02-25 74/week @ 2024-03-03 255/week @ 2024-03-10 237/week @ 2024-03-17 23/week @ 2024-03-24 373/week @ 2024-03-31 115/week @ 2024-04-07

775 downloads per month

MIT license

25KB
456 lines

Tonic Async Interceptor

Crates.io Documentation Crates.io

This crate contains AsyncInterceptor, an async variant of Tonic's Interceptor. Other than accepting an async interceptor function, it works the same as Interceptor.

Async interceptor functions are useful for tasks like authentication, where you need to make asynchronous calls to another service or database within the interceptor.

Usage

Using with Tonic built-in Server/Router

async fn my_async_interceptor(req: Request<()>) -> Result<Request<()>, Status> {
    // do things and stuff
    Ok(req)
}

async fn main() {
    use tonic::transport::server;
    use tonic_async_interceptor::async_interceptor;
    let router = server::Server::builder()
        .layer(async_interceptor(my_async_interceptor))
        .add_service(some_service)
        .add_service(another_service);
    // ...
}

Setting a custom extension

Here's an example of an async interceptor which authenticates a user and sets a custom extension for the underlying service to use.

// Your custom extension
struct UserContext {
    user_id: String,
}

// Async interceptor fn
async fn authenticate(req: Request<()>) -> Result<Request<()>, Status> {
    // Inspect the gRPC metadata.
    let auth_header_val = match req.metadata().get("x-my-auth-header") {
        Some(val) => val,
        None => return Err(Status::unauthorized("Request missing creds")),
    };
    // Call some async function (`verify_auth`).
    let maybe_user_id: Result<String> =
        verify_auth(auth_header_val).await;
    
    let user_id = match maybe_user_id {
        Ok(id) => id,
        Err(_) => return Err(Status::unauthorized("Invalid creds")),
    };
    
    // Insert an extension, which can be inspected by the service.
    req.extensions_mut().insert(UserContext { user_id });
    
    Ok(req)
}

Why is this a separate crate?

The code in this crate was originally intended to live in the official Tonic crate, alongside the non-async interceptor code. However, the maintainer decided not to merge it due to lack of time and misalignment with his future vision for Tonic.

Dependencies

~4–6MB
~99K SLoC