Some formatting changes
[moonshot-ui.git] / src / moonshot-keyring-store-gnome.vala
1 /*
2 * Copyright (C) 2018   Sam Hartman
3 * Copyright (c) 2011-2016, JANET(UK)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of JANET(UK) nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32 */
33
34 #if GNOME_KEYRING
35
36 using Gee;
37
38 public class KeyringStore : KeyringStoreBase {
39     private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
40
41     /* clear all keyring-stored ids (in preparation to store current list) */
42     protected override void clear_keyring() {
43         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
44         match.append_string(keyring_store_attribute, keyring_store_version);
45         GLib.List<GnomeKeyring.Found> items;
46         GnomeKeyring.find_items_sync(item_type, match, out items);
47         foreach(unowned GnomeKeyring.Found entry in items) {
48             GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
49             if (result != GnomeKeyring.Result.OK) {
50                 stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
51             }
52         }
53     }
54
55     protected override void load_id_cards() {
56         id_card_list.clear();
57
58         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
59         match.append_string(keyring_store_attribute, keyring_store_version);
60         GLib.List<GnomeKeyring.Found> items;
61         GnomeKeyring.find_items_sync(item_type, match, out items);
62         foreach(unowned GnomeKeyring.Found entry in items) {
63             KeyringStoreBase.Attributes new_attrs = new KeyringStoreBase.Attributes();
64             for (int i = 0; i < entry.attributes.len; i++) {
65                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
66                 if (attribute.type == GnomeKeyring.AttributeType.STRING) {
67                     unowned string value = attribute.string_value;
68                     new_attrs.insert(attribute.name, value);
69                 }
70             }
71
72             var id_card = deserialize(new_attrs, entry.secret);
73
74             id_card_list.add(id_card);
75         }
76     }
77
78     internal override void store_id_cards() {
79         logger.trace("store_id_cards");
80         clear_keyring();
81         foreach (IdCard id_card in this.id_card_list) {
82             /* workaround for Centos vala array property bug: use temp array */
83             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
84             uint32 item_id;
85             var hash_attrs = serialize(id_card);
86                 hash_attrs.foreach((k, v) => {
87                 attributes.append_string((string) k, (string) v);
88             });
89
90             attributes.append_string(keyring_store_attribute, keyring_store_version);
91
92             GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
93                                                                        item_type, id_card.display_name, attributes,
94                                                                        id_card.store_password ? id_card.password : "",
95                                                                        true, out item_id);
96             if (result != GnomeKeyring.Result.OK) {
97                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
98             }
99         }
100         try {
101             load_id_cards();
102         } catch(GLib.Error e) {
103             logger.error(@"Unable to load ID cards: $(e.message)\n");
104         }
105
106     }
107
108     public static bool is_available()
109     {
110         return GnomeKeyring.is_available();
111     }
112
113 }
114
115 #endif