babelforce_manager_sdk/resources/
auth.rs1use crate::error::{map_auth_err, ManagerError};
2use crate::gen::auth::apis::configuration::Configuration;
3use crate::gen::auth::apis::o_auth_api as api;
4use crate::gen::auth::models;
5use crate::retry::{with_retry, RetryPolicy};
6use crate::token::SharedCfg;
7
8pub struct AuthResource {
13 pub(crate) cfg: SharedCfg<Configuration>,
14 pub(crate) retry: RetryPolicy,
15}
16
17#[derive(Clone, Default)]
25pub struct TokenRequest {
26 pub code: Option<String>,
28 pub redirect_uri: Option<String>,
30 pub code_verifier: Option<String>,
32 pub refresh_token: Option<String>,
34 pub username: Option<String>,
36 pub password: Option<String>,
38 pub client_id: Option<String>,
40 pub client_secret: Option<String>,
42 pub scope: Option<String>,
44 pub access_type: Option<String>,
46}
47
48impl std::fmt::Debug for TokenRequest {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 let redact = |o: &Option<String>| o.as_ref().map(|_| "***");
52 f.debug_struct("TokenRequest")
53 .field("code", &redact(&self.code))
54 .field("redirect_uri", &self.redirect_uri)
55 .field("code_verifier", &redact(&self.code_verifier))
56 .field("refresh_token", &redact(&self.refresh_token))
57 .field("username", &self.username)
58 .field("password", &redact(&self.password))
59 .field("client_id", &self.client_id)
60 .field("client_secret", &redact(&self.client_secret))
61 .field("scope", &self.scope)
62 .field("access_type", &self.access_type)
63 .finish()
64 }
65}
66
67impl AuthResource {
68 pub async fn token(
71 &self,
72 grant_type: &str,
73 req: TokenRequest,
74 ) -> Result<models::OAuthTokenResponse, ManagerError> {
75 let cfg = self.cfg.get().await?;
76 let cfg = cfg.as_ref();
77 with_retry(&self.retry, false, || {
78 api::token(
79 cfg,
80 grant_type,
81 req.code.as_deref(),
82 req.redirect_uri.as_deref(),
83 req.code_verifier.as_deref(),
84 req.refresh_token.as_deref(),
85 req.username.as_deref(),
86 req.password.as_deref(),
87 req.client_id.as_deref(),
88 req.client_secret.as_deref(),
89 req.scope.as_deref(),
90 req.access_type.as_deref(),
91 )
92 })
93 .await
94 .map_err(map_auth_err)
95 }
96
97 pub async fn revoke(
99 &self,
100 token: &str,
101 token_type_hint: Option<&str>,
102 client_id: Option<&str>,
103 client_secret: Option<&str>,
104 ) -> Result<(), ManagerError> {
105 let cfg = self.cfg.get().await?;
106 let cfg = cfg.as_ref();
107 with_retry(&self.retry, false, || {
108 api::revoke(cfg, token, token_type_hint, client_id, client_secret)
109 })
110 .await
111 .map_err(map_auth_err)?;
112 Ok(())
113 }
114
115 #[allow(clippy::too_many_arguments)]
119 pub async fn authorize(
120 &self,
121 response_type: &str,
122 client_id: &str,
123 redirect_uri: &str,
124 scope: &str,
125 state: Option<&str>,
126 code_challenge: Option<&str>,
127 code_challenge_method: Option<&str>,
128 ) -> Result<String, ManagerError> {
129 let cfg = self.cfg.get().await?;
130 let cfg = cfg.as_ref();
131 with_retry(&self.retry, true, || {
132 api::authorize(
133 cfg,
134 response_type,
135 client_id,
136 redirect_uri,
137 scope,
138 state,
139 code_challenge,
140 code_challenge_method,
141 )
142 })
143 .await
144 .map_err(map_auth_err)
145 }
146}