Skip to main content

cloud_storage_lite/token_provider/
mod.rs

1//! The [`TokenProvider`] trait and implementations.
2
3use std::sync::Arc;
4
5use chrono::{DateTime, Utc};
6
7#[cfg(any(test, feature = "oauth-token-provider"))]
8pub mod oauth;
9mod renewing;
10
11pub use renewing::RenewingTokenProvider;
12
13/// A `TokenProvider` is something that, when invoked, gives a valid, unexpired access token.
14#[cfg_attr(test, mockall::automock)]
15#[async_trait::async_trait]
16pub trait TokenProvider: Send + Sync {
17    /// Returns new JWT-formatted access token.
18    async fn get_token(&self) -> anyhow::Result<Arc<Token>>;
19
20    /// Marks the current token as invalid, forcing a refresh, if possible.
21    async fn invalidate_token(&self) {}
22}
23
24/// A access token and its expiry.
25#[derive(Clone)]
26#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
27pub struct Token {
28    /// The JWT-formatted token.
29    pub token: String,
30
31    /// The token's expiry.
32    pub expiry: DateTime<Utc>,
33}
34
35impl Default for Token {
36    fn default() -> Self {
37        Self {
38            token: Default::default(),
39            expiry: DateTime::from_utc(chrono::NaiveDateTime::from_timestamp(0, 0), Utc),
40        }
41    }
42}
43
44impl AsRef<str> for Token {
45    fn as_ref(&self) -> &str {
46        &self.token
47    }
48}