Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize secret-rs to work on modern versions of glib #10

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions secret-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
extern crate libc;
extern crate glib;

use libc::{c_uint, c_char, c_int, c_ulong, c_long};
use glib::ffi::{GError, GType, GList, gboolean, gsize, gpointer, GHashTable};
use libc::{c_uint, c_char, c_int, c_ulong, c_long, size_t};
use glib::ffi::{GError, GType, GList, gboolean, gpointer, GHashTable};
type gsize = size_t;

//glib stuff not provided by glib-rs
pub type gchar = c_char;
pub type gint = c_int;
pub type guint = c_uint;
pub type guint64 = c_ulong;
pub type gssize = c_long;
#[repr(C)] pub struct GCancellable;
#[repr(C)] pub struct GCancellable { private: [u8; 0] }

//libsecret

#[repr(C)] pub struct SecretService;
#[repr(C)] pub struct SecretSchema;
#[repr(C)] pub struct SecretCollection;
#[repr(C)] pub struct SecretItem;
#[repr(C)] pub struct SecretValue;
#[repr(C)] pub struct SecretService { private: [u8; 0] }
#[repr(C)] pub struct SecretServiceClass { private: [u8; 0] }
#[repr(C)] pub struct SecretSchema { private: [u8; 0] }
#[repr(C)] pub struct SecretCollection { private: [u8; 0] }
#[repr(C)] pub struct SecretCollectionClass { private: [u8; 0] }
#[repr(C)] pub struct SecretItem { private: [u8; 0] }
#[repr(C)] pub struct SecretItemClass { private: [u8; 0] }
#[repr(C)] pub struct SecretValue { private: [u8; 0] }

#[link(name="secret-1")]
extern "C" {
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ mod secret_item;
mod secret_value;
mod util;

pub use self::secret_sys as ffi;
use self::secret_sys as ffi;
pub use self::secret_service::SecretService;
pub use self::secret_collection::SecretCollection;
pub use self::secret_item::SecretItem;
pub use self::secret_value::SecretValue;

use glib::Error;
use glib::object::Wrapper;
use glib::object::ObjectType;

/// A Result which may contain an error from the SecretService backend.
pub type SecretResult<T> = Result<T, Error>;

/// This Trait is implemented by objects which can be locked and unlocked
pub trait Lock<T: Wrapper> {
pub trait Lock<T: ObjectType> {

/// Lock the object.
fn lock(&self) -> SecretResult<Vec<T>>;
Expand Down
84 changes: 37 additions & 47 deletions src/secret_collection.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use std::ptr;
use glib::Error;
use glib::object::{Object, Upcast, Wrapper, Ref};
use glib::types::{StaticType, Type};
use glib::translate::*;
use glib::glib_container::GlibContainer;
use glib::wrapper;
use secret_service::SecretService;
use secret_item::SecretItem;
use SecretResult;
use util::{lock_object, unlock_object};
use Lock;
use ffi;

/// SecretCollection represents a collection of secret items stored in the
/// Secret Service.
/// A collection can be in a locked or unlocked state. Use `Lock::lock()` or
/// `Lock::unlock()` to lock or unlock the collection.
/// Use `get_items()` to lookup the items in the collection. There may not be
/// any items exposed when the collection is locked.
pub struct SecretCollection(Ref);
wrapper! {
/// SecretCollection represents a collection of secret items stored in the
/// Secret Service.
/// A collection can be in a locked or unlocked state. Use `Lock::lock()` or
/// `Lock::unlock()` to lock or unlock the collection.
/// Use `get_items()` to lookup the items in the collection. There may not be
/// any items exposed when the collection is locked.
pub struct SecretCollection(Object<ffi::SecretCollection, ffi::SecretCollectionClass>);

match fn {
type_ => || ffi::secret_collection_get_type(),
}
}

impl SecretCollection {

Expand All @@ -42,7 +45,11 @@ impl SecretCollection {
}
)
} else {
Err(Error::wrap(err))
Err(
unsafe {
from_glib_full(err)
}
)
}
}

Expand All @@ -68,9 +75,13 @@ impl SecretCollection {
unsafe {
from_glib_full(ptr)
}
)
)
} else {
Err(Error::wrap(err))
Err(
unsafe {
from_glib_full(err)
}
)
}
}

Expand All @@ -88,7 +99,11 @@ impl SecretCollection {
if err.is_null(){
Ok(())
} else {
Err(Error::wrap(err))
Err(
unsafe {
from_glib_full(err)
}
)
}
}

Expand Down Expand Up @@ -167,7 +182,7 @@ impl SecretCollection {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(from_glib_full(err))
}
}
}
Expand All @@ -187,7 +202,7 @@ impl SecretCollection {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(from_glib_full(err))
}
}
}
Expand All @@ -199,32 +214,7 @@ impl SecretCollection {
self.to_glib_none().0
)
};
from_glib(gbool)
}
}

impl StaticType for SecretCollection {
fn static_type() -> Type{
unsafe {
from_glib(ffi::secret_collection_get_type())
}
}
}

