gouth 0.2.1

This library provides auto-renewed tokens for GCP service authentication.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use crate::Token;
use tonic::{metadata::MetadataValue, Interceptor, Request, Status};

macro_rules! map_err {
    ($res:expr) => {
        $res.map_err(|e| Status::unknown(e.to_string()))
    };
}

pub fn interceptor() -> impl Into<Interceptor> {
    let token = Token::new().expect("Token::new()");
    move |mut req: Request<()>| {
        let token = map_err!(token.header_value())?;
        let meta = map_err!(MetadataValue::from_str(&*token))?;
        req.metadata_mut().insert("authorization", meta);
        Ok(req)
    }
}