Skip to content

Commit 9235380

Browse files
quinnjrclaude
andcommitted
feat(credential): enable cargo:libsecret on all Unix platforms except macOS and mobile
Widen the libsecret credential provider's cfg gate from `target_os = "linux"` to `all(unix, not(any(target_os = "macos", "ios", "tvos", "watchos", "visionos", "android")))`, making the provider available on the BSDs and other Unix-like systems. libsecret is loaded dynamically at runtime via libloading (which supports these platforms), and the library plus Secret Service implementations are packaged on FreeBSD, OpenBSD, and NetBSD, so the previous Linux-only gate was an artificial restriction rather than a technical one. macOS keeps its OS-specific keyring provider (cargo:macos-keychain), and the mobile Unix targets (iOS-family, Android) are excluded since libsecret does not exist there. Also document the provider's platform availability in registry-authentication.md and the crate README. This is pre-work for RFC 3981 (store registry tokens in the OS credential store by default), which flips secure token storage from opt-in to the default and notes the BSD gap in Cargo's built-in providers: rust-lang/rfcs#3981 Tested on a FreeBSD 15.1-RELEASE amd64 VirtualBox VM with libsecret and gnome-keyring over a D-Bus session bus: LibSecretCredential::new() loads libsecret via dlopen, and login/get/logout round-trips through the Secret Service correctly (token stored, retrieved matching, removed, and reported absent after logout). cargo check -p cargo and cargo fmt pass on x86_64-unknown-linux-gnu. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 5e1cded commit 9235380

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

credential/cargo-credential-libsecret/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ See the [credential-provider] documentation for how to use this.
55

66
This credential provider is built-in to cargo as `cargo:libsecret`.
77

8+
It is available on Unix-like platforms such as Linux and the BSDs — except
9+
macOS, which uses its OS-specific keyring via the `cargo:macos-keychain`
10+
provider instead, and mobile platforms, where libsecret does not exist.
11+
812
> This crate is maintained by the Cargo team, primarily for use by Cargo
913
> and not intended for external use (except as a transitive dependency). This
1014
> crate may make major changes to its APIs or be deprecated without warning.

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
//! > and not intended for external use (except as a transitive dependency). This
33
//! > crate may make major changes to its APIs or be deprecated without warning.
44
5-
#[cfg(target_os = "linux")]
5+
#[cfg(all(
6+
unix,
7+
not(any(
8+
target_os = "macos",
9+
target_os = "ios",
10+
target_os = "tvos",
11+
target_os = "watchos",
12+
target_os = "visionos",
13+
target_os = "android"
14+
))
15+
))]
616
mod linux {
717
//! Implementation of the libsecret credential helper.
818
@@ -259,7 +269,27 @@ mod linux {
259269
}
260270
}
261271

262-
#[cfg(not(target_os = "linux"))]
272+
#[cfg(not(all(
273+
unix,
274+
not(any(
275+
target_os = "macos",
276+
target_os = "ios",
277+
target_os = "tvos",
278+
target_os = "watchos",
279+
target_os = "visionos",
280+
target_os = "android"
281+
))
282+
)))]
263283
pub use cargo_credential::UnsupportedCredential as LibSecretCredential;
264-
#[cfg(target_os = "linux")]
284+
#[cfg(all(
285+
unix,
286+
not(any(
287+
target_os = "macos",
288+
target_os = "ios",
289+
target_os = "tvos",
290+
target_os = "watchos",
291+
target_os = "visionos",
292+
target_os = "android"
293+
))
294+
))]
265295
pub use linux::LibSecretCredential;

doc/book/src/reference/registry-authentication.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ The Keychain Access app can be used to view stored tokens.
5555
### `cargo:libsecret`
5656
Uses [libsecret](https://wiki.gnome.org/Projects/Libsecret) to store tokens.
5757

58+
This provider is available on Unix-like platforms, including Linux and the
59+
BSDs — except macOS, which uses its OS-specific keyring via
60+
[`cargo:macos-keychain`](#cargomacos-keychain), and mobile platforms (iOS,
61+
Android, and similar), where libsecret does not exist. The `libsecret` library
62+
is loaded at runtime, so it only needs to be installed when this provider is
63+
used.
64+
5865
Any password manager with libsecret support can be used to view stored tokens.
5966
The following are a few examples (non-exhaustive):
6067

src/util/auth/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,17 @@ static BUILT_IN_PROVIDERS: &[&'static str] = &[
480480

481481
/// Retrieves a cached instance of `LibSecretCredential`.
482482
/// Must be cached to avoid repeated load/unload cycles, which are not supported by `glib`.
483-
#[cfg(target_os = "linux")]
483+
#[cfg(all(
484+
unix,
485+
not(any(
486+
target_os = "macos",
487+
target_os = "ios",
488+
target_os = "tvos",
489+
target_os = "watchos",
490+
target_os = "visionos",
491+
target_os = "android"
492+
))
493+
))]
484494
fn get_credential_libsecret()
485495
-> CargoResult<&'static cargo_credential_libsecret::LibSecretCredential> {
486496
static CARGO_CREDENTIAL_LIBSECRET: std::sync::OnceLock<
@@ -535,7 +545,17 @@ fn credential_action(
535545
"cargo:wincred" => Box::new(cargo_credential_wincred::WindowsCredential {}),
536546
#[cfg(target_os = "macos")]
537547
"cargo:macos-keychain" => Box::new(cargo_credential_macos_keychain::MacKeychain {}),
538-
#[cfg(target_os = "linux")]
548+
#[cfg(all(
549+
unix,
550+
not(any(
551+
target_os = "macos",
552+
target_os = "ios",
553+
target_os = "tvos",
554+
target_os = "watchos",
555+
target_os = "visionos",
556+
target_os = "android"
557+
))
558+
))]
539559
"cargo:libsecret" => Box::new(get_credential_libsecret()?),
540560
name if BUILT_IN_PROVIDERS.contains(&name) => {
541561
Box::new(cargo_credential::UnsupportedCredential {})

0 commit comments

Comments
 (0)