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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Module dedicated to OAuth 2.0 configuration.
//!
//! This module contains everything related to OAuth 2.0
//! configuration.

use log::warn;
use oauth::v2_0::{AuthorizationCodeGrant, Client, RefreshAccessToken};
use secret::Secret;
use std::{io, vec};
use thiserror::Error;

use crate::Result;

/// Errors related to OAuth 2.0 configuration.
#[derive(Debug, Error)]
pub enum Error {
    #[error("cannot create oauth2 client")]
    InitClientError(#[source] oauth::v2_0::Error),
    #[error("cannot create oauth2 client")]
    BuildClientError(#[source] oauth::v2_0::Error),
    #[error("cannot wait for oauth2 redirection error")]
    WaitForRedirectionError(#[source] oauth::v2_0::Error),

    #[error("cannot get oauth2 access token from global keyring")]
    GetAccessTokenError(#[source] secret::Error),
    #[error("cannot set oauth2 access token")]
    SetAccessTokenError(#[source] secret::Error),
    #[error("cannot refresh oauth2 access token")]
    RefreshAccessTokenError(#[source] oauth::v2_0::Error),
    #[error("cannot delete oauth2 access token from global keyring")]
    DeleteAccessTokenError(#[source] secret::Error),

    #[error("cannot get oauth2 refresh token")]
    GetRefreshTokenError(#[source] secret::Error),
    #[error("cannot set oauth2 refresh token")]
    SetRefreshTokenError(#[source] secret::Error),
    #[error("cannot delete oauth2 refresh token from global keyring")]
    DeleteRefreshTokenError(#[source] secret::Error),

    #[error("cannot get oauth2 client secret from user")]
    GetClientSecretFromUserError(#[source] io::Error),
    #[error("cannot get oauth2 client secret from global keyring")]
    GetClientSecretFromKeyringError(#[source] secret::Error),
    #[error("cannot save oauth2 client secret into global keyring")]
    SetClientSecretIntoKeyringError(#[source] secret::Error),
    #[error("cannot delete oauth2 client secret from global keyring")]
    DeleteClientSecretError(#[source] secret::Error),
}

/// The OAuth 2.0 configuration.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OAuth2Config {
    /// Method for presenting an OAuth 2.0 bearer token to a service
    /// for authentication.
    pub method: OAuth2Method,

    /// Client identifier issued to the client during the registration process described by
    /// [Section 2.2](https://datatracker.ietf.org/doc/html/rfc6749#section-2.2).
    pub client_id: String,

    /// Client password issued to the client during the registration process described by
    /// [Section 2.2](https://datatracker.ietf.org/doc/html/rfc6749#section-2.2).
    pub client_secret: Secret,

    /// URL of the authorization server's authorization endpoint.
    pub auth_url: String,

    /// URL of the authorization server's token endpoint.
    pub token_url: String,

    /// Access token returned by the token endpoint and used to access
    /// protected resources.
    pub access_token: Secret,

    /// Refresh token used to obtain a new access token (if supported
    /// by the authorization server).
    pub refresh_token: Secret,

    /// Enable the [PKCE](https://datatracker.ietf.org/doc/html/rfc7636) protection.
    /// The value must have a minimum length of 43 characters and a maximum length of 128 characters.
    /// Each character must be ASCII alphanumeric or one of the characters “-” / “.” / “_” / “~”.
    pub pkce: bool,

    /// Access token scope(s), as defined by the authorization server.
    pub scopes: OAuth2Scopes,

    /// Host name of the client's redirection endpoint.
    pub redirect_host: String,

    /// Host port of the client's redirection endpoint.
    pub redirect_port: u16,
}

impl Default for OAuth2Config {
    fn default() -> Self {
        Self {
            method: Default::default(),
            client_id: Default::default(),
            client_secret: Default::default(),
            auth_url: Default::default(),
            token_url: Default::default(),
            access_token: Default::default(),
            refresh_token: Default::default(),
            pkce: Default::default(),
            scopes: Default::default(),
            redirect_host: Self::default_redirect_host(),
            redirect_port: Self::default_redirect_port(),
        }
    }
}

impl OAuth2Config {
    /// Returns the default redirect host name. Combines well with
    /// serde's `default` and `skip_serializing_if` macros.
    pub fn default_redirect_host() -> String {
        String::from("localhost")
    }

    /// Returns the default redirect host port. Combines well with
    /// serde's `default` and `skip_serializing_if` macros.
    pub fn default_redirect_port() -> u16 {
        9999
    }

    /// Resets the three secrets of the OAuth 2.0 configuration.
    pub fn reset(&self) -> Result<()> {
        self.client_secret
            .delete_keyring_entry_secret()
            .map_err(Error::DeleteClientSecretError)?;
        self.access_token
            .delete_keyring_entry_secret()
            .map_err(Error::DeleteAccessTokenError)?;
        self.refresh_token
            .delete_keyring_entry_secret()
            .map_err(Error::DeleteRefreshTokenError)?;
        Ok(())
    }

    /// If the access token is not defined, runs the authorization
    /// code grant OAuth 2.0 flow in order to save the acces token and
    /// the refresh token if present.
    pub async fn configure(
        &self,
        get_client_secret: impl Fn() -> io::Result<String>,
    ) -> Result<()> {
        if self.access_token.get().await.is_ok() {
            return Ok(());
        }

        let set_client_secret = || {
            self.client_secret
                .set_keyring_entry_secret(
                    get_client_secret().map_err(Error::GetClientSecretFromUserError)?,
                )
                .map_err(Error::SetClientSecretIntoKeyringError)
        };

        let client_secret = match self.client_secret.find().await {
            Ok(None) => {
                warn!("cannot find oauth2 client secret from keyring, setting it");
                set_client_secret()
            }
            Ok(Some(client_secret)) => Ok(client_secret),
            Err(err) => Err(Error::GetClientSecretFromKeyringError(err)),
        }?;

        let client = Client::new(
            self.client_id.clone(),
            client_secret,
            self.auth_url.clone(),
            self.token_url.clone(),
        )
        .map_err(Error::InitClientError)?
        .with_redirect_host(self.redirect_host.clone())
        .with_redirect_port(self.redirect_port)
        .build()
        .map_err(Error::BuildClientError)?;

        let mut auth_code_grant = AuthorizationCodeGrant::new()
            .with_redirect_host(self.redirect_host.clone())
            .with_redirect_port(self.redirect_port);

        if self.pkce {
            auth_code_grant = auth_code_grant.with_pkce();
        }

        for scope in self.scopes.clone() {
            auth_code_grant = auth_code_grant.with_scope(scope);
        }

        let (redirect_url, csrf_token) = auth_code_grant.get_redirect_url(&client);

        println!("To complete your OAuth 2.0 setup, click on the following link:");
        println!("");
        println!("{}", redirect_url.to_string());

        let (access_token, refresh_token) = auth_code_grant
            .wait_for_redirection(&client, csrf_token)
            .await
            .map_err(Error::WaitForRedirectionError)?;

        self.access_token
            .set_keyring_entry_secret(access_token)
            .map_err(Error::SetAccessTokenError)?;

        if let Some(refresh_token) = &refresh_token {
            self.refresh_token
                .set_keyring_entry_secret(refresh_token)
                .map_err(Error::SetRefreshTokenError)?;
        }

        Ok(())
    }

    /// Runs the refresh access token OAuth 2.0 flow by exchanging a
    /// refresh token with a new pair of access/refresh token.
    pub async fn refresh_access_token(&self) -> Result<String> {
        let client_secret = self
            .client_secret
            .get()
            .await
            .map_err(Error::GetClientSecretFromKeyringError)?;

        let client = Client::new(
            self.client_id.clone(),
            client_secret,
            self.auth_url.clone(),
            self.token_url.clone(),
        )
        .map_err(Error::InitClientError)?
        .with_redirect_host(self.redirect_host.clone())
        .with_redirect_port(self.redirect_port)
        .build()
        .map_err(Error::BuildClientError)?;

        let refresh_token = self
            .refresh_token
            .get()
            .await
            .map_err(Error::GetRefreshTokenError)?;

        let (access_token, refresh_token) = RefreshAccessToken::new()
            .refresh_access_token(&client, refresh_token)
            .await
            .map_err(Error::RefreshAccessTokenError)?;

        self.access_token
            .set_keyring_entry_secret(&access_token)
            .map_err(Error::SetAccessTokenError)?;

        if let Some(refresh_token) = &refresh_token {
            self.refresh_token
                .set_keyring_entry_secret(refresh_token)
                .map_err(Error::SetRefreshTokenError)?;
        }

        Ok(access_token)
    }

    /// Returns the access token if existing, otherwise returns an
    /// error.
    pub async fn access_token(&self) -> Result<String> {
        let access_token = self
            .access_token
            .get()
            .await
            .map_err(Error::GetAccessTokenError)?;
        Ok(access_token)
    }
}

/// Method for presenting an OAuth 2.0 bearer token to a service for
/// authentication.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum OAuth2Method {
    #[default]
    XOAuth2,
    OAuthBearer,
}

/// Access token scope(s), as defined by the authorization server.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OAuth2Scopes {
    Scope(String),
    Scopes(Vec<String>),
}

impl Default for OAuth2Scopes {
    fn default() -> Self {
        Self::Scopes(Vec::new())
    }
}

impl IntoIterator for OAuth2Scopes {
    type Item = String;
    type IntoIter = vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Self::Scope(scope) => vec![scope].into_iter(),
            Self::Scopes(scopes) => scopes.into_iter(),
        }
    }
}