mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Auto merge of #11993 - epage:member, r=weihanglo
fix: Allow win/mac credential managers to build on all platforms ### What does this PR try to resolve? This is a step towards #11987 by making two of the platform-specific credential managers build on all platforms using `cfg`. I haven't done `gnome-secret` yet because that is more of an oddball in that it isn't just platforms-specific but dependent on what is installed on that platform. ### How should we test and review this PR? ```console $ cargo check --workspace --exclude cargo-credential-gnome-secret ``` Note that the commits are broken down so you can view the movements of code separate from the functionality being changed. ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. --> <!-- homu-ignore:end -->
This commit is contained in:
commit
9d506e58cd
@ -1,18 +1,20 @@
|
|||||||
//! Cargo registry macos keychain credential process.
|
//! Cargo registry macos keychain credential process.
|
||||||
|
|
||||||
use cargo_credential::{Credential, Error};
|
#[cfg(target_os = "macos")]
|
||||||
use security_framework::os::macos::keychain::SecKeychain;
|
mod macos {
|
||||||
|
use cargo_credential::{Credential, Error};
|
||||||
|
use security_framework::os::macos::keychain::SecKeychain;
|
||||||
|
|
||||||
struct MacKeychain;
|
pub(crate) struct MacKeychain;
|
||||||
|
|
||||||
/// The account name is not used.
|
/// The account name is not used.
|
||||||
const ACCOUNT: &'static str = "";
|
const ACCOUNT: &'static str = "";
|
||||||
|
|
||||||
fn registry(registry_name: &str) -> String {
|
fn registry(registry_name: &str) -> String {
|
||||||
format!("cargo-registry:{}", registry_name)
|
format!("cargo-registry:{}", registry_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Credential for MacKeychain {
|
impl Credential for MacKeychain {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
env!("CARGO_PKG_NAME")
|
env!("CARGO_PKG_NAME")
|
||||||
}
|
}
|
||||||
@ -43,8 +45,14 @@ impl Credential for MacKeychain {
|
|||||||
item.delete();
|
item.delete();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
use cargo_credential::UnsupportedCredential as MacKeychain;
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use macos::MacKeychain;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
cargo_credential::main(MacKeychain);
|
cargo_credential::main(MacKeychain);
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,39 @@
|
|||||||
//! Cargo registry windows credential process.
|
//! Cargo registry windows credential process.
|
||||||
|
|
||||||
use cargo_credential::{Credential, Error};
|
#[cfg(windows)]
|
||||||
use std::ffi::OsStr;
|
mod win {
|
||||||
use std::os::windows::ffi::OsStrExt;
|
use cargo_credential::{Credential, Error};
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
use std::os::windows::ffi::OsStrExt;
|
||||||
|
|
||||||
use windows_sys::core::PWSTR;
|
use windows_sys::core::PWSTR;
|
||||||
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
|
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
|
||||||
use windows_sys::Win32::Foundation::FILETIME;
|
use windows_sys::Win32::Foundation::FILETIME;
|
||||||
use windows_sys::Win32::Foundation::TRUE;
|
use windows_sys::Win32::Foundation::TRUE;
|
||||||
use windows_sys::Win32::Security::Credentials::CredDeleteW;
|
use windows_sys::Win32::Security::Credentials::CredDeleteW;
|
||||||
use windows_sys::Win32::Security::Credentials::CredReadW;
|
use windows_sys::Win32::Security::Credentials::CredReadW;
|
||||||
use windows_sys::Win32::Security::Credentials::CredWriteW;
|
use windows_sys::Win32::Security::Credentials::CredWriteW;
|
||||||
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
|
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
|
||||||
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
|
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
|
||||||
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
|
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
|
||||||
|
|
||||||
struct WindowsCredential;
|
pub(crate) struct WindowsCredential;
|
||||||
|
|
||||||
/// Converts a string to a nul-terminated wide UTF-16 byte sequence.
|
/// Converts a string to a nul-terminated wide UTF-16 byte sequence.
|
||||||
fn wstr(s: &str) -> Vec<u16> {
|
fn wstr(s: &str) -> Vec<u16> {
|
||||||
let mut wide: Vec<u16> = OsStr::new(s).encode_wide().collect();
|
let mut wide: Vec<u16> = OsStr::new(s).encode_wide().collect();
|
||||||
if wide.iter().any(|b| *b == 0) {
|
if wide.iter().any(|b| *b == 0) {
|
||||||
panic!("nul byte in wide string");
|
panic!("nul byte in wide string");
|
||||||
}
|
}
|
||||||
wide.push(0);
|
wide.push(0);
|
||||||
wide
|
wide
|
||||||
}
|
}
|
||||||
|
|
||||||
fn target_name(registry_name: &str) -> Vec<u16> {
|
fn target_name(registry_name: &str) -> Vec<u16> {
|
||||||
wstr(&format!("cargo-registry:{}", registry_name))
|
wstr(&format!("cargo-registry:{}", registry_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Credential for WindowsCredential {
|
impl Credential for WindowsCredential {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
env!("CARGO_PKG_NAME")
|
env!("CARGO_PKG_NAME")
|
||||||
}
|
}
|
||||||
@ -47,15 +49,18 @@ impl Credential for WindowsCredential {
|
|||||||
p_credential as *mut _ as *mut _,
|
p_credential as *mut _ as *mut _,
|
||||||
) != TRUE
|
) != TRUE
|
||||||
{
|
{
|
||||||
return Err(
|
return Err(format!(
|
||||||
format!("failed to fetch token: {}", std::io::Error::last_os_error()).into(),
|
"failed to fetch token: {}",
|
||||||
);
|
std::io::Error::last_os_error()
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
let bytes = std::slice::from_raw_parts(
|
let bytes = std::slice::from_raw_parts(
|
||||||
(*p_credential).CredentialBlob,
|
(*p_credential).CredentialBlob,
|
||||||
(*p_credential).CredentialBlobSize as usize,
|
(*p_credential).CredentialBlobSize as usize,
|
||||||
);
|
);
|
||||||
String::from_utf8(bytes.to_vec()).map_err(|_| "failed to convert token to UTF8".into())
|
String::from_utf8(bytes.to_vec())
|
||||||
|
.map_err(|_| "failed to convert token to UTF8".into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,8 +109,14 @@ impl Credential for WindowsCredential {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
use cargo_credential::UnsupportedCredential as WindowsCredential;
|
||||||
|
#[cfg(windows)]
|
||||||
|
use win::WindowsCredential;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
cargo_credential::main(WindowsCredential);
|
cargo_credential::main(WindowsCredential);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,26 @@ pub trait Credential {
|
|||||||
fn erase(&self, index_url: &str) -> Result<(), Error>;
|
fn erase(&self, index_url: &str) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct UnsupportedCredential;
|
||||||
|
|
||||||
|
impl Credential for UnsupportedCredential {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"unsupported"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, _index_url: &str) -> Result<String, Error> {
|
||||||
|
Err("unsupported".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn store(&self, _index_url: &str, _token: &str, _name: Option<&str>) -> Result<(), Error> {
|
||||||
|
Err("unsupported".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn erase(&self, _index_url: &str) -> Result<(), Error> {
|
||||||
|
Err("unsupported".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Runs the credential interaction by processing the command-line and
|
/// Runs the credential interaction by processing the command-line and
|
||||||
/// environment variables.
|
/// environment variables.
|
||||||
pub fn main(credential: impl Credential) {
|
pub fn main(credential: impl Credential) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user