d23764d8da8a56b21d1443197faebd0de32eb06e
[moonshot-ui.git] / src / moonshot-keyring-store-base.vala
1 /*
2  * Copyright (c) 2011-2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 using Gee;
33
34 #if GNOME_KEYRING || LIBSECRET_KEYRING
35
36 public abstract class KeyringStoreBase : Object, IIdentityCardStore {
37     protected static MoonshotLogger logger = get_logger("KeyringStore");
38
39     protected LinkedList<IdCard> id_card_list;
40     internal const string keyring_store_attribute = "Moonshot";
41     internal const string keyring_store_version = "1.0";
42
43     /*  
44      * This class is directly useful for the libsecret implementation.
45      * However, we convert the gnome keyring attributes into a HashTable
46      * so we can share the serialization code between the two
47      * implementations.  This ends up decreasing complexity even of the
48      * gnome keyring code
49     */
50     protected class Attributes: GLib.HashTable<string, string> {
51         public Attributes() {
52             base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
53       }
54
55     }
56
57     protected static Attributes match_attributes;
58
59     protected IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
60     {
61         IdCard id_card = new IdCard();
62         unowned string store_password = attrs["StorePassword"];
63         unowned string ca_cert = attrs["CA-Cert"] ?? "";
64         unowned string server_cert = attrs["Server-Cert"] ?? "";
65         unowned string subject = attrs["Subject"] ?? "";
66         unowned string subject_alt = attrs["Subject-Alt"] ?? "";
67         unowned string ta_datetime_added = attrs["TA_DateTime_Added"];
68
69         id_card.issuer = attrs["Issuer"];
70         id_card.username = attrs["Username"];
71         id_card.display_name = attrs["DisplayName"];
72         unowned string services = attrs["Services"];
73         if ((services != null) && services != "") {
74             id_card.update_services(services.split(";"));
75         }
76         var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
77         if (ta_datetime_added != null) {
78             ta.set_datetime_added(ta_datetime_added);
79             }
80         id_card.set_trust_anchor_from_store(ta);
81
82         unowned string rules_pattern_all = attrs["Rules-Pattern"];
83         unowned string rules_always_confirm_all = attrs["Rules-AlwaysConfirm"];
84         if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
85             string[] rules_patterns = rules_pattern_all.split(";");
86             string[] rules_always_confirm = rules_always_confirm_all.split(";");
87             if (rules_patterns.length == rules_always_confirm.length) {
88                 Rule[] rules = new Rule[rules_patterns.length];
89                 for (int i = 0; i < rules_patterns.length; i++) {
90                     rules[i].pattern = (owned) rules_patterns[i];
91                     rules[i].always_confirm = (owned) rules_always_confirm[i];
92                 }
93                 id_card.rules = rules;
94             }
95         }
96         
97         if (store_password != null)
98             id_card.store_password = (store_password == "yes");
99         else
100             id_card.store_password = ((secret != null) && (secret != ""));
101
102         if (id_card.store_password)
103             id_card.password = secret;
104         else
105             id_card.password = null;
106
107
108             
109
110         return id_card;
111     }
112         
113     class construct {
114         match_attributes = new Attributes();
115         match_attributes.insert(keyring_store_attribute, keyring_store_version);
116     }
117
118     public void add_card(IdCard card) {
119         logger.trace("add_card: Adding card '%s' with services: '%s'"
120                      .printf(card.display_name, card.get_services_string("; ")));
121
122         id_card_list.add(card);
123         store_id_cards();
124     }
125
126     public IdCard? update_card(IdCard card) {
127         logger.trace("update_card");
128
129         id_card_list.remove(card);
130         id_card_list.add(card);
131
132         store_id_cards();
133         foreach (IdCard idcard in id_card_list) {
134             if (idcard.display_name == card.display_name) {
135                 return idcard;
136             }
137         }
138
139         logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
140         return null;
141     }
142
143     public bool remove_card(IdCard card) {
144         bool retval = id_card_list.remove(card);
145         if (retval)
146             store_id_cards();
147         return retval;
148     }
149
150     public IIdentityCardStore.StoreType get_store_type() {
151         return IIdentityCardStore.StoreType.KEYRING;
152     }
153
154     public LinkedList<IdCard> get_card_list() {
155         return id_card_list;
156     }
157
158     protected abstract void clear_keyring();     
159     protected abstract void load_id_cards() throws GLib.Error;
160     internal abstract void store_id_cards();
161     
162
163
164     public KeyringStoreBase() {
165         id_card_list = new LinkedList<IdCard>();
166         try {
167             load_id_cards();
168         } catch( GLib.Error e) {
169             stdout.printf("Unable to load ID cards: %s\n", e.message);
170         }
171         
172     }
173 }
174
175 #endif