Skip to content

Commit c6476ca

Browse files
committed
Make debug formatting useful on credentials.
As reported in hwchen#201, the existing debug print of credentials gives no information about the underlying platform credential. This fixes that.
1 parent 8f95997 commit c6476ca

7 files changed

+58
-5
lines changed

src/credential.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ if matches!(persistence, credential::CredentialPersistence::UntilDelete) {
2727
}
2828
```
2929
*/
30-
use super::Result;
3130
use std::any::Any;
3231

32+
use super::Result;
33+
3334
/// The API that [credentials](Credential) implement.
3435
pub trait CredentialApi {
3536
/// Set the credential's password (a string).
@@ -66,17 +67,30 @@ pub trait CredentialApi {
6667
/// can do platform-specific things with it (e.g.,
6768
/// query its attributes in the underlying store).
6869
fn as_any(&self) -> &dyn Any;
69-
}
7070

71-
impl std::fmt::Debug for Credential {
72-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73-
self.as_any().fmt(f)
71+
/// The Debug trait call for the object.
72+
///
73+
/// This is used to implement the Debug trait on this type; it
74+
/// allows generic code to provide debug printing as provided by
75+
/// the underlying concrete object.
76+
///
77+
/// We provide a (useless) default implementation for backward
78+
/// compatibility with existing implementors who may have not
79+
/// implemented the Debug trait for their credential objects
80+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
std::fmt::Debug::fmt(self.as_any(), f)
7482
}
7583
}
7684

7785
/// A thread-safe implementation of the [Credential API](CredentialApi).
7886
pub type Credential = dyn CredentialApi + Send + Sync;
7987

88+
impl std::fmt::Debug for Credential {
89+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90+
self.debug_fmt(f)
91+
}
92+
}
93+
8094
/// A descriptor for the lifetime of stored credentials, returned from
8195
/// a credential store's [persistence](CredentialBuilderApi::persistence) call.
8296
#[non_exhaustive]

src/ios.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ wildcards when looking up credentials by attribute value.)
1616
On iOS, the target parameter is ignored, because there is only one keychain
1717
that can be targeted to store a generic credential.
1818
*/
19+
1920
use security_framework::base::Error;
2021
use security_framework::passwords::{
2122
delete_generic_password, get_generic_password, set_generic_password,
@@ -87,6 +88,11 @@ impl CredentialApi for IosCredential {
8788
fn as_any(&self) -> &dyn std::any::Any {
8889
self
8990
}
91+
92+
/// Expose the concrete debug formatter for use via the [Credential] trait
93+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94+
std::fmt::Debug::fmt(self, f)
95+
}
9096
}
9197

9298
impl IosCredential {

src/keyutils.rs

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Alternatively, you can drop the secret-service credential store altogether
9797
with `--no-default-features` and `--features linux-no-secret-service`.
9898
9999
*/
100+
100101
use super::credential::{
101102
Credential, CredentialApi, CredentialBuilder, CredentialBuilderApi, CredentialPersistence,
102103
};
@@ -223,6 +224,11 @@ impl CredentialApi for KeyutilsCredential {
223224
fn as_any(&self) -> &dyn std::any::Any {
224225
self
225226
}
227+
228+
/// Expose the concrete debug formatter for use via the [Credential] trait
229+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
230+
std::fmt::Debug::fmt(self, f)
231+
}
226232
}
227233

228234
impl KeyutilsCredential {

src/macos.rs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ name as the target parameter to `Entry::new_with_target`.
2727
Any name other than one of the OS-supplied keychains (User, Common, System, and Dynamic)
2828
will be mapped to `User`.
2929
*/
30+
3031
use security_framework::base::Error;
3132
use security_framework::os::macos::keychain::{SecKeychain, SecPreferencesDomain};
3233
use security_framework::os::macos::passwords::find_generic_password;
@@ -110,6 +111,11 @@ impl CredentialApi for MacCredential {
110111
fn as_any(&self) -> &dyn std::any::Any {
111112
self
112113
}
114+
115+
/// Expose the concrete debug formatter for use via the [Credential] trait
116+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117+
std::fmt::Debug::fmt(self, f)
118+
}
113119
}
114120

115121
impl MacCredential {

src/mock.rs

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ impl CredentialApi for MockCredential {
174174
fn as_any(&self) -> &dyn std::any::Any {
175175
self
176176
}
177+
178+
/// Expose the concrete debug formatter for use via the [Credential] trait
179+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180+
std::fmt::Debug::fmt(self, f)
181+
}
177182
}
178183

179184
impl MockCredential {

src/secret_service.rs

+10
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ impl CredentialApi for SsCredential {
196196
fn as_any(&self) -> &dyn std::any::Any {
197197
self
198198
}
199+
200+
/// Expose the concrete debug formatter for use via the [Credential] trait
201+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202+
std::fmt::Debug::fmt(self, f)
203+
}
199204
}
200205

201206
impl SsCredential {
@@ -361,6 +366,11 @@ impl CredentialBuilderApi for SsCredentialBuilder {
361366
fn as_any(&self) -> &dyn std::any::Any {
362367
self
363368
}
369+
370+
/// Expose the concrete debug formatter for use via the [Credential] trait
371+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
372+
std::fmt::Debug::fmt(self, f)
373+
}
364374
}
365375

366376
//

src/windows.rs

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ the order in which they were made. Careful testing has
2929
shown that modifying the same entry in the same (almost simultaneous) order from
3030
different threads produces different results on different runs.
3131
*/
32+
3233
use byteorder::{ByteOrder, LittleEndian};
3334
use std::iter::once;
3435
use std::mem::MaybeUninit;
@@ -166,6 +167,11 @@ impl CredentialApi for WinCredential {
166167
fn as_any(&self) -> &dyn std::any::Any {
167168
self
168169
}
170+
171+
/// Expose the concrete debug formatter for use via the [Credential] trait
172+
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173+
std::fmt::Debug::fmt(self, f)
174+
}
169175
}
170176

171177
impl WinCredential {

0 commit comments

Comments
 (0)