Skip to main content

oauth2_doorkeeper/
client_credentials_grant.rs

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