Skip to content

Commit be5fc43

Browse files
committed
refactor(cred): Move win impl to inline mod
1 parent 5ec32a0 commit be5fc43

File tree

1 file changed

+94
-89
lines changed
  • crates/credential/cargo-credential-wincred/src

1 file changed

+94
-89
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,116 @@
11
//! Cargo registry windows credential process.
22
3-
use cargo_credential::{Credential, Error};
4-
use std::ffi::OsStr;
5-
use std::os::windows::ffi::OsStrExt;
3+
mod win {
4+
use cargo_credential::{Credential, Error};
5+
use std::ffi::OsStr;
6+
use std::os::windows::ffi::OsStrExt;
67

7-
use windows_sys::core::PWSTR;
8-
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
9-
use windows_sys::Win32::Foundation::FILETIME;
10-
use windows_sys::Win32::Foundation::TRUE;
11-
use windows_sys::Win32::Security::Credentials::CredDeleteW;
12-
use windows_sys::Win32::Security::Credentials::CredReadW;
13-
use windows_sys::Win32::Security::Credentials::CredWriteW;
14-
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
15-
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
16-
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
8+
use windows_sys::core::PWSTR;
9+
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
10+
use windows_sys::Win32::Foundation::FILETIME;
11+
use windows_sys::Win32::Foundation::TRUE;
12+
use windows_sys::Win32::Security::Credentials::CredDeleteW;
13+
use windows_sys::Win32::Security::Credentials::CredReadW;
14+
use windows_sys::Win32::Security::Credentials::CredWriteW;
15+
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
16+
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
17+
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
1718

18-
struct WindowsCredential;
19+
pub(crate) struct WindowsCredential;
1920

20-
/// Converts a string to a nul-terminated wide UTF-16 byte sequence.
21-
fn wstr(s: &str) -> Vec<u16> {
22-
let mut wide: Vec<u16> = OsStr::new(s).encode_wide().collect();
23-
if wide.iter().any(|b| *b == 0) {
24-
panic!("nul byte in wide string");
21+
/// Converts a string to a nul-terminated wide UTF-16 byte sequence.
22+
fn wstr(s: &str) -> Vec<u16> {
23+
let mut wide: Vec<u16> = OsStr::new(s).encode_wide().collect();
24+
if wide.iter().any(|b| *b == 0) {
25+
panic!("nul byte in wide string");
26+
}
27+
wide.push(0);
28+
wide
2529
}
26-
wide.push(0);
27-
wide
28-
}
2930

30-
fn target_name(registry_name: &str) -> Vec<u16> {
31-
wstr(&format!("cargo-registry:{}", registry_name))
32-
}
33-
34-
impl Credential for WindowsCredential {
35-
fn name(&self) -> &'static str {
36-
env!("CARGO_PKG_NAME")
31+
fn target_name(registry_name: &str) -> Vec<u16> {
32+
wstr(&format!("cargo-registry:{}", registry_name))
3733
}
3834

39-
fn get(&self, index_url: &str) -> Result<String, Error> {
40-
let target_name = target_name(index_url);
41-
let p_credential: *mut CREDENTIALW = std::ptr::null_mut() as *mut _;
42-
unsafe {
43-
if CredReadW(
44-
target_name.as_ptr(),
45-
CRED_TYPE_GENERIC,
46-
0,
47-
p_credential as *mut _ as *mut _,
48-
) != TRUE
49-
{
50-
return Err(
51-
format!("failed to fetch token: {}", std::io::Error::last_os_error()).into(),
35+
impl Credential for WindowsCredential {
36+
fn name(&self) -> &'static str {
37+
env!("CARGO_PKG_NAME")
38+
}
39+
40+
fn get(&self, index_url: &str) -> Result<String, Error> {
41+
let target_name = target_name(index_url);
42+
let p_credential: *mut CREDENTIALW = std::ptr::null_mut() as *mut _;
43+
unsafe {
44+
if CredReadW(
45+
target_name.as_ptr(),
46+
CRED_TYPE_GENERIC,
47+
0,
48+
p_credential as *mut _ as *mut _,
49+
) != TRUE
50+
{
51+
return Err(format!(
52+
"failed to fetch token: {}",
53+
std::io::Error::last_os_error()
54+
)
55+
.into());
56+
}
57+
let bytes = std::slice::from_raw_parts(
58+
(*p_credential).CredentialBlob,
59+
(*p_credential).CredentialBlobSize as usize,
5260
);
61+
String::from_utf8(bytes.to_vec())
62+
.map_err(|_| "failed to convert token to UTF8".into())
5363
}
54-
let bytes = std::slice::from_raw_parts(
55-
(*p_credential).CredentialBlob,
56-
(*p_credential).CredentialBlobSize as usize,
57-
);
58-
String::from_utf8(bytes.to_vec()).map_err(|_| "failed to convert token to UTF8".into())
5964
}
60-
}
6165

62-
fn store(&self, index_url: &str, token: &str, name: Option<&str>) -> Result<(), Error> {
63-
let token = token.as_bytes();
64-
let target_name = target_name(index_url);
65-
let comment = match name {
66-
Some(name) => wstr(&format!("Cargo registry token for {}", name)),
67-
None => wstr("Cargo registry token"),
68-
};
69-
let mut credential = CREDENTIALW {
70-
Flags: 0,
71-
Type: CRED_TYPE_GENERIC,
72-
TargetName: target_name.as_ptr() as PWSTR,
73-
Comment: comment.as_ptr() as PWSTR,
74-
LastWritten: FILETIME {
75-
dwLowDateTime: 0,
76-
dwHighDateTime: 0,
77-
},
78-
CredentialBlobSize: token.len() as u32,
79-
CredentialBlob: token.as_ptr() as *mut u8,
80-
Persist: CRED_PERSIST_LOCAL_MACHINE,
81-
AttributeCount: 0,
82-
Attributes: std::ptr::null_mut(),
83-
TargetAlias: std::ptr::null_mut(),
84-
UserName: std::ptr::null_mut(),
85-
};
86-
let result = unsafe { CredWriteW(&mut credential, 0) };
87-
if result != TRUE {
88-
let err = std::io::Error::last_os_error();
89-
return Err(format!("failed to store token: {}", err).into());
66+
fn store(&self, index_url: &str, token: &str, name: Option<&str>) -> Result<(), Error> {
67+
let token = token.as_bytes();
68+
let target_name = target_name(index_url);
69+
let comment = match name {
70+
Some(name) => wstr(&format!("Cargo registry token for {}", name)),
71+
None => wstr("Cargo registry token"),
72+
};
73+
let mut credential = CREDENTIALW {
74+
Flags: 0,
75+
Type: CRED_TYPE_GENERIC,
76+
TargetName: target_name.as_ptr() as PWSTR,
77+
Comment: comment.as_ptr() as PWSTR,
78+
LastWritten: FILETIME {
79+
dwLowDateTime: 0,
80+
dwHighDateTime: 0,
81+
},
82+
CredentialBlobSize: token.len() as u32,
83+
CredentialBlob: token.as_ptr() as *mut u8,
84+
Persist: CRED_PERSIST_LOCAL_MACHINE,
85+
AttributeCount: 0,
86+
Attributes: std::ptr::null_mut(),
87+
TargetAlias: std::ptr::null_mut(),
88+
UserName: std::ptr::null_mut(),
89+
};
90+
let result = unsafe { CredWriteW(&mut credential, 0) };
91+
if result != TRUE {
92+
let err = std::io::Error::last_os_error();
93+
return Err(format!("failed to store token: {}", err).into());
94+
}
95+
Ok(())
9096
}
91-
Ok(())
92-
}
9397

94-
fn erase(&self, index_url: &str) -> Result<(), Error> {
95-
let target_name = target_name(index_url);
96-
let result = unsafe { CredDeleteW(target_name.as_ptr(), CRED_TYPE_GENERIC, 0) };
97-
if result != TRUE {
98-
let err = std::io::Error::last_os_error();
99-
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
100-
eprintln!("not currently logged in to `{}`", index_url);
101-
return Ok(());
98+
fn erase(&self, index_url: &str) -> Result<(), Error> {
99+
let target_name = target_name(index_url);
100+
let result = unsafe { CredDeleteW(target_name.as_ptr(), CRED_TYPE_GENERIC, 0) };
101+
if result != TRUE {
102+
let err = std::io::Error::last_os_error();
103+
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
104+
eprintln!("not currently logged in to `{}`", index_url);
105+
return Ok(());
106+
}
107+
return Err(format!("failed to remove token: {}", err).into());
102108
}
103-
return Err(format!("failed to remove token: {}", err).into());
109+
Ok(())
104110
}
105-
Ok(())
106111
}
107112
}
108113

109114
fn main() {
110-
cargo_credential::main(WindowsCredential);
115+
cargo_credential::main(win::WindowsCredential);
111116
}

0 commit comments

Comments
 (0)