cargo_credential_wincred/
lib.rs1#[cfg(windows)]
8mod win {
9 use cargo_credential::{read_token, Action, CacheControl, CredentialResponse, RegistryInfo};
10 use cargo_credential::{Credential, Error};
11 use std::ffi::OsStr;
12
13 use std::os::windows::ffi::OsStrExt;
14
15 use windows_sys::core::PWSTR;
16 use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
17 use windows_sys::Win32::Foundation::FILETIME;
18 use windows_sys::Win32::Foundation::TRUE;
19 use windows_sys::Win32::Security::Credentials::CredReadW;
20 use windows_sys::Win32::Security::Credentials::CredWriteW;
21 use windows_sys::Win32::Security::Credentials::CREDENTIALW;
22 use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
23 use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
24 use windows_sys::Win32::Security::Credentials::{CredDeleteW, CredFree};
25
26 pub struct WindowsCredential;
27
28 fn wstr(s: &str) -> Vec<u16> {
30 let mut wide: Vec<u16> = OsStr::new(s).encode_wide().collect();
31 if wide.iter().any(|b| *b == 0) {
32 panic!("nul byte in wide string");
33 }
34 wide.push(0);
35 wide
36 }
37
38 fn target_name(index_url: &str) -> Vec<u16> {
39 wstr(&format!("cargo-registry:{}", index_url))
40 }
41
42 impl Credential for WindowsCredential {
43 fn perform(
44 &self,
45 registry: &RegistryInfo<'_>,
46 action: &Action<'_>,
47 _args: &[&str],
48 ) -> Result<CredentialResponse, Error> {
49 match action {
50 Action::Get(_) => {
51 let target_name = target_name(registry.index_url);
52 let mut p_credential: *mut CREDENTIALW = std::ptr::null_mut() as *mut _;
53 let bytes = unsafe {
54 if CredReadW(
55 target_name.as_ptr(),
56 CRED_TYPE_GENERIC,
57 0,
58 &mut p_credential as *mut _,
59 ) != TRUE
60 {
61 let err = std::io::Error::last_os_error();
62 if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
63 return Err(Error::NotFound);
64 }
65 return Err(Box::new(err).into());
66 }
67 std::slice::from_raw_parts(
68 (*p_credential).CredentialBlob,
69 (*p_credential).CredentialBlobSize as usize,
70 )
71 };
72 let token = String::from_utf8(bytes.to_vec()).map_err(Box::new);
73 unsafe { CredFree(p_credential as *mut _) };
74 Ok(CredentialResponse::Get {
75 token: token?.into(),
76 cache: CacheControl::Session,
77 operation_independent: true,
78 })
79 }
80 Action::Login(options) => {
81 let token = read_token(options, registry)?.expose();
82 let target_name = target_name(registry.index_url);
83 let comment = wstr("Cargo registry token");
84 let credential = CREDENTIALW {
85 Flags: 0,
86 Type: CRED_TYPE_GENERIC,
87 TargetName: target_name.as_ptr() as PWSTR,
88 Comment: comment.as_ptr() as PWSTR,
89 LastWritten: FILETIME {
90 dwLowDateTime: 0,
91 dwHighDateTime: 0,
92 },
93 CredentialBlobSize: token.len() as u32,
94 CredentialBlob: token.as_bytes().as_ptr() as *mut u8,
95 Persist: CRED_PERSIST_LOCAL_MACHINE,
96 AttributeCount: 0,
97 Attributes: std::ptr::null_mut(),
98 TargetAlias: std::ptr::null_mut(),
99 UserName: std::ptr::null_mut(),
100 };
101 let result = unsafe { CredWriteW(&credential, 0) };
102 if result != TRUE {
103 let err = std::io::Error::last_os_error();
104 return Err(Box::new(err).into());
105 }
106 Ok(CredentialResponse::Login)
107 }
108 Action::Logout => {
109 let target_name = target_name(registry.index_url);
110 let result = unsafe { CredDeleteW(target_name.as_ptr(), CRED_TYPE_GENERIC, 0) };
111 if result != TRUE {
112 let err = std::io::Error::last_os_error();
113 if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
114 return Err(Error::NotFound);
115 }
116 return Err(Box::new(err).into());
117 }
118 Ok(CredentialResponse::Logout)
119 }
120 _ => Err(Error::OperationNotSupported),
121 }
122 }
123 }
124}
125
126#[cfg(not(windows))]
127pub use cargo_credential::UnsupportedCredential as WindowsCredential;
128#[cfg(windows)]
129pub use win::WindowsCredential;