unsafe impl Upcast<Object> for SecretCollection { }

impl Wrapper for SecretCollection {
type GlibType = ffi::SecretCollection;
unsafe fn wrap(r: Ref) -> Self{
SecretCollection(r)
}

fn as_ref(&self) -> &Ref{
&self.0
}

fn unwrap(self) -> Ref{
self.0
unsafe { from_glib(gbool) }
}
}

Expand Down Expand Up @@ -262,9 +252,9 @@ mod test {

#[test]
pub fn test_sc_static_type() {
match SecretCollection::static_type() {
Type::Other(_) => {},
_ => panic!("Expected Type::Other")
}
let type_ = SecretCollection::static_type();
assert!(type_.is_valid());
assert!(type_ > Type::OBJECT);
assert_eq!(type_.name(), "SecretCollection");
}
}
88 changes: 37 additions & 51 deletions src/secret_item.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::ptr;
use std::collections::HashMap;
use glib::Error;
use glib::glib_container::GlibContainer;
use glib::object::{Wrapper, Ref, Object, Upcast};
use glib::types::{StaticType, Type};
use glib::translate::*;
use glib::wrapper;
use secret_service::SecretService;
use secret_collection::SecretCollection;
use secret_value::SecretValue;
Expand All @@ -13,22 +10,28 @@ use ffi;
use util::{lock_object, unlock_object};
use Lock;

/// SecretItem represents a secret item stored in the Secret Service.
/// Each item has a value, represented by a SecretValue, which can be retrieved
/// by `get_secret()` or set by `set_secret()`. The item is only available when
/// the item is not locked.
/// Items can be locked or unlocked using the `Lock::lock()` or `Lock::unlock()`
/// functions. The Lock trait is implemented by SecretItem. The Secret Service
/// may not be able to unlock individual items, and may unlock an entire
/// collection when a single item is unlocked.
/// Each item has a set of attributes, which are used to locate the item later.
/// These are not stored or transferred in a secure manner. Each attribute has
/// a string name and a string value. Use `SecretService::search()` to search
/// for items based on their attributes, and `set_attributes()` to change the
/// attributes associated with an item.
/// Items can be created with `create()` or `SecretService::store()`.
///
pub struct SecretItem(Ref);
wrapper! {
/// SecretItem represents a secret item stored in the Secret Service.
/// Each item has a value, represented by a SecretValue, which can be retrieved
/// by `get_secret()` or set by `set_secret()`. The item is only available when
/// the item is not locked.
/// Items can be locked or unlocked using the `Lock::lock()` or `Lock::unlock()`
/// functions. The Lock trait is implemented by SecretItem. The Secret Service
/// may not be able to unlock individual items, and may unlock an entire
/// collection when a single item is unlocked.
/// Each item has a set of attributes, which are used to locate the item later.
/// These are not stored or transferred in a secure manner. Each attribute has
/// a string name and a string value. Use `SecretService::search()` to search
/// for items based on their attributes, and `set_attributes()` to change the
/// attributes associated with an item.
/// Items can be created with `create()` or `SecretService::store()`.
///
pub struct SecretItem(Object<ffi::SecretItem, ffi::SecretItemClass>);

match fn {
type_ => || ffi::secret_item_get_type(),
}
}

impl SecretItem {

Expand All @@ -54,7 +57,7 @@ impl SecretItem {
if err.is_null() { //TODO for all patterns like this: This if does not need to be in the unsafe block. Fix pls.
Ok(from_glib_full(item))
} else {
Err(Error::wrap(err))
Err(from_glib_full(err))
}
}
}
Expand All @@ -72,7 +75,11 @@ impl SecretItem {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(
unsafe {
from_glib_full(err)
}
)
}
}

Expand Down Expand Up @@ -129,7 +136,11 @@ impl SecretItem {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(
unsafe {
from_glib_full(err)
}
)
}
}

Expand Down Expand Up @@ -161,7 +172,7 @@ impl SecretItem {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(from_glib_full(err))
}
}
}
Expand All @@ -188,7 +199,7 @@ impl SecretItem {
if err.is_null() {
Ok(())
} else {
Err(Error::wrap(err))
Err(from_glib_full(err))
}
}
}
Expand All @@ -198,32 +209,7 @@ impl SecretItem {
let gbool = unsafe {
ffi::secret_item_get_locked(self.to_glib_none().0)
};
from_glib(gbool)
}
}

impl StaticType for SecretItem {
fn static_type() -> Type{
unsafe {
from_glib(ffi::secret_item_get_type())
}
}
}

unsafe impl Upcast<Object> for SecretItem { }

impl Wrapper for SecretItem {
type GlibType = ffi::SecretItem;
unsafe fn wrap(r: Ref) -> Self{
SecretItem(r)
}

fn as_ref(&self) -> &Ref{
&self.0
}

fn unwrap(self) -> Ref{
self.0
unsafe { from_glib(gbool) }
}
}

Expand Down
Loading