oauth2_microsoft/
authorization_code_grant.rs

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