Skip to main content

openauth_oauth/oauth2/
client_credentials_token.rs

1use super::error::OAuthError;
2use super::request::{
3    apply_client_authentication, post_form, ClientAuthentication, OAuthFormRequest,
4};
5use super::tokens::{get_oauth2_tokens, get_primary_client_id, OAuth2Tokens, ProviderOptions};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct ClientCredentialsTokenRequest {
9    pub options: ProviderOptions,
10    pub scope: Option<String>,
11    pub authentication: ClientAuthentication,
12    pub resource: Vec<String>,
13}
14
15impl Default for ClientCredentialsTokenRequest {
16    fn default() -> Self {
17        Self {
18            options: ProviderOptions::default(),
19            scope: None,
20            authentication: ClientAuthentication::Post,
21            resource: Vec::new(),
22        }
23    }
24}
25
26impl ClientCredentialsTokenRequest {
27    pub fn try_new(options: ProviderOptions) -> Result<Self, OAuthError> {
28        get_primary_client_id(&options.client_id).ok_or(OAuthError::MissingOption("client_id"))?;
29        options
30            .client_secret
31            .as_deref()
32            .filter(|secret| !secret.is_empty())
33            .ok_or(OAuthError::MissingOption("client_secret"))?;
34        Ok(Self {
35            options,
36            ..Self::default()
37        })
38    }
39
40    pub fn scope(mut self, scope: impl Into<String>) -> Self {
41        self.scope = Some(scope.into());
42        self
43    }
44
45    pub fn authentication(mut self, authentication: ClientAuthentication) -> Self {
46        self.authentication = authentication;
47        self
48    }
49
50    pub fn resource(mut self, resource: impl Into<String>) -> Self {
51        self.resource.push(resource.into());
52        self
53    }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct ClientCredentialsGrant {
58    pub token_endpoint: String,
59    pub request: ClientCredentialsTokenRequest,
60}
61
62pub fn create_client_credentials_token_request(
63    input: ClientCredentialsTokenRequest,
64) -> Result<OAuthFormRequest, OAuthError> {
65    let mut request = OAuthFormRequest::new();
66    request.set_body("grant_type", "client_credentials");
67    if let Some(scope) = input.scope {
68        request.set_body("scope", scope);
69    }
70    for resource in input.resource {
71        request.push_body("resource", resource);
72    }
73    get_primary_client_id(&input.options.client_id)
74        .ok_or(OAuthError::MissingOption("client_id"))?;
75    input
76        .options
77        .client_secret
78        .as_deref()
79        .filter(|secret| !secret.is_empty())
80        .ok_or(OAuthError::MissingOption("client_secret"))?;
81    apply_client_authentication(&mut request, &input.options, input.authentication, true)?;
82    Ok(request)
83}
84
85pub fn client_credentials_token_request(
86    input: ClientCredentialsTokenRequest,
87) -> Result<OAuthFormRequest, OAuthError> {
88    create_client_credentials_token_request(input)
89}
90
91pub async fn client_credentials_token(
92    input: ClientCredentialsGrant,
93) -> Result<OAuth2Tokens, OAuthError> {
94    let request = client_credentials_token_request(input.request)?;
95    let data = post_form(&input.token_endpoint, request).await?;
96    get_oauth2_tokens(data)
97}