oauth2_broker/ext/
token_lease.rs1use crate::{
6 _prelude::*,
7 auth::{ScopeSet, TokenFamily},
8};
9
10pub type TokenLeaseFuture<'a, Lease, Error> =
12 Pin<Box<dyn Future<Output = Result<TokenLeaseState<Lease>, Error>> + 'a + Send>>;
13
14pub trait TokenLeaseExt<Lease, Error>: Send + Sync {
17 fn lease(&self, context: TokenLeaseContext) -> TokenLeaseFuture<'_, Lease, Error>;
19}
20
21#[derive(Clone, Debug)]
23pub struct TokenLeaseContext {
24 pub family: TokenFamily,
26 pub scope: ScopeSet,
28 pub requested_at: OffsetDateTime,
30 pub minimum_ttl: Duration,
32 pub reason: Option<String>,
34}
35impl TokenLeaseContext {
36 pub fn new(family: TokenFamily, scope: ScopeSet) -> Self {
38 Self {
39 family,
40 scope,
41 requested_at: OffsetDateTime::now_utc(),
42 minimum_ttl: Duration::ZERO,
43 reason: None,
44 }
45 }
46
47 pub fn with_requested_at(mut self, instant: OffsetDateTime) -> Self {
49 self.requested_at = instant;
50
51 self
52 }
53
54 pub fn with_minimum_ttl(mut self, ttl: Duration) -> Self {
57 self.minimum_ttl = ttl;
58
59 self
60 }
61
62 pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
64 self.reason = Some(reason.into());
65
66 self
67 }
68}
69
70pub enum TokenLeaseState<Lease> {
72 Granted {
74 lease: Lease,
76 expires_at: OffsetDateTime,
78 },
79 Pending {
81 retry_in: Duration,
83 },
84 NeedsRefresh,
86}
87impl<Lease> Debug for TokenLeaseState<Lease> {
88 fn fmt(&self, f: &mut Formatter) -> FmtResult {
89 match self {
90 Self::Granted { expires_at, .. } =>
91 f.debug_struct("TokenLeaseState::Granted").field("expires_at", expires_at).finish(),
92 Self::Pending { retry_in } =>
93 f.debug_struct("TokenLeaseState::Pending").field("retry_in", retry_in).finish(),
94 Self::NeedsRefresh => f.debug_struct("TokenLeaseState::NeedsRefresh").finish(),
95 }
96 }
97}