Skip to main content

TokenProvider

Trait TokenProvider 

Source
pub trait TokenProvider: Send + Sync {
    // Required method
    fn get_token<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Contract for obtaining a fresh Tapis JWT.

Implement this trait on any type that knows how to produce or refresh a Tapis access token. Pass an Arc<dyn TokenProvider> to a service-client constructor to enable automatic token refresh inside RefreshMiddleware.

§No-circular-dependency guarantee

tapis-core has zero HTTP or service-client dependencies. tapis-authenticator implements TokenProvider; all other service crates depend on tapis-core only. This breaks the would-be cycle:

tapis-jobs ──dep──> tapis-core <──impl── tapis-authenticator
                         ▲
                   (no dep on tapis-authenticator)

§Infinite-loop protection

RefreshMiddleware checks the outgoing URL before calling get_token(). If the URL contains /oauth2/tokens or /v3/tokens it skips refresh, mirroring the tapipy guard on create_token / refresh_token operations.

§Example

use std::sync::Arc;
use tapis_core::TokenProvider;

struct StaticToken(String);

#[async_trait::async_trait]
impl TokenProvider for StaticToken {
    async fn get_token(&self) -> Option<String> {
        Some(self.0.clone())
    }
}

// Pass to any service client that accepts a TokenProvider.
let provider: Arc<dyn TokenProvider> = Arc::new(StaticToken("my-jwt".into()));

Required Methods§

Source

fn get_token<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Return a fresh JWT string, or None to leave the current token on the request unchanged.

Implementations should not panic. If the token cannot be obtained (e.g., network error), return None so the request proceeds with the stale token rather than crashing.

Implementors§