Working libsecret keystore
[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 public class KeyringStore : KeyringStoreBase {
38     private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
39
40     /* clear all keyring-stored ids (in preparation to store current list) */
41     protected override void clear_keyring() {
42       GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
43       match.append_string(keyring_store_attribute, keyring_store_version);
44       GLib.List<GnomeKeyring.Found> items;
45       GnomeKeyring.find_items_sync(item_type, match, out items);
46       foreach(unowned GnomeKeyring.Found entry in items) {
47         GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
48         if (result != GnomeKeyring.Result.OK) {
49           stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
50         }
51       }
52     }
53
54     protected override void load_id_cards() {
55         id_card_list.clear();
56
57         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
58         match.append_string(keyring_store_attribute, keyring_store_version);
59         GLib.List<GnomeKeyring.Found> items;
60         GnomeKeyring.find_items_sync(item_type, match, out items);
61         foreach(unowned GnomeKeyring.Found entry in items) {
62             KeyringStoreBase.Attributes new_attrs = new KeyringStoreBase.Attributes();                  
63             for (int i = 0; i < entry.attributes.len; i++) {
64                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
65                 if (attribute.type == GnomeKeyring.AttributeType.STRING) {
66                     unowned string value = attribute.string_value;
67                     new_attrs.insert(attribute.name, value);
68                 }
69             }
70             
71             var id_card = deserialize(new_attrs, entry.secret);
72             
73             id_card_list.add(id_card);
74         }
75     }
76
77     internal override void store_id_cards() {
78         logger.trace("store_id_cards");
79         clear_keyring();
80         foreach (IdCard id_card in this.id_card_list) {
81             /* workaround for Centos vala array property bug: use temp array */
82             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
83             uint32 item_id;
84             var hash_attrs = serialize(id_card);
85             hash_attrs.foreach((k, v) => {
86                     attributes.append_string(k,v); });
87             
88             attributes.append_string(keyring_store_attribute, keyring_store_version);
89
90             GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
91                                                                        item_type, id_card.display_name, attributes,
92                                                                        id_card.store_password ? id_card.password : "",
93                                                                        true, out item_id);
94             if (result != GnomeKeyring.Result.OK) {
95                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
96             }
97         }
98         try {
99             load_id_cards();
100         } catch(GLib.Error e) {
101             logger.error(@"Unable to load ID cards: $(e.message)\n");
102         }
103         
104     }
105
106     public static bool is_available()
107     {
108         return GnomeKeyring.is_available();
109     }
110     
111 }
112
113 #endif