summaryrefslogtreecommitdiff
path: root/libsecret/secret-retrievable.c
blob: 714ad33687b31d8056418f60d4986c977fb21b83 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* libsecret - GLib wrapper for Secret Service
 *
 * Copyright 2019 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Daiki Ueno
 */

#include "config.h"

#include "secret-retrievable.h"
#include "secret-private.h"

/**
 * SecretRetrievable:
 *
 * A read-only view of a secret item in the Secret Service.
 *
 * #SecretRetrievable provides a read-only view of a secret item
 * stored in the Secret Service.
 *
 * Each item has a value, represented by a [struct@Value], which can be
 * retrieved by [method@Retrievable.retrieve_secret] and
 * [method@Retrievable.retrieve_secret_finish].
 *
 * Stability: Stable
 *
 * Since: 0.19.0
 */

/**
 * SecretRetrievableInterface:
 * @parent_iface: the parent interface
 * @retrieve_secret: implementation of [method@Retrievable.retrieve_secret],
 *   required
 * @retrieve_secret_finish: implementation of
 *   [method@Retrievable.retrieve_secret_finish], required
 *
 * The interface for #SecretRetrievable.
 *
 * Since: 0.19.0
 */

G_DEFINE_INTERFACE (SecretRetrievable, secret_retrievable, G_TYPE_OBJECT);

