Skip to content

Commit

Permalink
Merge pull request #188 from tmpfs/ios-secret-impls
Browse files Browse the repository at this point in the history
Add set_secret() and get_secret() for iOS impl.

Thanks @tmpfs for fixing #188!!
  • Loading branch information
brotskydotcom authored Jul 13, 2024
2 parents 808e8c7 + 060b6c4 commit 58c11f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Thanks to the following for helping make this library better, whether through co
- @steveatinfincia
- @Sytten
- @thewh1teagle
- @tmpfs
- @VorpalBlade
- @zschreur

Expand Down
2 changes: 1 addition & 1 deletion examples/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn test_update_password() {

#[cfg(target_os = "ios")]
fn test_get_credential() {
use keyring::default::IosCredential;
use keyring::ios::IosCredential;
let name = "test_get_credential".to_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let credential: &IosCredential = entry
Expand Down
26 changes: 21 additions & 5 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ impl CredentialApi for IosCredential {
/// Since there is only one credential with a given _account_ and _user_
/// in any given keychain, there is no chance of ambiguity.
fn set_password(&self, password: &str) -> Result<()> {
set_generic_password(&self.service, &self.account, password.as_bytes())
.map_err(decode_error)?;
self.set_secret(password.as_bytes())?;
Ok(())
}

/// Create and write a credential with secret for this entry.
///
/// The new credential replaces any existing one in the store.
/// Since there is only one credential with a given _account_ and _user_
/// in any given keychain, there is no chance of ambiguity.
fn set_secret(&self, secret: &[u8]) -> Result<()> {
set_generic_password(&self.service, &self.account, secret).map_err(decode_error)?;
Ok(())
}

Expand All @@ -52,9 +61,16 @@ impl CredentialApi for IosCredential {
/// Returns a [NoEntry](ErrorCode::NoEntry) error if there is no
/// credential in the store.
fn get_password(&self) -> Result<String> {
let password_bytes =
get_generic_password(&self.service, &self.account).map_err(decode_error)?;
decode_password(password_bytes.to_vec())
let password_bytes = self.get_secret()?;
decode_password(password_bytes)
}

/// Look up the secret for this entry, if any.
///
/// Returns a [NoEntry](ErrorCode::NoEntry) error if there is no
/// credential in the store.
fn get_secret(&self) -> Result<Vec<u8>> {
get_generic_password(&self.service, &self.account).map_err(decode_error)
}

/// Delete the underlying generic credential for this entry, if any.
Expand Down

0 comments on commit 58c11f3

Please sign in to comment.