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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use hyper::{ header::{HeaderValue, AUTHORIZATION}, Request, }; use parking_lot::RwLock; use tracing::{info, trace}; use std::{ fmt, future::Future, sync::Arc, task::{self, Poll}, time::{Duration, Instant}, }; use crate::{Credentials, TokenSource}; pub struct AddAuthorization<S> { inner: Arc<Inner>, service: S, } impl AddAuthorization<()> { pub async fn init<S>(service: S) -> AddAuthorization<S> { AddAuthorization { inner: Arc::new(Inner::init().await), service } } pub fn init_with<S>(source: impl Into<TokenSource>, service: S) -> AddAuthorization<S> { AddAuthorization { inner: Arc::new(Inner::init_with(source)), service } } } struct Inner { source: TokenSource, cache: RwLock<Option<Cache>>, max_retry: u8, } impl Inner { async fn init() -> Self { Self::init_with(Credentials::default().await) } fn init_with(s: impl Into<TokenSource>) -> Self { Self { source: s.into(), cache: RwLock::new(None), max_retry: 5 } } fn poll_ready(&self, cx: &mut task::Context<'_>) -> Poll<()> { if let Some(ref cache) = *self.cache.read() { if !cache.expired(Instant::now()) { return Poll::Ready(()); } trace!("token is expired: expiry={:?}", cache.expiry); } else { trace!("token is uninitialized"); } let mut lock = self.cache.write(); trace!("start token update..."); if let Some(ref cache) = *lock { if !cache.expired(Instant::now()) { trace!("token is already updated: expiry={:?}", cache.expiry); return Poll::Ready(()); } } let fut = self.source.token(); pin_utils::pin_mut!(fut); let mut retry = 0; let cache = loop { match fut.as_mut().poll(cx) { Poll::Ready(r) => match r.and_then(|t| t.into_pairs()) { Ok((value, expiry)) => break Cache::new(value, expiry), Err(err) => { info!("an error occurred: retry={}, err={:?}", retry, err); if retry >= self.max_retry { panic!("max retry exceeded: retry={}, last error={:?}", retry, err); } retry += 1; } }, Poll::Pending => {} } }; trace!("token updated: expiry={:?}", cache.expiry); *lock = Some(cache); Poll::Ready(()) } } #[derive(Clone)] struct Cache { value: HeaderValue, expiry: Instant, } impl Cache { fn new(value: HeaderValue, expiry: Instant) -> Self { Self { value, expiry } } fn expired(&self, at: Instant) -> bool { const EXPIRY_DELTA: Duration = Duration::from_secs(10); self.expiry.checked_duration_since(at).map(|dur| dur < EXPIRY_DELTA).unwrap_or(true) } fn value(&self) -> HeaderValue { self.value.clone() } } impl<S, B> tower_service::Service<Request<B>> for AddAuthorization<S> where S: tower_service::Service<Request<B>>, { type Response = S::Response; type Error = S::Error; type Future = S::Future; fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> { match self.inner.poll_ready(cx) { Poll::Ready(()) => self.service.poll_ready(cx), Poll::Pending => unreachable!(), } } fn call(&mut self, mut req: Request<B>) -> Self::Future { match *self.inner.cache.read() { Some(ref cache) => req.headers_mut().insert(AUTHORIZATION, cache.value()), None => unreachable!(), }; self.service.call(req) } } impl<S: Clone> Clone for AddAuthorization<S> { fn clone(&self) -> Self { Self { inner: self.inner.clone(), service: self.service.clone() } } } impl<S: fmt::Debug> fmt::Debug for AddAuthorization<S> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("AddAuthorization").field("service", &self.service).finish() } } #[cfg(test)] mod test { use super::*; #[test] fn cache_expiry() { let now = Instant::now(); let c = Cache::new(HeaderValue::from_static("value"), now); assert!(c.expired(now - Duration::from_secs(5))); assert!(!c.expired(now - Duration::from_secs(30))) } }