1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use async_trait::async_trait;
use graph_core::cache::AsBearer;
use graph_core::identity::{ClientApplication, ForceTokenRefresh};
use graph_error::AuthExecutionResult;
use std::fmt::Display;

#[derive(Clone)]
pub struct BearerTokenCredential(String);

impl BearerTokenCredential {
    pub fn new(access_token: impl ToString) -> BearerTokenCredential {
        BearerTokenCredential(access_token.to_string())
    }

    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl Display for BearerTokenCredential {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl AsBearer for BearerTokenCredential {
    fn as_bearer(&self) -> String {
        self.0.clone()
    }
}

impl AsRef<str> for BearerTokenCredential {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl From<&str> for BearerTokenCredential {
    fn from(value: &str) -> Self {
        BearerTokenCredential(value.to_string())
    }
}

impl From<String> for BearerTokenCredential {
    fn from(value: String) -> Self {
        BearerTokenCredential(value)
    }
}

#[async_trait]
impl ClientApplication for BearerTokenCredential {
    fn get_token_silent(&mut self) -> AuthExecutionResult<String> {
        Ok(self.0.clone())
    }

    async fn get_token_silent_async(&mut self) -> AuthExecutionResult<String> {
        Ok(self.0.clone())
    }

    fn with_force_token_refresh(&mut self, _force_token_refresh: ForceTokenRefresh) {}
}