12 releases (6 breaking)
0.7.0 | Jan 29, 2025 |
---|---|
0.5.0 | Feb 24, 2024 |
0.4.0 | Aug 1, 2023 |
0.3.0 | Feb 25, 2023 |
0.1.3 | Feb 9, 2022 |
#62 in Authentication
2,078 downloads per month
310KB
7.5K
SLoC
libsecret-rs
The Rust bindings of libsecret.
Maintenance
This crate is in maintenace-only mode; we will keep it up to date with the gtk-core-rs crates, but do not expect new development or bugfixes. Please contribute merge requests instead.
Documentation
lib.rs
:
Rust Libsecret bindings
This library contains safe Rust bindings for Libsecret, a library that offers access to the Secret Service API.
See also
Usage
You can add libsecret by adding it in your Cargo.toml
file:
[dependencies.secret]
package = "libsecret"
version = "0.x.y"
Sync versus async API
The following examples use synchronous functions for simplicit. In a GUI
application however you should use the async Future-based API (e.g
password_store_future
and password_lookup_future
) instead to avoid
blocking the UI.
Define a password schema
Each stored password has a set of attributes which are later used to lookup the password. The names and types of the attributes are defined in a schema. The schema is usually defined once globally. Here’s how to define a schema:
use libsecret::*;
let mut attributes = HashMap::new();
attributes.insert("number", SchemaAttributeType::Integer);
attributes.insert("string", SchemaAttributeType::String);
attributes.insert("even", SchemaAttributeType::Boolean);
let schema = Schema::new("com.example.app", SchemaFlags::NONE, attributes);
Store a password
Each stored password has a set of attributes which are later used to lookup the password. The attributes should not contain secrets, as they are not stored in an encrypted fashion.
let mut attributes = std::collections::HashMap::new();
attributes.insert("number", "8");
attributes.insert("string", "eight");
attributes.insert("even", "true");
let collection = libsecret::COLLECTION_DEFAULT;
libsecret::password_store_sync(
Some(&schema),
attributes,
Some(&collection),
"The Label",
"the password",
gio::Cancellable::NONE
)?;
Lookup a password
Each stored password has a set of attributes which are used to lookup the password. If multiple passwords match the lookup attributes, then the one stored most recently is returned.
This first example looks up a password asynchronously, and is appropriate for GUI applications so that the UI does not block.
let mut attributes = std::collections::HashMap::new();
attributes.insert("number", "8");
attributes.insert("even", "true");
let password: Option<glib::GString> = libsecret::password_lookup_sync(Some(&schema), attributes, gio::Cancellable::NONE)?;
Remove a password
Each stored password has a set of attributes which are used to find which password to remove. If multiple passwords match the attributes, then the one stored most recently is removed.
This first example removes a password asynchronously, and is appropriate for GUI applications so that the UI does not block.
let mut attributes = HashMap::new();
attributes.insert("number", "8");
attributes.insert("even", "true");
libsecret::password_clear_sync(Some(&schema), attributes, gio::Cancellable::NONE)?;
Dependencies
~7–14MB
~216K SLoC