1use std::collections::BTreeMap;
26use std::fmt::{self, Debug, Formatter};
27
28use super::credential::{
29 Credential, HttpAuth, HttpCredentials, OAuth2Auth, ServiceAccount, ServiceAccountCredential,
30};
31use super::schemes::{OAuthFlow, OAuthFlows, Scheme};
32use super::tool::{Config, ToolArguments};
33
34const REDACTED: &str = "<redacted>";
36
37struct Secret<'a>(&'a str);
39
40impl Debug for Secret<'_> {
41 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42 let shown = if self.0.is_empty() { "" } else { REDACTED };
43 Debug::fmt(shown, f)
44 }
45}
46
47struct RedactedValues<'a, V>(&'a BTreeMap<String, V>);
49
50impl<V> Debug for RedactedValues<'_, V> {
51 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
52 f.debug_map()
53 .entries(self.0.keys().map(|k| (k, REDACTED)))
54 .finish()
55 }
56}
57
58impl Debug for OAuthFlow {
59 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60 f.debug_struct("OAuthFlow")
61 .field("authorization_url", &self.authorization_url)
62 .field("token_url", &self.token_url)
63 .field("refresh_url", &self.refresh_url)
64 .field("scopes", &self.scopes)
65 .field("extra", &self.extra)
66 .finish()
67 }
68}
69
70impl Debug for OAuthFlows {
71 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
72 f.debug_struct("OAuthFlows")
73 .field("implicit", &self.implicit)
74 .field("password", &self.password)
75 .field("client_credentials", &self.client_credentials)
76 .field("authorization_code", &self.authorization_code)
77 .field("extra", &self.extra)
78 .finish()
79 }
80}
81
82impl Debug for Scheme {
83 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
84 f.debug_struct("Scheme")
85 .field("type", &self.r#type)
86 .field("description", &self.description)
87 .field("in", &self.r#in)
88 .field("name", &self.name)
89 .field("scheme", &self.scheme)
90 .field("bearer_format", &self.bearer_format)
91 .field("flows", &self.flows)
92 .field("issuer_url", &self.issuer_url)
93 .field("open_id_connect_url", &self.open_id_connect_url)
94 .field("authorization_endpoint", &self.authorization_endpoint)
95 .field("token_endpoint", &self.token_endpoint)
96 .field("userinfo_endpoint", &self.userinfo_endpoint)
97 .field("revocation_endpoint", &self.revocation_endpoint)
98 .field(
99 "token_endpoint_auth_methods_supported",
100 &self.token_endpoint_auth_methods_supported,
101 )
102 .field("grant_types_supported", &self.grant_types_supported)
103 .field("scopes", &self.scopes)
104 .field("extra", &self.extra)
105 .finish()
106 }
107}
108
109impl Debug for HttpCredentials {
111 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
112 f.debug_struct("HttpCredentials")
113 .field("username", &self.username)
114 .field("password", &Secret(&self.password))
115 .field("token", &Secret(&self.token))
116 .field("extra", &RedactedValues(&self.extra))
117 .finish()
118 }
119}
120
121impl Debug for HttpAuth {
123 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
124 f.debug_struct("HttpAuth")
125 .field("scheme", &self.scheme)
126 .field("credentials", &self.credentials)
127 .field(
128 "additional_headers",
129 &self.additional_headers.as_ref().map(RedactedValues),
130 )
131 .field("extra", &RedactedValues(&self.extra))
132 .finish()
133 }
134}
135
136impl Debug for OAuth2Auth {
139 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
140 f.debug_struct("OAuth2Auth")
141 .field("client_id", &self.client_id)
142 .field("client_secret", &Secret(&self.client_secret))
143 .field("auth_uri", &self.auth_uri)
144 .field("nonce", &self.nonce)
145 .field("state", &self.state)
146 .field("redirect_uri", &self.redirect_uri)
147 .field("auth_response_uri", &Secret(&self.auth_response_uri))
148 .field("auth_code", &Secret(&self.auth_code))
149 .field("access_token", &Secret(&self.access_token))
150 .field("refresh_token", &Secret(&self.refresh_token))
151 .field("id_token", &Secret(&self.id_token))
152 .field("expires_at", &self.expires_at)
153 .field("expires_in", &self.expires_in)
154 .field("audience", &self.audience)
155 .field("prompt", &self.prompt)
156 .field("code_verifier", &Secret(&self.code_verifier))
157 .field("code_challenge_method", &self.code_challenge_method)
158 .field(
159 "token_endpoint_auth_method",
160 &self.token_endpoint_auth_method,
161 )
162 .field("extra", &RedactedValues(&self.extra))
163 .finish()
164 }
165}
166
167impl Debug for ServiceAccountCredential {
170 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
171 f.debug_struct("ServiceAccountCredential")
172 .field("type", &self.r#type)
173 .field("project_id", &self.project_id)
174 .field("private_key_id", &Secret(&self.private_key_id))
175 .field("private_key", &Secret(&self.private_key))
176 .field("client_email", &self.client_email)
177 .field("client_id", &self.client_id)
178 .field("auth_uri", &self.auth_uri)
179 .field("token_uri", &self.token_uri)
180 .field(
181 "auth_provider_x509_cert_url",
182 &self.auth_provider_x509_cert_url,
183 )
184 .field("client_x509_cert_url", &self.client_x509_cert_url)
185 .field("universe_domain", &self.universe_domain)
186 .field("extra", &RedactedValues(&self.extra))
187 .finish()
188 }
189}
190
191impl Debug for ServiceAccount {
193 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
194 f.debug_struct("ServiceAccount")
195 .field(
196 "service_account_credential",
197 &self.service_account_credential,
198 )
199 .field("scopes", &self.scopes)
200 .field("use_default_credential", &self.use_default_credential)
201 .field("use_id_token", &self.use_id_token)
202 .field("audience", &self.audience)
203 .field("extra", &RedactedValues(&self.extra))
204 .finish()
205 }
206}
207
208impl Debug for Credential {
211 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
212 f.debug_struct("Credential")
213 .field("auth_type", &self.auth_type)
214 .field("resource_ref", &self.resource_ref)
215 .field("api_key", &Secret(&self.api_key))
216 .field("http", &self.http)
217 .field("service_account", &self.service_account)
218 .field("oauth2", &self.oauth2)
219 .field("extra", &RedactedValues(&self.extra))
220 .finish()
221 }
222}
223
224impl Debug for Config {
226 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
227 f.debug_struct("Config")
228 .field("auth_scheme", &self.auth_scheme)
229 .field("raw_auth_credential", &self.raw_auth_credential)
230 .field("exchanged_auth_credential", &self.exchanged_auth_credential)
231 .field("credential_key", &self.credential_key)
232 .field("extra", &RedactedValues(&self.extra))
233 .finish()
234 }
235}
236
237impl Debug for ToolArguments {
239 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
240 f.debug_struct("ToolArguments")
241 .field("function_call_id", &self.function_call_id)
242 .field("auth_config", &self.auth_config)
243 .field("extra", &RedactedValues(&self.extra))
244 .finish()
245 }
246}