Skip to content

Commit

Permalink
Support account name, comment, description, service when adding items…
Browse files Browse the repository at this point in the history
… to the keychain
  • Loading branch information
grahamc authored and kornelski committed Apr 2, 2024
1 parent 39e64d8 commit 588c313
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions security-framework-sys/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ extern "C" {
pub static kSecAttrAccessGroupToken: CFStringRef;

pub static kSecAttrAuthenticationType: CFStringRef;
pub static kSecAttrDescription: CFStringRef;
pub static kSecAttrPath: CFStringRef;
pub static kSecAttrPort: CFStringRef;
pub static kSecAttrProtocol: CFStringRef;
Expand Down
47 changes: 45 additions & 2 deletions security-framework/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,39 @@ impl SearchResult {
pub struct ItemAddOptions {
/// The value (by ref or data) of the item to add, required.
pub value: ItemAddValue,
/// Optional kSecAttrAccount attribute.
pub account_name: Option<String>,
/// Optional kSecAttrComment attribute.
pub comment: Option<String>,
/// Optional kSecAttrDescription attribute.
pub description: Option<String>,
/// Optional kSecAttrLabel attribute.
pub label: Option<String>,
/// Optional kSecAttrService attribute.
pub service: Option<String>,
/// Optional keychain location.
pub location: Option<Location>,
}

impl ItemAddOptions {
/// Specifies the item to add.
#[must_use] pub fn new(value: ItemAddValue) -> Self {
Self{ value, label: None, location: None }
Self{ value, label: None, location: None, service: None, account_name: None, comment: None, description: None }
}
/// Specifies the `kSecAttrAccount` attribute.
pub fn set_account_name(&mut self, account_name: impl Into<String>) -> &mut Self {
self.account_name = Some(account_name.into());
self
}
/// Specifies the `kSecAttrComment` attribute.
pub fn set_comment(&mut self, comment: impl Into<String>) -> &mut Self {
self.comment = Some(comment.into());
self
}
/// Specifies the `kSecAttrDescription` attribute.
pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
self.description = Some(description.into());
self
}
/// Specifies the `kSecAttrLabel` attribute.
pub fn set_label(&mut self, label: impl Into<String>) -> &mut Self {
Expand All @@ -557,6 +580,11 @@ impl ItemAddOptions {
self.location = Some(location);
self
}
/// Specifies the `kSecAttrService` attribute.
pub fn set_service(&mut self, service: impl Into<String>) -> &mut Self {
self.service = Some(service.into());
self
}
/// Populates a `CFDictionary` to be passed to
pub fn to_dictionary(&self) -> CFDictionary {
let mut dict = CFMutableDictionary::from_CFType_pairs(&[]);
Expand Down Expand Up @@ -592,11 +620,26 @@ impl ItemAddOptions {
},
}
}

let account_name = self.account_name.as_deref().map(CFString::from);
if let Some(account_name) = &account_name {
dict.add(&unsafe { kSecAttrAccount }.to_void(), &account_name.to_void());
}
let comment = self.comment.as_deref().map(CFString::from);
if let Some(comment) = &comment {
dict.add(&unsafe { kSecAttrDescription }.to_void(), &comment.to_void());
}
let description = self.description.as_deref().map(CFString::from);
if let Some(description) = &description {
dict.add(&unsafe { kSecAttrDescription }.to_void(), &description.to_void());
}
let label = self.label.as_deref().map(CFString::from);
if let Some(label) = &label {
dict.add(&unsafe { kSecAttrLabel }.to_void(), &label.to_void());
}
let service = self.service.as_deref().map(CFString::from);
if let Some(service) = &service {
dict.add(&unsafe { kSecAttrService }.to_void(), &service.to_void());
}

dict.to_immutable()
}
Expand Down

0 comments on commit 588c313

Please sign in to comment.