Skip to content

Save password with "USER_PRESENCE" #251

@allevo

Description

@allevo

Hi!
I'm trying to insert a new password inside the keychain.
To do that, I wrote the following code:

    pub fn set_secret(service: &str, account: &str, secret: &[u8]) -> anyhow::Result<()> {
        let mut pwd_option = PasswordOptions::new_generic_password(service, account);
        pwd_option.set_access_control_options(AccessControlOptions::USER_PRESENCE);
        set_generic_password_options(secret, pwd_option).unwrap(); // <--- error

        Ok(())
    }

I aim to write a CLI, and when a credential is required to proceed, "force" the user to use their fingerprint to unblock the security check.
Unfortunately, the above code goes to an error:

called `Result::unwrap()` on an `Err` value: Error { code: -34018, message: "A required entitlement isn't present." }

I comment set_access_control_options line, it worked.

Instead, using the below approach goes ok:

        let class = ItemClass::generic_password();
        let data = CFData::from_buffer(secret);
        let value = ItemAddValue::Data { class, data };
        let mut item_to_add = ItemAddOptions::new(value);
        item_to_add.set_service(service);
        item_to_add.set_account_name(account);
        item_to_add.add().unwrap();

However, I am unable to set the access control option.

I also tried to sign the bin and run the bin with:

cargo build --release
codesign -s - -f target/release/my-cli
codesign --verify --deep --strict target/release/my-cli

But the error is still there.

Can you suggest to me how to fix it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions