1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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 secret_service::SecretService;
use secret_collection::SecretCollection;
use secret_value::SecretValue;
use SecretResult;
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);

impl SecretItem {

    /// Create a new item in the secret service.
    /// collection: a secret collection to create this item in
    /// attributes: attributes for the new item.
    /// label: label for the new item
    /// value: secret value for the new item
    pub fn create(collection: &SecretCollection, attributes: &HashMap<String, String>, label: &str, value: &SecretValue) -> SecretResult<SecretItem> {
        let mut err = ptr::null_mut();
        unsafe {
            let item = ffi::secret_item_create_sync(
                collection.to_glib_none().0,
                ptr::null(),
                attributes.to_glib_none().0,
                label.to_glib_none().0,
                value.to_glib_none(),
                SECRET_ITEM_CREATE_NONE,
                ptr::null_mut(),
                &mut err
                );

            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))
            }
        }
    }

    /// Delete this secret item.
    pub fn delete(&self) -> SecretResult<()> {
        let mut err = ptr::null_mut();
        unsafe {
            ffi::secret_item_delete_sync(
                self.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                )
        };
        if err.is_null() {
            Ok(())
        } else {
            Err(Error::wrap(err))
        }
    }

    /// Get the name of the attribute schema.
    pub fn get_schema_name(&self) -> String {
        unsafe {
            let ptr = ffi::secret_item_get_schema_name(self.to_glib_none().0);
            from_glib_full(ptr)
        }
    }

    /// Get the created date and time of the item.
    /// The return value is the number of seconds since the unix epoch, January 1st 1970.
    pub fn get_created(&self) -> u64 {
        unsafe {
            ffi::secret_item_get_created(self.to_glib_none().0)
        }
    }

    /// Get the modified date and time of the item.
    /// The return value is the number of seconds since the unix epoch, January 1st 1970.
    pub fn get_modified(&self) -> u64 {
        unsafe {
            ffi::secret_item_get_modified(self.to_glib_none().0)
        }
    }

    /// Get the label of the item.
    pub fn get_label(&self) -> String {
        unsafe {
            let ptr = ffi::secret_item_get_label(self.to_glib_none().0);
            from_glib_full(ptr)
        }
    }

    /// Get the SecretService this item was created with.
    pub fn get_service(&self) -> SecretService {
        unsafe {
            let ptr = ffi::secret_item_get_service(self.to_glib_none().0);
            from_glib_none(ptr)
        }
    }

    /// Ensure that the SecretValue of this item is loaded.
    pub fn load_secret(&self) -> SecretResult<()> {
        let mut err = ptr::null_mut();
        unsafe {
            ffi::secret_item_load_secret_sync(
                self.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                )
        };
        if err.is_null() {
            Ok(())
        } else {
            Err(Error::wrap(err))
        }
    }

    /// Get the SecretValue of this item. The item must be unlocked and the 
    /// value must be loaded.
    pub fn get_secret(&self) -> Option<SecretValue> {
        unsafe {
            let ptr = ffi::secret_item_get_secret(self.to_glib_none().0);
            if ptr.is_null() {
                None
            } else {
                Some(SecretValue::from_glib_full(ptr))
            }
        }
    }

    /// Set the secret value of this item.
    /// Each item has a single secret which might be a password or some other 
    /// secret binary value (not supported yet).
    pub fn set_secret(&self, value: &SecretValue) -> SecretResult<()> {
        let mut err = ptr::null_mut();
        unsafe {
            ffi::secret_item_set_secret_sync(
                self.to_glib_none().0,
                value.to_glib_none(),
                ptr::null_mut(),
                &mut err
                );
            if err.is_null() {
                Ok(())
            } else {
                Err(Error::wrap(err))
            }
        }
    }

    /// Get the attributes of this item.
    pub fn get_attributes(&self) -> HashMap<String, String> {
        unsafe {
            let ptr = ffi::secret_item_get_attributes(self.to_glib_none().0);
            HashMap::from_glib_full(ptr)
        }
    }

    /// Set the attributes of this item.
    pub fn set_attributes(&self, attributes: &HashMap<String, String>) -> SecretResult<()> {
        let mut err = ptr::null_mut();
        unsafe {
            ffi::secret_item_set_attributes_sync(
                self.to_glib_none().0,
                ptr::null(),
                attributes.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                );
            if err.is_null() {
                Ok(())
            } else {
                Err(Error::wrap(err))
            }
        }
    }

    /// Check if the item is currently locked.
    pub fn is_locked(&self) -> bool {
        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
    }
}

impl Lock<SecretItem> for SecretItem {

    fn lock(&self) -> SecretResult<Vec<SecretItem>>{
        lock_object::<SecretItem>(self)
    }

    fn unlock(&self) -> SecretResult<Vec<SecretItem>>{
        unlock_object::<SecretItem>(self)
    }
}

const SECRET_ITEM_CREATE_NONE: i32        = 0;
#[allow(dead_code)]
const SECRET_ITEM_CREATE_REPLACE: i32     = 1 << 1;