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
|
use oauth2::{
AuthUrl, ClientId, ClientSecret, CsrfToken, EndpointNotSet, EndpointSet, RedirectUrl, Scope,
TokenUrl,
};
use secrecy::{ExposeSecret, SecretString};
use tracing::debug;
use url::Url;
use crate::{AuthServiceError, Provider};
#[derive(Debug, Clone)]
pub struct OauthClient(
oauth2::basic::BasicClient<
EndpointSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointSet,
>,
);
#[derive(Debug)]
pub struct ClientConfig {
client_id: String,
client_secret: SecretString,
token_url: Url,
auth_url: Url,
}
impl ClientConfig {
pub fn new(
client_id: String,
client_secret: SecretString,
token_url: Url,
auth_url: Url,
) -> Self {
Self {
client_id,
client_secret,
token_url,
auth_url,
}
}
}
impl TryFrom<&ClientConfig> for OauthClient {
type Error = AuthServiceError;
fn try_from(value: &ClientConfig) -> Result<Self, Self::Error> {
debug!("creating oauth client");
Ok(Self(
oauth2::basic::BasicClient::new(ClientId::new(value.client_id.to_string()))
.set_client_secret(ClientSecret::new(
value.client_secret.expose_secret().to_string(),
))
.set_auth_uri(AuthUrl::from_url(value.auth_url.to_owned()))
.set_token_uri(TokenUrl::from_url(value.token_url.to_owned())),
))
}
}
impl OauthClient {
#[must_use]
pub fn with_redirect_url(self, url: &Url) -> Self {
Self(
self.0
.set_redirect_uri(RedirectUrl::from_url(url.to_owned())),
)
}
pub fn url_token(&self, provider: Provider) -> (Url, CsrfToken) {
let req = self.0.authorize_url(CsrfToken::new_random);
match provider {
Provider::Discord => req.add_scope(Scope::new("identify".to_string())),
}
.url()
}
}
|