Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrappers for SecItemDelete and SecItemUpdate #210

Merged

Conversation

tsoutsman
Copy link
Contributor

Thank you for the great library!

This PR adds delete_item, update_item and ItemUpdateOptions. The functions reuse SearchItemParams to identify which items to modify and so I've also added a to_dictionary method to SearchItemParams. In fact, it would probably make sense to replace
SearchItemParams::search with a search_item function in line with the add/update/delete API, but I didn't want to make unnecessary changes.

Also in the spirit of minimising changes to existing code:

  • I added a slightly strange API to ItemUpdateOptions::set_class to allow for rewriting the secret data without overwriting the class. Alternatively, I could add an ItemUpdateValue that is the same as ItemAddValue, but doesn't take a class.
  • ItemUpdateOptions is basically a duplicate of ItemAddOptions. I could instead define struct ItemAddOptions(ItemUpdateOptions) and then just forward all the methods.

Thank you for the great library!

This PR adds `delete_item`, `update_item` and `ItemUpdateOptions`. The
functions reuse `SearchItemParams` to identify which items to modify
and so I've also added a `to_dictionary` method to `SearchItemParams`.
In fact, it would probably make sense to replace
`SearchItemParams::search` with a `search_item` function in line with
the add/update/delete API, but I didn't want to make unnecessary
changes.

Also in the spirit of minimising changes to existing code:
- I added a slightly strange API to `ItemUpdateOptions::set_class` to
  allow for rewriting the secret data without overwriting the class.
  Alternatively, I could add an `ItemUpdateValue` that is the same as
  `ItemAddValue`, but doesn't take a class.
- `ItemUpdateOptions` is basically a duplicate of `ItemAddOptions`. I
  could instead define `struct ItemAddOptions(ItemUpdateOptions)` and
  then just forward all the methods.

Signed-off-by: Klim Tsoutsman <[email protected]>
security-framework/src/item.rs Outdated Show resolved Hide resolved
/// This overrides the class provided by `value`.
///
/// If set to `None`, `value`'s class is ignored, and not added to the `CFDictionary`.
pub class: Option<Option<ItemClass>>,
Copy link
Owner

@kornelski kornelski Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option in option is weird.

Can you explicitly contrast in the docs what's the difference between None Some(None)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option in option is weird.

I've added an ItemUpdateValue instead that removes the need for the Option<Option<_>>

@@ -762,6 +917,20 @@ pub fn add_item(add_params: CFDictionary) -> Result<()> {
cvt(unsafe { SecItemAdd(add_params.as_concrete_TypeRef(), std::ptr::null_mut()) })
}

/// Translates to `SecItemUpdate`. Use `ItemSearchOptions` to build a `search_params` `CFDictionary`
/// and `ItemUpdateOptions` to build an `update_params` `CFDictionary`.
pub fn update_item(search_params: CFDictionary, update_params: CFDictionary) -> Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to expose bare ObjC types in the high-level Rust wrapper (existing APIs that do that are bad too)

Copy link
Contributor Author

@tsoutsman tsoutsman Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I also bring add_item into ItemAddOptions::add_item and deprecate/remove the old add_item? I could also make ItemAddOptions::to_dictionary private/deprecated?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about add_item, unless you want to. I don't want to throw unrelated refactorings at you.

Here take &ItemSearchOptions and &ItemUpdateOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured I may as well deprecate add_item and create an ItemAddOptions::add method. Also an ItemSearchOptions::delete instead of delete_item.

Signed-off-by: Klim Tsoutsman <[email protected]>
@kornelski kornelski merged commit 464f8a3 into kornelski:main Sep 1, 2024
6 checks passed
@kornelski
Copy link
Owner

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants