auth0_integration/services/
auth0_client_token.rs1use serde::Serialize;
2
3use crate::{config::Auth0Config, error::AppError, models::AccessTokenResponse, services::HttpClient};
4
5#[derive(Debug, Serialize)]
6struct ClientCredentialsRequest<'a> {
7 grant_type: &'a str,
8 client_id: &'a str,
9 client_secret: &'a str,
10 audience: &'a str,
11}
12
13pub struct Auth0ClientToken {
14 http: HttpClient,
15 config: Auth0Config,
16}
17
18impl Auth0ClientToken {
19 pub fn new(config: &Auth0Config) -> Self {
20 let http = HttpClient::new(config);
21 Self { http, config: config.clone() }
22 }
23
24 pub async fn get_access_token(&self) -> Result<AccessTokenResponse, AppError> {
26 let body = ClientCredentialsRequest {
27 grant_type: "client_credentials",
28 client_id: &self.config.auth0_client_id,
29 client_secret: &self.config.auth0_client_secret,
30 audience: &self.config.auth0_audience,
31 };
32
33 let res = self.http.post("/oauth/token", &body).await?;
34
35 if !res.status().is_success() {
36 let text = res.text().await.unwrap_or_default();
37 return Err(AppError::Auth0(text));
38 }
39
40 Ok(res.json::<AccessTokenResponse>().await?)
41 }
42}