Skip to main content

gadk_session/toolauth/
redact.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! `Debug` for the toolauth models, withholding secrets.
17//!
18//! The credential types print their secrets as `"<redacted>"`: the fields adk-python marks
19//! `repr=False` in auth_credential.py, every value in `extra` and every value in
20//! `additional_headers`, as its `BaseModelWithConfig.__repr_args__` does. Keys still show, and an
21//! empty secret prints as `""`. Nested values redact themselves. Only the printed text is
22//! redacted; the values keep their secrets. The scheme types hold no secret, so their `Debug`
23//! is what `#[derive(Debug)]` would print. `{:?}` and `{:#?}` are the only forms.
24
25use 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
34/// Printed in place of a secret that is set.
35const REDACTED: &str = "<redacted>";
36
37/// A non-empty string prints as `"<redacted>"`, an empty one as `""`.
38struct 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
47/// The map with every value printed as `"<redacted>"`, so the keys still show.
48struct 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
109/// Password, token and the values in extra are redacted.
110impl 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
121/// The values in additional headers and extra are redacted.
122impl 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
136/// Client secret, auth response URI, auth code, access, refresh and ID
137/// tokens, code verifier and the values in extra are redacted.
138impl 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
167/// Private key ID, private key and the values in extra are
168/// redacted.
169impl 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
191/// The values in extra are redacted; the key redacts itself.
192impl 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
208/// The API key and the values in extra are redacted; the nested
209/// credentials redact themselves.
210impl 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
224/// The values in extra are redacted; the credentials redact themselves.
225impl 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
237/// The values in extra are redacted; the config redacts itself.
238impl 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}