pub trait TokenSource:
Send
+ Sync
+ 'static {
// Required method
fn watch(
&self,
) -> Receiver<Option<Result<String, Box<dyn Error + Send + Sync>>>>;
// Provided methods
fn get_token<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn format_header(&self, token: String) -> String { ... }
}Expand description
A source for authentication tokens.
Required Methods§
Provided Methods§
Sourcefn get_token<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn get_token<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Gets a token, possibly refreshing it.
If the token cannot be obtained, returns a TokenSourceError.
Prefer using watch if a subscription to token updates is needed.
§Implementation Note
The default implementation uses the watch channel to get the latest token. ``
- Should be efficient to call multiple times.
- Errors should be returned if no valid token can be obtained.
- Should try to not return errors as long as a valid token is available.
Sourcefn format_header(&self, token: String) -> String
fn format_header(&self, token: String) -> String
Formats the token for use in an Authorization header.
The default implementation formats the token as a Bearer token. Override this method if a different format is required.