graph_core/identity/
client_application.rs

1use async_trait::async_trait;
2use dyn_clone::DynClone;
3use graph_error::AuthExecutionResult;
4
5#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
6pub enum ForceTokenRefresh {
7    /// Always use the token cache first to when returning tokens.
8    /// Expired tokens will still cause an authorization request to
9    /// be called.
10    #[default]
11    Never,
12    /// ForceRefreshToken::Once will cause only the next authorization request
13    /// to ignore any tokens in cache and request a new token. Authorization
14    /// requests after this are treated as ForceRefreshToken::Never
15    Once,
16    /// Always make an authorization request regardless of any tokens in cache.
17    Always,
18}
19
20dyn_clone::clone_trait_object!(ClientApplication);
21
22#[async_trait]
23pub trait ClientApplication: DynClone + Send + Sync {
24    fn get_token_silent(&mut self) -> AuthExecutionResult<String>;
25
26    async fn get_token_silent_async(&mut self) -> AuthExecutionResult<String>;
27
28    fn with_force_token_refresh(&mut self, force_token_refresh: ForceTokenRefresh);
29}
30
31#[async_trait]
32impl ClientApplication for String {
33    fn get_token_silent(&mut self) -> AuthExecutionResult<String> {
34        Ok(self.clone())
35    }
36
37    async fn get_token_silent_async(&mut self) -> AuthExecutionResult<String> {
38        Ok(self.clone())
39    }
40
41    fn with_force_token_refresh(&mut self, _force_token_refresh: ForceTokenRefresh) {}
42}