summaryrefslogtreecommitdiff
path: root/docs/reference/libsecret/libsecret-examples.sgml
blob: f885799e0d38012dec97f26486ccf4249f5f1c11 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
               "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
<!ENTITY version SYSTEM "version.xml">
]>
<part id="examples">
	<title>Examples</title>

	<chapter id="c-examples">
		<title>C examples</title>

		<section id="c-schema-example">
			<title>C example: Define a password schema</title>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The names and types of the attributes
			are defined in a schema. The schema is usually defined once globally.
			Here's how to define a schema:</para>

			<informalexample><programlisting language="c"><![CDATA[
			/* in a header: */

			const SecretSchema * example_get_schema (void) G_GNUC_CONST;

			#define EXAMPLE_SCHEMA  example_get_schema ()


			/* in a .c file: */

			const SecretSchema *
			example_get_schema (void)
			{
				static const SecretSchema the_schema = {
					"org.example.Password", SECRET_SCHEMA_NONE,
					{
						{  "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
						{  "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
						{  "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
						{  "NULL", 0 },
					}
				};
				return &the_schema;
			}
			]]></programlisting></informalexample>

			<para>See the <link linkend="c-store-example">other examples</link> for how
			to use the schema.</para>
		</section>

		<section id="c-store-example">
			<title>C example: Store a password</title>

			<para>Here's how to store a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The attributes should not contain
			secrets, as they are not stored in an encrypted fashion.</para>

			<para>These examples use <link linkend="c-schema-example">the example
			schema</link>.</para>

			<para>This first example stores a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="c"><![CDATA[
			static void
			on_password_stored (GObject *source,
			                    GAsyncResult *result,
			                    gpointer unused)
			{
				GError *error = NULL;

				secret_password_store_finish (result, &error);
				if (error != NULL) {
					/* ... handle the failure here */
					g_error_free (error);
				} else {
					/* ... do something now that the password has been stored */
				}
			}

			/*
			 * The variable argument list is the attributes used to later
			 * lookup the password. These attributes must conform to the schema.
			 */
			secret_password_store (EXAMPLE_SCHEMA, SECRET_COLLECTION_DEFAULT, "The label",
			                       "the password", NULL, on_password_stored, NULL,
			                       "number", 8,
			                       "string", "eight",
			                       "even", TRUE,
			                       NULL);
			]]></programlisting></informalexample>

			<para>This next example stores a password synchronously. The function
			call will block until the password is stored. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			GError *error = NULL;

			/*
			 * The variable argument list is the attributes used to later
			 * lookup the password. These attributes must conform to the schema.
			 */
			secret_password_store_sync (EXAMPLE_SCHEMA, SECRET_COLLECTION_DEFAULT,
			                            "The label", "the password", NULL, &error,
			                            "number", 9,
			                            "string", "nine",
			                            "even", FALSE,
			                            NULL);

			if (error != NULL) {
				/* ... handle the failure here */
				g_error_free (error);
			} else {
				/* ... do something now that the password has been stored */
			}

			]]></programlisting></informalexample>
		</section>

		<section id="c-lookup-example">
			<title>C example: Lookup a password</title>

			<para>Here's how to lookup a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to lookup the password. If multiple passwords match the
			lookup attributes, then the one stored most recently is returned.</para>

			<para>These examples use <link linkend="c-schema-example">the example
			schema</link>.</para>

			<para>This first example looks up a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			static void
			on_password_lookup (GObject *source,
			                    GAsyncResult *result,
			                    gpointer unused)
			 {
				GError *error = NULL;

				gchar *password = secret_password_lookup_finish (result, &error);

				if (error != NULL) {
					/* ... handle the failure here */
					g_error_free (error);

				} else if (password == NULL) {
					/* password will be null, if no matching password found */

				} else {
					/* ... do something with the password */
					secret_password_free (password);
				}

			}

			/*
			 * The variable argument list is the attributes used to later
			 * lookup the password. These attributes must conform to the schema.
			 */
			secret_password_lookup (EXAMPLE_SCHEMA, NULL, on_password_lookup, NULL,
			                        "string", "nine",
			                        "even", FALSE,
			                        NULL);
			]]></programlisting></informalexample>

			<para>This next example looks up a password synchronously. The function
			call will block until the lookup completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="c"><![CDATA[
			GError *error = NULL;

			/* The attributes used to lookup the password should conform to the schema. */
			gchar *password = secret_password_lookup_sync (EXAMPLE_SCHEMA, NULL, &error,
			                                               "string", "nine",
			                                               "even", FALSE,
			                                               NULL);
			
			if (error != NULL) {
				/* ... handle the failure here */
				g_error_free (error);

			} else if (password == NULL) {
				/* password will be null, if no matching password found */

			} else {
				/* ... do something with the password */
				secret_password_free (password);
			}
			]]></programlisting></informalexample>
		</section>

		<section id="c-remove-example">
			<title>C example: Remove a password</title>

			<para>Here's how to remove a password from the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to find which password to remove. If multiple passwords match the
			attributes, then the one stored most recently is removed.</para>

			<para>These examples use <link linkend="c-schema-example">the example
			schema</link>.</para>

			<para>This first example removes a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="c"><![CDATA[
			static void
			on_password_removed (GObject *source,
			                     GAsyncResult *result,
			                     gpointer unused)
			 {
				GError *error = NULL;

				gboolean removed = secret_password_remove_finish (result, &error);

				if (error != NULL) {
					/* ... handle the failure here */
					g_error_free (error);

				} else {
					/* removed will be TRUE if a password was removed */
				}
			}

			/*
			 * The variable argument list is the attributes used to later
			 * lookup the password. These attributes must conform to the schema.
			 */
			secret_password_remove (EXAMPLE_SCHEMA, NULL, on_password_removed, NULL,
			                        "string", "nine",
			                        "even", FALSE,
			                        NULL);
			]]></programlisting></informalexample>

			<para>This next example looks up a password synchronously. The function
			call will block until the lookup completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="c"><![CDATA[
			GError *error = NULL;

			/*
			 * The variable argument list is the attributes used to later
			 * lookup the password. These attributes must conform to the schema.
			 */
			gboolean removed = secret_password_remove_sync (EXAMPLE_SCHEMA, NULL, &error,
			                                                "string", "nine",
			                                                "even", FALSE,
			                                                NULL);

			if (error != NULL) {
				/* ... handle the failure here */
				g_error_free (error);

			} else {
				/* removed will be TRUE if a password was removed */
			}
			]]></programlisting></informalexample>
		</section>

	</chapter>

	<chapter id="js-examples">
		<title>Javascript examples</title>

		<section id="js-schema-example">
			<title>Javascript example: Define a password schema</title>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The names and types of the attributes
			are defined in a schema. The schema is usually defined once globally.
			Here's how to define a schema:</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			/* This schema is usually defined once globally */
			const EXAMPLE_SCHEMA = new Secret.Schema.new("org.example.Password",
				Secret.SchemaFlags.NONE,
				{
					"number": Secret.SchemaAttributeType.INTEGER,
					"string": Secret.SchemaAttributeType.STRING,
					"even": Secret.SchemaAttributeType.BOOLEAN,
				}
			);
			]]></programlisting></informalexample>

			<para>See the <link linkend="js-store-example">other examples</link> for how
			to use the schema.</para>
		</section>

		<section id="js-store-example">
			<title>Javascript example: Store a password</title>

			<para>Here's how to store a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The attributes should not contain
			secrets, as they are not stored in an encrypted fashion.</para>

			<para>These examples use <link linkend="js-schema-example">the example
			schema</link>.</para>

			<para>This first example stores a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			function on_password_stored(source, result) {
				Secret.password_store_finish(result);
				/* ... do something now that the password has been stored */
			}

			/*
			 * The attributes used to later lookup the password. These
			 * attributes should conform to the schema.
			 */
			var attributes = {
				"number": "8",
				"string": "eight",
				"even": "true"
			};

			Secret.password_store(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
			                      "The label", "the password", null, on_password_stored);
			]]></programlisting></informalexample>

			<para>This next example stores a password synchronously. The function
			call will block until the password is stored. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			/*
			 * The attributes used to later lookup the password. These
			 * attributes should conform to the schema.
			 */
			var attributes = {
				"number": "9",
				"string": "nine",
				"even": "false"
			};

			Secret.password_store_sync(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
			                           "The label", "the password", null);
			]]></programlisting></informalexample>
		</section>

		<section id="js-lookup-example">
			<title>Javascript example: Lookup a password</title>

			<para>Here's how to lookup a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to lookup the password. If multiple passwords match the
			lookup attributes, then the one stored most recently is returned.</para>

			<para>These examples use <link linkend="js-schema-example">the example
			schema</link>.</para>

			<para>This first example looks up a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			function on_password_lookup(source, result) {
				var password = Secret.password_lookup_finish(result);
				/* password will be null if no matching password found */
			}

			/* The attributes used to lookup the password should conform to the schema. */
			Secret.password_lookup(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
			                       null, on_password_lookup);
			]]></programlisting></informalexample>

			<para>This next example looks up a password synchronously. The function
			call will block until the lookup completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			/* The attributes used to lookup the password should conform to the schema. */
			var password = Secret.password_lookup_sync(EXAMPLE_SCHEMA,
			                                           { "number": "8", "even": "true" },
			                                           null);

			/* password will be null, if no matching password found */ 
			]]></programlisting></informalexample>
		</section>

		<section id="js-remove-example">
			<title>Javascript example: Remove a password</title>

			<para>Here's how to remove a password from the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to find which password to remove. If multiple passwords match the
			attributes, then the one stored most recently is removed.</para>

			<para>These examples use <link linkend="js-schema-example">the example
			schema</link>.</para>

			<para>This first example removes a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			function on_password_remove(source, result) {
				var removed = Secret.password_remove_finish(result);
				/* removed will be true if the password was removed */
			}

			/* The attributes used to lookup which password to remove should conform to the schema. */
			Secret.password_remove(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
			                       null, on_password_remove);
			]]></programlisting></informalexample>

			<para>This next example removes a password synchronously. The function
			call will block until the removal completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="javascript"><![CDATA[
			const Secret = imports.gi.Secret;

			/* The attributes used to lookup which password to remove should conform to the schema. */
			var removed = Secret.password_remove_sync(EXAMPLE_SCHEMA,
			                                          { "number": "8", "even": "true" },
			                                          null);

			/* removed will be true if the password was removed */
			]]></programlisting></informalexample>
		</section>

	</chapter>

	<chapter id="py-examples">
		<title>Python examples</title>

		<section id="py-schema-example">
			<title>Python example: Define a password schema</title>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The names and types of the attributes
			are defined in a schema. The schema is usually defined once globally.
			Here's how to define a schema:</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			EXAMPLE_SCHEMA = Secret.Schema.new("org.mock.type.Store",
				Secret.SchemaFlags.NONE,
				{
					"number": Secret.SchemaAttributeType.INTEGER,
					"string": Secret.SchemaAttributeType.STRING,
					"even": Secret.SchemaAttributeType.BOOLEAN,
				}
			)
			]]></programlisting></informalexample>

			<para>See the <link linkend="py-store-example">other examples</link> for how
			to use the schema.</para>
		</section>

		<section id="py-store-example">
			<title>Python example: Store a password</title>

			<para>Here's how to store a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are later
			used to lookup the password. The attributes should not contain
			secrets, as they are not stored in an encrypted fashion.</para>

			<para>These examples use <link linkend="py-schema-example">the example
			schema</link>.</para>

			<para>This first example stores a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			def on_password_stored(source, result, unused):
				Secret.password_store_finish(result)
				# ... do something now that the password has been stored

			# The attributes used to later lookup the password. These
			# attributes should conform to the schema.
			attributes = {
				"number": "8",
				"string": "eight",
				"even": "true"
			}

			Secret.password_store(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
			                      "The label", "the password", None, on_password_stored)
			]]></programlisting></informalexample>

			<para>This next example stores a password synchronously. The function
			call will block until the password is stored. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			# The attributes used to later lookup the password. These
			# attributes should conform to the schema.
			attributes = {
				"number": "8",
				"string": "eight",
				"even": "true"
			}

			Secret.password_store_sync(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
			                           "The label", "the password", None)
			]]></programlisting></informalexample>
		</section>

		<section id="py-lookup-example">
			<title>Python example: Lookup a password</title>

			<para>Here's how to lookup a password in the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to lookup the password. If multiple passwords match the
			lookup attributes, then the one stored most recently is returned.</para>

			<para>These examples use <link linkend="py-schema-example">the example
			schema</link>.</para>

			<para>This first example looks up a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			def on_password_lookup(source, result, unused):
				password = Secret.password_lookup_finish(result)
				# password will be null, if no matching password found

			Secret.password_lookup(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
			                       None, on_password_lookup)
			]]></programlisting></informalexample>

			<para>This next example looks up a password synchronously. The function
			call will block until the lookup completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			password = Secret.password_lookup_sync(EXAMPLE_SCHEMA, { "number": "8", "even": "true" }, None)
			# password will be null, if no matching password found
			]]></programlisting></informalexample>
		</section>

		<section id="py-remove-example">
			<title>Python example: Remove a password</title>

			<para>Here's how to remove a password from the running secret service,
			like gnome-keyring or ksecretservice.</para>

			<para>Each stored password has a set of attributes which are
			used to find which password to remove. If multiple passwords match the
			attributes, then the one stored most recently is removed.</para>

			<para>These examples use <link linkend="py-schema-example">the example
			schema</link>.</para>

			<para>This first example removes a password asynchronously, and is
			appropriate for GUI applications so that the UI does not block.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			def on_password_remove(source, result, unused):
				removed = Secret.password_remove_finish(result)
				# removed will be true if the password was removed

			Secret.password_remove(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
                                               None, on_password_remove)
			]]></programlisting></informalexample>

			<para>This next example removes a password synchronously. The function
			call will block until the removal completes. So this is appropriate for
			non GUI applications.</para>

			<informalexample><programlisting language="python"><![CDATA[
			from gi.repository import Secret

			removed = Secret.password_remove_sync(EXAMPLE_SCHEMA, { "number": "8", "even": "true" }, None)
			# removed will be true if the password was removed
			]]></programlisting></informalexample>
		</section>

	</chapter>

</part>