oauth2_doorkeeper/
authorization_code_grant.rs

1use core::marker::PhantomData;
2
3use oauth2_client::{
4    oauth2_core::types::Scope,
5    re_exports::{ClientId, ClientSecret, RedirectUri, Url, UrlParseError},
6    Provider, ProviderExtAuthorizationCodeGrant,
7};
8
9#[derive(Debug, Clone)]
10pub struct DoorkeeperProviderWithAuthorizationCodeFlow<SCOPE>
11where
12    SCOPE: Scope,
13{
14    client_id: ClientId,
15    client_secret: ClientSecret,
16    redirect_uri: RedirectUri,
17    //
18    token_endpoint_url: Url,
19    authorization_endpoint_url: Url,
20    //
21    phantom: PhantomData<SCOPE>,
22}
23impl<SCOPE> DoorkeeperProviderWithAuthorizationCodeFlow<SCOPE>
24where
25    SCOPE: Scope,
26{
27    pub fn new(
28        client_id: ClientId,
29        client_secret: ClientSecret,
30        redirect_uri: RedirectUri,
31        token_url: impl AsRef<str>,
32        authorization_url: impl AsRef<str>,
33    ) -> Result<Self, UrlParseError> {
34        Ok(Self {
35            client_id,
36            client_secret,
37            redirect_uri,
38            token_endpoint_url: token_url.as_ref().parse()?,
39            authorization_endpoint_url: authorization_url.as_ref().parse()?,
40            phantom: PhantomData,
41        })
42    }
43}
44impl<SCOPE> Provider for DoorkeeperProviderWithAuthorizationCodeFlow<SCOPE>
45where
46    SCOPE: Scope,
47{
48    type Scope = SCOPE;
49
50    fn client_id(&self) -> Option<&ClientId> {
51        Some(&self.client_id)
52    }
53
54    fn client_secret(&self) -> Option<&ClientSecret> {
55        Some(&self.client_secret)
56    }
57
58    fn token_endpoint_url(&self) -> &Url {
59        &self.token_endpoint_url
60    }
61}
62impl<SCOPE> ProviderExtAuthorizationCodeGrant for DoorkeeperProviderWithAuthorizationCodeFlow<SCOPE>
63where
64    SCOPE: Scope,
65{
66    fn redirect_uri(&self) -> Option<&RedirectUri> {
67        Some(&self.redirect_uri)
68    }
69
70    fn authorization_endpoint_url(&self) -> &Url {
71        &self.authorization_endpoint_url
72    }
73}