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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use oauth2::{
    basic::BasicClient, reqwest::async_http_client, AccessToken, AuthUrl, ClientId, ClientSecret,
    RefreshToken, TokenResponse, TokenUrl,
};

use async_trait::async_trait;
use tokio::sync::Mutex;

use std::sync::Arc;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SecretType {
    Password,
    AccessToken,
}

#[async_trait]
pub trait SecretManager {
    fn secret_type(&self) -> SecretType;
    async fn secret(&self) -> String;
    async fn refresh(&mut self);
}

pub struct PasswordManager {
    pub password: String,
}

impl PasswordManager {
    pub fn new(password: impl Into<String>) -> Self {
        Self {
            password: password.into(),
        }
    }
}

#[async_trait]
impl SecretManager for PasswordManager {
    fn secret_type(&self) -> SecretType {
        SecretType::Password
    }

    async fn secret(&self) -> String {
        self.password.clone()
    }

    async fn refresh(&mut self) {}
}

pub struct Oauth2Manager {
    auth_url: AuthUrl,
    token_url: TokenUrl,
    client_id: ClientId,
    client_secret: ClientSecret,
    refresh_token: RefreshToken,
    access_token: AccessToken,
}

impl Oauth2Manager {
    pub async fn new(
        auth_url: impl Into<String>,
        token_url: impl Into<String>,
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        refresh_token: impl Into<String>,
    ) -> Self {
        let mut this = Self {
            auth_url: AuthUrl::new(auth_url.into()).unwrap(),
            token_url: TokenUrl::new(token_url.into()).unwrap(),
            client_id: ClientId::new(client_id.into()),
            client_secret: ClientSecret::new(client_secret.into()),
            refresh_token: RefreshToken::new(refresh_token.into()),
            access_token: AccessToken::new("".into()),
        };

        this.refresh().await;
        this
    }
}

#[async_trait]
impl SecretManager for Oauth2Manager {
    fn secret_type(&self) -> SecretType {
        SecretType::AccessToken
    }

    async fn secret(&self) -> String {
        self.access_token.secret().clone()
    }

    async fn refresh(&mut self) {
        let client = BasicClient::new(
            self.client_id.clone(),
            Some(self.client_secret.clone()),
            self.auth_url.clone(),
            Some(self.token_url.clone()),
        );

        let response = client
            .exchange_refresh_token(&self.refresh_token)
            .request_async(async_http_client)
            .await
            .unwrap();

        self.access_token = response.access_token().clone();
    }
}

#[derive(Clone)]
pub struct SecretHandler {
    manager: Arc<Mutex<dyn SecretManager + Sync + Send>>,
    secret_type: SecretType,
}

impl SecretHandler {
    pub fn new(manager: impl SecretManager + Sync + Send + 'static) -> Self {
        Self {
            secret_type: manager.secret_type(),
            manager: Arc::new(Mutex::new(manager)),
        }
    }
}

#[async_trait]
impl SecretManager for SecretHandler {
    fn secret_type(&self) -> SecretType {
        self.secret_type
    }

    async fn secret(&self) -> String {
        self.manager.lock().await.secret().await
    }

    async fn refresh(&mut self) {
        self.manager.lock().await.refresh().await;
    }
}