-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauth.rs
More file actions
53 lines (45 loc) · 1.51 KB
/
auth.rs
File metadata and controls
53 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// -- Bearer token extension (set by HTTP middleware, read by RPC middleware) --
/// Marker inserted into request extensions by HTTP middleware when a valid
/// `Authorization: Bearer <token>` header is present.
#[derive(Clone, Debug)]
pub struct BearerToken(pub String);
/// Tower layer that extracts `Authorization: Bearer <token>` from HTTP
/// request headers and inserts a [`BearerToken`] into request extensions.
#[derive(Clone)]
pub struct BearerTokenLayer;
impl<S> tower::Layer<S> for BearerTokenLayer {
type Service = BearerTokenService<S>;
fn layer(&self, inner: S) -> Self::Service {
BearerTokenService { inner }
}
}
#[derive(Clone)]
pub struct BearerTokenService<S> {
inner: S,
}
impl<S, B> tower::Service<http::Request<B>> for BearerTokenService<S>
where
S: tower::Service<http::Request<B>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: http::Request<B>) -> Self::Future {
let token = req
.headers()
.get(http::header::AUTHORIZATION)
.and_then(|v| v.to_str().ok())
.and_then(|s| s.strip_prefix("Bearer "))
.map(|t| t.to_string());
if let Some(token) = token {
req.extensions_mut().insert(BearerToken(token));
}
self.inner.call(req)
}
}