Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 61 additions & 47 deletions credential/cargo-credential-libsecret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ mod linux {
#[allow(non_camel_case_types)]
type gboolean = c_int;

#[allow(non_camel_case_types)]
type gint = c_int;

#[allow(non_camel_case_types)]
type gpointer = *mut ();

type GQuark = u32;

#[repr(C)]
Expand All @@ -41,6 +47,14 @@ mod linux {
name: *const gchar,
flags: SecretSchemaFlags,
attributes: [SecretSchemaAttribute; 32],
reserved: gint,
reserved1: gpointer,
reserved2: gpointer,
reserved3: gpointer,
reserved4: gpointer,
reserved5: gpointer,
reserved6: gpointer,
reserved7: gpointer,
}

#[repr(C)]
Expand Down Expand Up @@ -97,13 +111,21 @@ mod linux {
attr_type: SecretSchemaAttributeType::String,
}; 32];
attributes[0] = SecretSchemaAttribute {
name: b"url\0".as_ptr() as *const gchar,
name: c"url".as_ptr() as *const gchar,
attr_type: SecretSchemaAttributeType::String,
};
SecretSchema {
name: b"org.rust-lang.cargo.registry\0".as_ptr() as *const gchar,
name: c"org.rust-lang.cargo.registry".as_ptr() as *const gchar,
flags: SecretSchemaFlags::None,
attributes,
reserved: 0,
reserved1: null_mut(),
reserved2: null_mut(),
reserved3: null_mut(),
reserved4: null_mut(),
reserved5: null_mut(),
reserved6: null_mut(),
reserved7: null_mut(),
}
}

Expand Down Expand Up @@ -145,60 +167,55 @@ mod linux {
}

let index_url_c = CString::new(registry.index_url).unwrap();
let mut error: *mut GError = null_mut();
let attr_url = c"url".as_ptr() as *const gchar;
let schema = schema();
match action {
cargo_credential::Action::Get(_) => {
let mut error: *mut GError = null_mut();
let attr_url = CString::new("url").unwrap();
let schema = schema();
unsafe {
let token_c = secret_password_lookup_sync(
&schema,
null_mut(),
&mut error,
attr_url.as_ptr(),
index_url_c.as_ptr(),
null() as *const gchar,
);
if !error.is_null() {
return Err(format!(
"failed to get token: {}",
CStr::from_ptr((*error).message)
.to_str()
.unwrap_or_default()
)
.into());
}
if token_c.is_null() {
return Err(Error::NotFound);
}
let token = Secret::from(
CStr::from_ptr(token_c)
cargo_credential::Action::Get(_) => unsafe {
let token_c = secret_password_lookup_sync(
&schema,
null_mut(),
&mut error,
attr_url,
index_url_c.as_ptr(),
null() as *const gchar,
);
if !error.is_null() {
return Err(format!(
"failed to get token: {}",
CStr::from_ptr((*error).message)
.to_str()
.map_err(|e| format!("expected utf8 token: {}", e))?
.to_string(),
);
Ok(CredentialResponse::Get {
token,
cache: CacheControl::Session,
operation_independent: true,
})
.unwrap_or_default()
)
.into());
}
}
if token_c.is_null() {
return Err(Error::NotFound);
}
let token = Secret::from(
CStr::from_ptr(token_c)
.to_str()
.map_err(|e| format!("expected utf8 token: {}", e))?
.to_string(),
);
Ok(CredentialResponse::Get {
token,
cache: CacheControl::Session,
operation_independent: true,
})
},
cargo_credential::Action::Login(options) => {
let label = label(registry.name.unwrap_or(registry.index_url));
let token = CString::new(read_token(options, registry)?.expose()).unwrap();
let mut error: *mut GError = null_mut();
let attr_url = CString::new("url").unwrap();
let schema = schema();
unsafe {
secret_password_store_sync(
&schema,
b"default\0".as_ptr() as *const gchar,
c"default".as_ptr() as *const gchar,
label.as_ptr(),
token.as_ptr(),
null_mut(),
&mut error,
attr_url.as_ptr(),
attr_url,
index_url_c.as_ptr(),
null() as *const gchar,
);
Expand All @@ -215,15 +232,12 @@ mod linux {
Ok(CredentialResponse::Login)
}
cargo_credential::Action::Logout => {
let schema = schema();
let mut error: *mut GError = null_mut();
let attr_url = CString::new("url").unwrap();
unsafe {
secret_password_clear_sync(
&schema,
null_mut(),
&mut error,
attr_url.as_ptr(),
attr_url,
index_url_c.as_ptr(),
null() as *const gchar,
);
Expand Down