Skip to content

Commit

Permalink
Merge pull request #203 from brotskydotcom/issue-201
Browse files Browse the repository at this point in the history
Make the CLI more useful
  • Loading branch information
brotskydotcom authored Aug 7, 2024
2 parents de5ccdd + fc10542 commit 0b2ced7
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords = ["password", "credential", "keychain", "keyring", "cross-platform"]
license = "MIT OR Apache-2.0"
name = "keyring"
repository = "https://github.com/hwchen/keyring-rs.git"
version = "3.0.5"
version = "3.1.0"
rust-version = "1.75"
edition = "2021"
exclude = [".github/"]
Expand Down
2 changes: 1 addition & 1 deletion examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use keyring::{Entry, Error, Result};

fn main() {
let mut args: Cli = Cli::parse();
if args.user.is_empty() || args.user.eq_ignore_ascii_case("<logged-in username>") {
if args.user.eq_ignore_ascii_case("<logged-in username>") {
args.user = whoami::username()
}
let entry = match args.entry_for() {
Expand Down
24 changes: 19 additions & 5 deletions src/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ if matches!(persistence, credential::CredentialPersistence::UntilDelete) {
}
```
*/
use super::Result;
use std::any::Any;

use super::Result;

/// The API that [credentials](Credential) implement.
pub trait CredentialApi {
/// Set the credential's password (a string).
Expand Down Expand Up @@ -66,17 +67,30 @@ pub trait CredentialApi {
/// can do platform-specific things with it (e.g.,
/// query its attributes in the underlying store).
fn as_any(&self) -> &dyn Any;
}

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

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

impl std::fmt::Debug for Credential {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.debug_fmt(f)
}
}

/// A descriptor for the lifetime of stored credentials, returned from
/// a credential store's [persistence](CredentialBuilderApi::persistence) call.
#[non_exhaustive]
Expand Down
6 changes: 6 additions & 0 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ wildcards when looking up credentials by attribute value.)
On iOS, the target parameter is ignored, because there is only one keychain
that can be targeted to store a generic credential.
*/

use security_framework::base::Error;
use security_framework::passwords::{
delete_generic_password, get_generic_password, set_generic_password,
Expand Down Expand Up @@ -87,6 +88,11 @@ impl CredentialApi for IosCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl IosCredential {
Expand Down
6 changes: 6 additions & 0 deletions src/keyutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Alternatively, you can drop the secret-service credential store altogether
with `--no-default-features` and `--features linux-no-secret-service`.
*/

use super::credential::{
Credential, CredentialApi, CredentialBuilder, CredentialBuilderApi, CredentialPersistence,
};
Expand Down Expand Up @@ -223,6 +224,11 @@ impl CredentialApi for KeyutilsCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl KeyutilsCredential {
Expand Down
6 changes: 6 additions & 0 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ name as the target parameter to `Entry::new_with_target`.
Any name other than one of the OS-supplied keychains (User, Common, System, and Dynamic)
will be mapped to `User`.
*/

use security_framework::base::Error;
use security_framework::os::macos::keychain::{SecKeychain, SecPreferencesDomain};
use security_framework::os::macos::passwords::find_generic_password;
Expand Down Expand Up @@ -110,6 +111,11 @@ impl CredentialApi for MacCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl MacCredential {
Expand Down
5 changes: 5 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ impl CredentialApi for MockCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl MockCredential {
Expand Down
7 changes: 6 additions & 1 deletion src/secret_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ impl CredentialApi for SsCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl SsCredential {
Expand Down Expand Up @@ -266,7 +271,7 @@ impl SsCredential {
Ok(())
}

/// Map a function over all of the items matching this credential.
/// Map a function over the items matching this credential.
///
/// Items are unlocked before the function is applied.
///
Expand Down
6 changes: 6 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ the order in which they were made. Careful testing has
shown that modifying the same entry in the same (almost simultaneous) order from
different threads produces different results on different runs.
*/

use byteorder::{ByteOrder, LittleEndian};
use std::iter::once;
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -166,6 +167,11 @@ impl CredentialApi for WinCredential {
fn as_any(&self) -> &dyn std::any::Any {
self
}

/// Expose the concrete debug formatter for use via the [Credential] trait
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl WinCredential {
Expand Down

0 comments on commit 0b2ced7

Please sign in to comment.