Skip to content

Commit 627936b

Browse files
committed
cargo-credential-gnome-secret: dynamically load libsecret
1 parent 3a34fca commit 627936b

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

Cargo.lock

+13-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ lazy_static = "1.4.0"
5757
lazycell = "1.3.0"
5858
libc = "0.2.147"
5959
libgit2-sys = "0.15.2"
60+
libloading = "0.8.0"
6061
memchr = "2.5.0"
6162
miow = "0.6.0"
6263
opener = "0.6.1"
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "cargo-credential-gnome-secret"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition.workspace = true
55
license.workspace = true
66
repository = "https://github.com/rust-lang/cargo"
77
description = "A Cargo credential process that stores tokens with GNOME libsecret."
88

99
[dependencies]
10+
anyhow.workspace = true
1011
cargo-credential.workspace = true
12+
libloading.workspace = true
1113

12-
[build-dependencies]
13-
pkg-config.workspace = true

credential/cargo-credential-gnome-secret/build.rs

-8
This file was deleted.

credential/cargo-credential-gnome-secret/src/libsecret.rs

+43-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Implementation of the libsecret credential helper.
22
3+
use anyhow::Context;
34
use cargo_credential::{
45
read_token, Action, CacheControl, Credential, CredentialResponse, Error, RegistryInfo, Secret,
56
};
7+
use libloading::{Library, Symbol};
68
use std::ffi::{CStr, CString};
79
use std::os::raw::{c_char, c_int};
810
use std::ptr::{null, null_mut};
@@ -52,29 +54,27 @@ enum SecretSchemaAttributeType {
5254
String = 0,
5355
}
5456

55-
extern "C" {
56-
fn secret_password_store_sync(
57-
schema: *const SecretSchema,
58-
collection: *const gchar,
59-
label: *const gchar,
60-
password: *const gchar,
61-
cancellable: *mut GCancellable,
62-
error: *mut *mut GError,
63-
...
64-
) -> gboolean;
65-
fn secret_password_clear_sync(
66-
schema: *const SecretSchema,
67-
cancellable: *mut GCancellable,
68-
error: *mut *mut GError,
69-
...
70-
) -> gboolean;
71-
fn secret_password_lookup_sync(
72-
schema: *const SecretSchema,
73-
cancellable: *mut GCancellable,
74-
error: *mut *mut GError,
75-
...
76-
) -> *mut gchar;
77-
}
57+
type SecretPasswordStoreSync = extern "C" fn(
58+
schema: *const SecretSchema,
59+
collection: *const gchar,
60+
label: *const gchar,
61+
password: *const gchar,
62+
cancellable: *mut GCancellable,
63+
error: *mut *mut GError,
64+
...
65+
) -> gboolean;
66+
type SecretPasswordClearSync = extern "C" fn(
67+
schema: *const SecretSchema,
68+
cancellable: *mut GCancellable,
69+
error: *mut *mut GError,
70+
...
71+
) -> gboolean;
72+
type SecretPasswordLookupSync = extern "C" fn(
73+
schema: *const SecretSchema,
74+
cancellable: *mut GCancellable,
75+
error: *mut *mut GError,
76+
...
77+
) -> *mut gchar;
7878

7979
pub struct GnomeSecret;
8080

@@ -105,6 +105,26 @@ impl Credential for GnomeSecret {
105105
action: &Action,
106106
_args: &[&str],
107107
) -> Result<CredentialResponse, Error> {
108+
// Dynamically load libsecret to avoid users needing to install
109+
// additional -dev packages when building this provider.
110+
let lib;
111+
let secret_password_lookup_sync: Symbol<SecretPasswordLookupSync>;
112+
let secret_password_store_sync: Symbol<SecretPasswordStoreSync>;
113+
let secret_password_clear_sync: Symbol<SecretPasswordClearSync>;
114+
unsafe {
115+
lib = Library::new("libsecret-1.so").context(
116+
"failed to load libsecret: try installing the `libsecret` \
117+
or `libsecret-1-0` package with the system package manager",
118+
)?;
119+
secret_password_lookup_sync = lib
120+
.get(b"secret_password_lookup_sync\0")
121+
.map_err(Box::new)?;
122+
secret_password_store_sync =
123+
lib.get(b"secret_password_store_sync\0").map_err(Box::new)?;
124+
secret_password_clear_sync =
125+
lib.get(b"secret_password_clear_sync\0").map_err(Box::new)?;
126+
}
127+
108128
let index_url_c = CString::new(registry.index_url).unwrap();
109129
match action {
110130
cargo_credential::Action::Get(_) => {

0 commit comments

Comments
 (0)