static void
secret_retrievable_default_init (SecretRetrievableInterface *iface)
{
	/**
	 * SecretRetrievable:attributes: (type GLib.HashTable(utf8,utf8)) (transfer full) (attributes org.gtk.Property.get=secret_retrievable_get_attributes)
	 *
	 * The attributes set on this item.
	 *
	 * Attributes are used to locate an item. They are not guaranteed to be
	 * stored or transferred securely.
	 *
	 * Since: 0.19.0
	 */
	g_object_interface_install_property (iface,
	             g_param_spec_boxed ("attributes", "Attributes", "Item attributes",
	                                 G_TYPE_HASH_TABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * SecretRetrievable:label: (attributes org.gtk.Property.get=secret_retrievable_get_label)
	 *
	 * The human readable label for the item.
	 *
	 * Since: 0.19.0
	 */
	g_object_interface_install_property (iface,
	            g_param_spec_string ("label", "Label", "Item label",
	                                 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * SecretRetrievable:created: (attributes org.gtk.Property.get=secret_retrievable_get_created)
	 *
	 * The date and time (in seconds since the UNIX epoch) that this
	 * item was created.
	 *
	 * Since: 0.19.0
	 */
	g_object_interface_install_property (iface,
	            g_param_spec_uint64 ("created", "Created", "Item creation date",
	                                 0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * SecretRetrievable:modified: (attributes org.gtk.Property.get=secret_retrievable_get_modified)
	 *
	 * The date and time (in seconds since the UNIX epoch) that this
	 * item was last modified.
	 *
	 * Since: 0.19.0
	 */
	g_object_interface_install_property (iface,
	            g_param_spec_uint64 ("modified", "Modified", "Item modified date",
	                                 0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

/**
 * secret_retrievable_retrieve_secret:
 * @self: a retrievable object
 * @cancellable: (nullable): optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to pass to the callback
 *
 * Retrieve the secret value of this object.
 *
 * Each retrievable object has a single secret which might be a
 * password or some other secret binary value.
 *
 * This function returns immediately and completes asynchronously.
 *
 * Since: 0.19.0
 */
void
secret_retrievable_retrieve_secret (SecretRetrievable *self,
				    GCancellable *cancellable,
				    GAsyncReadyCallback callback,
				    gpointer user_data)
{
	SecretRetrievableInterface *iface;

	g_return_if_fail (SECRET_IS_RETRIEVABLE (self));
	iface = SECRET_RETRIEVABLE_GET_IFACE (self);
	g_return_if_fail (iface->retrieve_secret != NULL);
	iface->retrieve_secret (self, cancellable, callback, user_data);
}

/**
 * secret_retrievable_retrieve_secret_finish:
 * @self: a retrievable object
 * @result: asynchronous result passed to callback
 * @error: location to place error on failure
 *
 * Complete asynchronous operation to retrieve the secret value of this object.
 *
 * Returns: (transfer full) (nullable): the secret value which should be
 *   released with [method@Value.unref], or %NULL
 *
 * Since: 0.19.0
 */
SecretValue *
secret_retrievable_retrieve_secret_finish (SecretRetrievable *self,
					   GAsyncResult *result,
					   GError **error)
{
	SecretRetrievableInterface *iface;

	g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);
	iface = SECRET_RETRIEVABLE_GET_IFACE (self);
	g_return_val_if_fail (iface->retrieve_secret_finish != NULL, NULL);
	return iface->retrieve_secret_finish (self, result, error);
}

/**
 * secret_retrievable_retrieve_secret_sync:
 * @self: a retrievable object
 * @cancellable: (nullable): optional cancellation object
 * @error: location to place error on failure
 *
 * Retrieve the secret value of this object synchronously.
 *
 * Each retrievable object has a single secret which might be a
 * password or some other secret binary value.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full) (nullable): the secret value which should be
 *   released with [method@Value.unref], or %NULL
 *
 * Since: 0.19.0
 */
SecretValue *
secret_retrievable_retrieve_secret_sync (SecretRetrievable *self,
					 GCancellable *cancellable,
					 GError **error)
{
	SecretSync *sync;
	SecretValue *value;

	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	sync = _secret_sync_new ();
	g_main_context_push_thread_default (sync->context);

	secret_retrievable_retrieve_secret (self,
					    cancellable,
					    _secret_sync_on_result, sync);

	g_main_loop_run (sync->loop);

	value = secret_retrievable_retrieve_secret_finish (self,
							   sync->result,
							   error);

	g_main_context_pop_thread_default (sync->context);
	_secret_sync_free (sync);

	return value;
}

/**
 * secret_retrievable_get_attributes: (attributes org.gtk.Method.get_property=attributes)
 * @self: a retrievable object
 *
 * Get the attributes of this object.
 *
 * The attributes are a mapping of string keys to string values.
 * Attributes are used to search for items. Attributes are not stored
 * or transferred securely by the secret service.
 *
 * Do not modify the attribute returned by this method.
 *
 * Returns: (transfer full) (element-type utf8 utf8): a new reference
 *   to the attributes, which should not be modified, and
 *   released with [func@GLib.HashTable.unref]
 *
 * Since: 0.19.0
 */
GHashTable *
secret_retrievable_get_attributes (SecretRetrievable *self)
{
	GHashTable *value;

	g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);

	g_object_get (G_OBJECT (self), "attributes", &value, NULL);
	return value;
}

/**
 * secret_retrievable_get_label:
 * @self: a retrievable object
 *
 * Get the label of this item.
 *
 * Returns: (transfer full): the label, which should be freed with [func@GLib.free]
 *
 * Since: 0.19.0
 */
gchar *
secret_retrievable_get_label (SecretRetrievable *self)
{
	gchar *value;

	g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), NULL);

	g_object_get (G_OBJECT (self), "label", &value, NULL);
	return value;
}

/**
 * secret_retrievable_get_created: (attributes org.gtk.Method.get_property=created)
 * @self: a retrievable object
 *
 * Get the created date and time of the object.
 *
 * The return value is the number of seconds since the unix epoch, January 1st
 * 1970.
 *
 * Returns: the created date and time
 *
 * Since: 0.19.0
 */
guint64
secret_retrievable_get_created (SecretRetrievable *self)
{
	guint64 value;

	g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), 0);

	g_object_get (G_OBJECT (self), "created", &value, NULL);
	return value;
}

/**
 * secret_retrievable_get_modified: (attributes org.gtk.Method.get_property=modified)
 * @self: a retrievable object
 *
 * Get the modified date and time of the object.
 *
 * The return value is the number of seconds since the unix epoch, January 1st
 * 1970.
 *
 * Returns: the modified date and time
 *
 * Since: 0.19.0
 */
guint64
secret_retrievable_get_modified (SecretRetrievable *self)
{
	guint64 value;

	g_return_val_if_fail (SECRET_IS_RETRIEVABLE (self), 0);

	g_object_get (G_OBJECT (self), "modified", &value, NULL);
	return value;
}