Branch data Line data Source code
1 : : /* libsecret - GLib wrapper for Secret Service
2 : : *
3 : : * Copyright 2019 Red Hat, Inc.
4 : : *
5 : : * This program is free software: you can redistribute it and/or modify
6 : : * it under the terms of the GNU Lesser General Public License as published
7 : : * by the Free Software Foundation; either version 2.1 of the licence or (at
8 : : * your option) any later version.
9 : : *
10 : : * See the included COPYING file for more information.
11 : : *
12 : : * Author: Daiki Ueno
13 : : */
14 : :
15 : : #include "config.h"
16 : :
17 : : #include "secret-retrievable.h"
18 : : #include "secret-private.h"
19 : :
20 : : /**
21 : : * SecretRetrievable:
22 : : *
23 : : * A read-only view of a secret item in the Secret Service.
24 : : *
25 : : * #SecretRetrievable provides a read-only view of a secret item
26 : : * stored in the Secret Service.
27 : : *
28 : : * Each item has a value, represented by a [struct@Value], which can be
29 : : * retrieved by [method@Retrievable.retrieve_secret] and
30 : : * [method@Retrievable.retrieve_secret_finish].
31 : : *
32 : : * Stability: Stable
33 : : *
34 : : * Since: 0.19.0
35 : : */
36 : :
37 : : /**
38 : : * SecretRetrievableInterface:
39 : : * @parent_iface: the parent interface
40 : : * @retrieve_secret: implementation of [method@Retrievable.retrieve_secret],
41 : : * required
42 : : * @retrieve_secret_finish: implementation of
43 : : * [method@Retrievable.retrieve_secret_finish], required
44 : : *
45 : : * The interface for #SecretRetrievable.
46 : : *
47 : : * Since: 0.19.0
48 : : */
49 : :
50 [ + + + - : 106 : G_DEFINE_INTERFACE (SecretRetrievable, secret_retrievable, G_TYPE_OBJECT);
+ + ]
51 : :
52 : : static void
53 : 17 : secret_retrievable_default_init (SecretRetrievableInterface *iface)
54 : : {
55 : : /**
56 : : * SecretRetrievable:attributes: (type GLib.HashTable(utf8,utf8)) (transfer full) (attributes org.gtk.Property.get=secret_retrievable_get_attributes)
57 : : *
58 : : * The attributes set on this item.
59 : : *
60 : : * Attributes are used to locate an item. They are not guaranteed to be
61 : : * stored or transferred securely.
62 : : *
63 : : * Since: 0.19.0
64 : : */
65 : 17 : g_object_interface_install_property (iface,
66 : : g_param_spec_boxed ("attributes", "Attributes", "Item attributes",
67 : : G_TYPE_HASH_TABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
68 : :
69 : : /**
70 : : * SecretRetrievable:label: (attributes org.gtk.Property.get=secret_retrievable_get_label)
71 : : *
72 : : * The human readable label for the item.
73 : : *
74 : : * Since: 0.19.0
75 : : */
76 : 17 : g_object_interface_install_property (iface,
77 : : g_param_spec_string ("label", "Label", "Item label",
78 : : NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
79 : :
80 : : /**
81 : : * SecretRetrievable:created: (attributes org.gtk.Property.get=secret_retrievable_get_created)
82 : : *
83 : : * The date and time (in seconds since the UNIX epoch) that this
84 : : * item was created.
85 : : *
86 : : * Since: 0.19.0
87 : : */
88 : 17 : g_object_interface_install_property (iface,
89 : : g_param_spec_uint64 ("created", "Created", "Item creation date",
90 : : 0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
91 : :
92 : : /**
93 : : * SecretRetrievable:modified: (attributes org.gtk.Property.get=secret_retrievable_get_modified)
94 : : *
95 : : * The date and time (in seconds since the UNIX epoch) that this
96 : : * item was last modified.
97 : : *
98 : : * Since: 0.19.0
99 : : */
100 : 17 : g_object_interface_install_property (iface,
101 : : g_param_spec_uint64 ("modified", "Modified", "Item modified date",
102 : : 0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
103 : 17 : }
104 : :
105 : : /**
106 : : * secret_retrievable_retrieve_secret:
107 : : * @self: a retrievable object
108 : : * @cancellable: (nullable): optional cancellation object
109 : : * @callback: called when the operation completes
110 : : * @user_data: data to pass to the callback
111 : : *
112 : : * Retrieve the secret value of this object.
113 : : *
114 : : * Each retrievable object has a single secret which might be a
115 : : * password or some other secret binary value.
116 : : *
117 : : * This function returns immediately and completes asynchronously.
118 : : *
119 : : * Since: 0.19.0
120 : : */
121 : : void
122 : 10 : secret_retrievable_retrieve_secret (SecretRetrievable *self,
123 : : GCancellable *cancellable,
124 : : GAsyncReadyCallback callback,
125 : : gpointer user_data)
126 : : {
127 : : SecretRetrievableInterface *iface;
128 : :
129 [ - + ]: 10 : g_return_if_fail (SECRET_IS_RETRIEVABLE (self));
130 : 10 : iface = SECRET_RETRIEVABLE_GET_IFACE (self);
131 [ - + ]: 10 : g_return_if_fail (iface->retrieve_secret != NULL);
132 : 10 : iface->retrieve_secret (self, cancellable, callback, user_data);
133 : : }
134 : :
135 : : /**
136 : : * secret_retrievable_retrieve_secret_finish:
137 : : * @self: a retrievable object
138 : : * @result: asynchronous result passed to callback
139 : : * @error: location to place error on failure
140 : : *
141 : : * Complete asynchronous operation to retrieve the secret value of this object.
142 : : *
143 : : * Returns: (transfer full) (nullable): the secret value which should be
144 : : * released with [method@Value.unref], or %NULL
145 : : *
146 : : * Since: 0.19.0
147 : : */
148 : : SecretValue *
149 : 10 : secret_retrievable_retrieve_secret_finish (SecretRetrievable *self,
150 : : GAsyncResult *result,
151 : : GError **error)
152 : : {
153 : : SecretRetrievableInterface *iface;
154 : :
155 [ - + ]: 10 : g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);
156 : 10 : iface = SECRET_RETRIEVABLE_GET_IFACE (self);
157 [ - + ]: 10 : g_return_val_if_fail (iface->retrieve_secret_finish != NULL, NULL);
158 : 10 : return iface->retrieve_secret_finish (self, result, error);
159 : : }
160 : :
161 : : /**
162 : : * secret_retrievable_retrieve_secret_sync:
163 : : * @self: a retrievable object
164 : : * @cancellable: (nullable): optional cancellation object
165 : : * @error: location to place error on failure
166 : : *
167 : : * Retrieve the secret value of this object synchronously.
168 : : *
169 : : * Each retrievable object has a single secret which might be a
170 : : * password or some other secret binary value.
171 : : *
172 : : * This method may block indefinitely and should not be used in user interface
173 : : * threads.
174 : : *
175 : : * Returns: (transfer full) (nullable): the secret value which should be
176 : : * released with [method@Value.unref], or %NULL
177 : : *
178 : : * Since: 0.19.0
179 : : */
180 : : SecretValue *
181 : 2 : secret_retrievable_retrieve_secret_sync (SecretRetrievable *self,
182 : : GCancellable *cancellable,
183 : : GError **error)
184 : : {
185 : : SecretSync *sync;
186 : : SecretValue *value;
187 : :
188 [ - + - - : 2 : g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
- - - - -
- ]
189 [ + - - + ]: 2 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
190 : :
191 : 2 : sync = _secret_sync_new ();
192 : 2 : g_main_context_push_thread_default (sync->context);
193 : :
194 : 2 : secret_retrievable_retrieve_secret (self,
195 : : cancellable,
196 : : _secret_sync_on_result, sync);
197 : :
198 : 2 : g_main_loop_run (sync->loop);
199 : :
200 : 2 : value = secret_retrievable_retrieve_secret_finish (self,
201 : : sync->result,
202 : : error);
203 : :
204 : 2 : g_main_context_pop_thread_default (sync->context);
205 : 2 : _secret_sync_free (sync);
206 : :
207 : 2 : return value;
208 : : }
209 : :
210 : : /**
211 : : * secret_retrievable_get_attributes: (attributes org.gtk.Method.get_property=attributes)
212 : : * @self: a retrievable object
213 : : *
214 : : * Get the attributes of this object.
215 : : *
216 : : * The attributes are a mapping of string keys to string values.
217 : : * Attributes are used to search for items. Attributes are not stored
218 : : * or transferred securely by the secret service.
219 : : *
220 : : * Do not modify the attribute returned by this method.
221 : : *
222 : : * Returns: (transfer full) (element-type utf8 utf8): a new reference
223 : : * to the attributes, which should not be modified, and
224 : : * released with [func@GLib.HashTable.unref]
225 : : *
226 : : * Since: 0.19.0
227 : : */
228 : : GHashTable *
229 : 6 : secret_retrievable_get_attributes (SecretRetrievable *self)
230 : : {
231 : : GHashTable *value;
232 : :
233 [ - + ]: 6 : g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);
234 : :
235 : 6 : g_object_get (G_OBJECT (self), "attributes", &value, NULL);
236 : 6 : return value;
237 : : }
238 : :
239 : : /**
240 : : * secret_retrievable_get_label:
241 : : * @self: a retrievable object
242 : : *
243 : : * Get the label of this item.
244 : : *
245 : : * Returns: (transfer full): the label, which should be freed with [func@GLib.free]
246 : : *
247 : : * Since: 0.19.0
248 : : */
249 : : gchar *
250 : 6 : secret_retrievable_get_label (SecretRetrievable *self)
251 : : {
252 : : gchar *value;
253 : :
254 [ - + ]: 6 : g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);
255 : :
256 : 6 : g_object_get (G_OBJECT (self), "label", &value, NULL);
257 : 6 : return value;
258 : : }
259 : :
260 : : /**
261 : : * secret_retrievable_get_created: (attributes org.gtk.Method.get_property=created)
262 : : * @self: a retrievable object
263 : : *
264 : : * Get the created date and time of the object.
265 : : *
266 : : * The return value is the number of seconds since the unix epoch, January 1st
267 : : * 1970.
268 : : *
269 : : * Returns: the created date and time
270 : : *
271 : : * Since: 0.19.0
272 : : */
273 : : guint64
274 : 6 : secret_retrievable_get_created (SecretRetrievable *self)
275 : : {
276 : : guint64 value;
277 : :
278 [ - + ]: 6 : g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), 0);
279 : :
280 : 6 : g_object_get (G_OBJECT (self), "created", &value, NULL);
281 : 6 : return value;
282 : : }
283 : :
284 : : /**
285 : : * secret_retrievable_get_modified: (attributes org.gtk.Method.get_property=modified)
286 : : * @self: a retrievable object
287 : : *
288 : : * Get the modified date and time of the object.
289 : : *
290 : : * The return value is the number of seconds since the unix epoch, January 1st
291 : : * 1970.
292 : : *
293 : : * Returns: the modified date and time
294 : : *
295 : : * Since: 0.19.0
296 : : */
297 : : guint64
298 : 6 : secret_retrievable_get_modified (SecretRetrievable *self)
299 : : {
300 : : guint64 value;
301 : :
302 [ - + ]: 6 : g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), 0);
303 : :
304 : 6 : g_object_get (G_OBJECT (self), "modified", &value, NULL);
305 : 6 : return value;
306 : : }
|