Skip to content

Commit 22940c7

Browse files
committed
refactor: make LibSecretCredential own libsecret library
1 parent e208691 commit 22940c7

File tree

2 files changed

+26
-14
lines changed
  • credential/cargo-credential-libsecret/src
  • src/cargo/util/auth

2 files changed

+26
-14
lines changed

credential/cargo-credential-libsecret/src/lib.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ mod linux {
8383
...
8484
) -> *mut gchar;
8585

86-
pub struct LibSecretCredential;
86+
pub struct LibSecretCredential {
87+
libsecret: Library,
88+
}
8789

8890
fn label(index_url: &str) -> CString {
8991
CString::new(format!("cargo-registry:{}", index_url)).unwrap()
@@ -105,31 +107,41 @@ mod linux {
105107
}
106108
}
107109

110+
impl LibSecretCredential {
111+
pub fn new() -> Result<LibSecretCredential, Error> {
112+
// Dynamically load libsecret to avoid users needing to install
113+
// additional -dev packages when building this provider.
114+
let libsecret = unsafe { Library::new("libsecret-1.so.0") }.context(
115+
"failed to load libsecret: try installing the `libsecret` \
116+
or `libsecret-1-0` package with the system package manager",
117+
)?;
118+
Ok(Self { libsecret })
119+
}
120+
}
121+
108122
impl Credential for LibSecretCredential {
109123
fn perform(
110124
&self,
111125
registry: &RegistryInfo<'_>,
112126
action: &Action<'_>,
113127
_args: &[&str],
114128
) -> Result<CredentialResponse, Error> {
115-
// Dynamically load libsecret to avoid users needing to install
116-
// additional -dev packages when building this provider.
117-
let lib;
118129
let secret_password_lookup_sync: Symbol<'_, SecretPasswordLookupSync>;
119130
let secret_password_store_sync: Symbol<'_, SecretPasswordStoreSync>;
120131
let secret_password_clear_sync: Symbol<'_, SecretPasswordClearSync>;
121132
unsafe {
122-
lib = Library::new("libsecret-1.so.0").context(
123-
"failed to load libsecret: try installing the `libsecret` \
124-
or `libsecret-1-0` package with the system package manager",
125-
)?;
126-
secret_password_lookup_sync = lib
133+
secret_password_lookup_sync = self
134+
.libsecret
127135
.get(b"secret_password_lookup_sync\0")
128136
.map_err(Box::new)?;
129-
secret_password_store_sync =
130-
lib.get(b"secret_password_store_sync\0").map_err(Box::new)?;
131-
secret_password_clear_sync =
132-
lib.get(b"secret_password_clear_sync\0").map_err(Box::new)?;
137+
secret_password_store_sync = self
138+
.libsecret
139+
.get(b"secret_password_store_sync\0")
140+
.map_err(Box::new)?;
141+
secret_password_clear_sync = self
142+
.libsecret
143+
.get(b"secret_password_clear_sync\0")
144+
.map_err(Box::new)?;
133145
}
134146

135147
let index_url_c = CString::new(registry.index_url).unwrap();

src/cargo/util/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn credential_action(
545545
#[cfg(target_os = "macos")]
546546
"cargo:macos-keychain" => Box::new(cargo_credential_macos_keychain::MacKeychain {}),
547547
#[cfg(target_os = "linux")]
548-
"cargo:libsecret" => Box::new(cargo_credential_libsecret::LibSecretCredential {}),
548+
"cargo:libsecret" => Box::new(cargo_credential_libsecret::LibSecretCredential::new()?),
549549
name if BUILT_IN_PROVIDERS.contains(&name) => {
550550
Box::new(cargo_credential::UnsupportedCredential {})
551551
}

0 commit comments

Comments
 (0)