Some formatting changes libsecret_alex
authorAlejandro Perez <alex.perez-mendez@jisc.ac.uk>
Tue, 4 Dec 2018 09:50:14 +0000 (10:50 +0100)
committerAlejandro Perez <alex.perez-mendez@jisc.ac.uk>
Tue, 4 Dec 2018 10:26:03 +0000 (11:26 +0100)
src/moonshot-keyring-store-base.vala
src/moonshot-keyring-store-gnome.vala
src/moonshot-keyring-store-secret.vala

index c6e5e55..c87fef0 100644 (file)
@@ -48,108 +48,103 @@ public abstract class KeyringStoreBase : Object, IIdentityCardStore {
      * gnome keyring code
     */
     protected class Attributes: GLib.HashTable<string, string> {
-       public Attributes() {
-           base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
+        public Attributes() {
+            base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
         }
-
     }
 
     protected static Attributes match_attributes;
 
     protected static IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
     {
-       IdCard id_card = new IdCard();
-       unowned string store_password = attrs.lookup("StorePassword");
-       unowned string ca_cert = attrs.lookup("CA-Cert") ?? "";
-       unowned string server_cert = attrs.lookup("Server-Cert") ?? "";
-       unowned string subject = attrs.lookup("Subject") ?? "";
-       unowned string subject_alt = attrs.lookup("Subject-Alt") ?? "";
-       unowned string ta_datetime_added = attrs.lookup("TA_DateTime_Added");
-
-       id_card.issuer = attrs.lookup("Issuer");
-       id_card.username = attrs.lookup("Username");
-       id_card.display_name = attrs.lookup("DisplayName");
-       unowned string services = attrs.lookup("Services");
-       if ((services != null) && services != "") {
-           id_card.update_services(services.split(";"));
-       }
-       var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
-       if (ta_datetime_added != null) {
-           ta.set_datetime_added(ta_datetime_added);
+        IdCard id_card = new IdCard();
+        unowned string store_password = attrs.lookup("StorePassword");
+        unowned string ca_cert = attrs.lookup("CA-Cert") ?? "";
+        unowned string server_cert = attrs.lookup("Server-Cert") ?? "";
+        unowned string subject = attrs.lookup("Subject") ?? "";
+        unowned string subject_alt = attrs.lookup("Subject-Alt") ?? "";
+        unowned string ta_datetime_added = attrs.lookup("TA_DateTime_Added");
+
+        id_card.issuer = attrs.lookup("Issuer");
+        id_card.username = attrs.lookup("Username");
+        id_card.display_name = attrs.lookup("DisplayName");
+        unowned string services = attrs.lookup("Services");
+        if ((services != null) && services != "") {
+            id_card.update_services(services.split(";"));
+        }
+        var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
+        if (ta_datetime_added != null) {
+            ta.set_datetime_added(ta_datetime_added);
+        }
+        id_card.set_trust_anchor_from_store(ta);
+
+        unowned string rules_pattern_all = attrs.lookup("Rules-Pattern");
+        unowned string rules_always_confirm_all = attrs.lookup("Rules-AlwaysConfirm");
+        if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
+            string[] rules_patterns = rules_pattern_all.split(";");
+            string[] rules_always_confirm = rules_always_confirm_all.split(";");
+            if (rules_patterns.length == rules_always_confirm.length) {
+                Rule[] rules = new Rule[rules_patterns.length];
+                for (int i = 0; i < rules_patterns.length; i++) {
+                    rules[i].pattern = (owned) rules_patterns[i];
+                    rules[i].always_confirm = (owned) rules_always_confirm[i];
+                }
+                id_card.rules = rules;
             }
-       id_card.set_trust_anchor_from_store(ta);
-
-       unowned string rules_pattern_all = attrs.lookup("Rules-Pattern");
-       unowned string rules_always_confirm_all = attrs.lookup("Rules-AlwaysConfirm");
-       if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
-           string[] rules_patterns = rules_pattern_all.split(";");
-           string[] rules_always_confirm = rules_always_confirm_all.split(";");
-           if (rules_patterns.length == rules_always_confirm.length) {
-               Rule[] rules = new Rule[rules_patterns.length];
-               for (int i = 0; i < rules_patterns.length; i++) {
-                   rules[i].pattern = (owned) rules_patterns[i];
-                   rules[i].always_confirm = (owned) rules_always_confirm[i];
-               }
-               id_card.rules = rules;
-           }
-       }
-
-       if (store_password != null)
-           id_card.store_password = (store_password == "yes");
-       else
-           id_card.store_password = ((secret != null) && (secret != ""));
-
-       if (id_card.store_password)
-           id_card.password = secret;
-       else
-           id_card.password = null;
-
+        }
 
+        if (store_password != null)
+            id_card.store_password = (store_password == "yes");
+        else
+            id_card.store_password = ((secret != null) && (secret != ""));
 
+        if (id_card.store_password)
+            id_card.password = secret;
+        else
+            id_card.password = null;
 
-       return id_card;
+        return id_card;
     }
 
     internal static Attributes serialize(IdCard id_card)
     {
-       /* workaround for Centos vala array property bug: use temp array */
-       var rules = id_card.rules;
-       string[] rules_patterns = new string[rules.length];
-       string[] rules_always_conf = new string[rules.length];
-
-       for (int i = 0; i < rules.length; i++) {
-           rules_patterns[i] = rules[i].pattern;
-           rules_always_conf[i] = rules[i].always_confirm;
-       }
-       string patterns = string.joinv(";", rules_patterns);
-       string always_conf = string.joinv(";", rules_always_conf);
-       string services = id_card.get_services_string(";");
-       Attributes attributes = new Attributes();
-       attributes.insert(keyring_store_attribute, keyring_store_version);
-       attributes.insert("Issuer", id_card.issuer);
-       attributes.insert("Username", id_card.username);
-       attributes.insert("DisplayName", id_card.display_name);
-       attributes.insert("Services", services);
-       attributes.insert("Rules-Pattern", patterns);
-       attributes.insert("Rules-AlwaysConfirm", always_conf);
-       attributes.insert("CA-Cert", id_card.trust_anchor.ca_cert);
-       attributes.insert("Server-Cert", id_card.trust_anchor.server_cert);
-       attributes.insert("Subject", id_card.trust_anchor.subject);
-       attributes.insert("Subject-Alt", id_card.trust_anchor.subject_alt);
-       attributes.insert("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
-       attributes.insert("StorePassword", id_card.store_password ? "yes" : "no");
-       return attributes;
+        /* workaround for Centos vala array property bug: use temp array */
+        var rules = id_card.rules;
+        string[] rules_patterns = new string[rules.length];
+        string[] rules_always_conf = new string[rules.length];
+
+        for (int i = 0; i < rules.length; i++) {
+            rules_patterns[i] = rules[i].pattern;
+            rules_always_conf[i] = rules[i].always_confirm;
+        }
+        string patterns = string.joinv(";", rules_patterns);
+        string always_conf = string.joinv(";", rules_always_conf);
+        string services = id_card.get_services_string(";");
+        Attributes attributes = new Attributes();
+        attributes.insert(keyring_store_attribute, keyring_store_version);
+        attributes.insert("Issuer", id_card.issuer);
+        attributes.insert("Username", id_card.username);
+        attributes.insert("DisplayName", id_card.display_name);
+        attributes.insert("Services", services);
+        attributes.insert("Rules-Pattern", patterns);
+        attributes.insert("Rules-AlwaysConfirm", always_conf);
+        attributes.insert("CA-Cert", id_card.trust_anchor.ca_cert);
+        attributes.insert("Server-Cert", id_card.trust_anchor.server_cert);
+        attributes.insert("Subject", id_card.trust_anchor.subject);
+        attributes.insert("Subject-Alt", id_card.trust_anchor.subject_alt);
+        attributes.insert("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
+        attributes.insert("StorePassword", id_card.store_password ? "yes" : "no");
+        return attributes;
     }
 
     class construct {
-       match_attributes = new Attributes();
-       match_attributes.insert(keyring_store_attribute, keyring_store_version);
+        match_attributes = new Attributes();
+        match_attributes.insert(keyring_store_attribute, keyring_store_version);
     }
 
     public void add_card(IdCard card) {
         logger.trace("add_card: Adding card '%s' with services: '%s'"
                      .printf(card.display_name, card.get_services_string("; ")));
-
         id_card_list.add(card);
         store_id_cards();
     }
@@ -190,17 +185,13 @@ public abstract class KeyringStoreBase : Object, IIdentityCardStore {
     protected abstract void load_id_cards() throws GLib.Error;
     internal abstract void store_id_cards();
 
-
-
-
     public KeyringStoreBase() {
         id_card_list = new LinkedList<IdCard>();
         try {
-           load_id_cards();
-       } catch( GLib.Error e) {
-           stdout.printf("Unable to load ID cards: %s\n", e.message);
-       }
-
+            load_id_cards();
+        } catch( GLib.Error e) {
+            stdout.printf("Unable to load ID cards: %s\n", e.message);
+        }
     }
 }
 
index bf1297b..a101fc1 100644 (file)
 #if GNOME_KEYRING
 
 using Gee;
+
 public class KeyringStore : KeyringStoreBase {
     private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
 
     /* clear all keyring-stored ids (in preparation to store current list) */
     protected override void clear_keyring() {
-      GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
-      match.append_string(keyring_store_attribute, keyring_store_version);
-      GLib.List<GnomeKeyring.Found> items;
-      GnomeKeyring.find_items_sync(item_type, match, out items);
-      foreach(unowned GnomeKeyring.Found entry in items) {
-       GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
-       if (result != GnomeKeyring.Result.OK) {
-         stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
-       }
-      }
+        GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
+        match.append_string(keyring_store_attribute, keyring_store_version);
+        GLib.List<GnomeKeyring.Found> items;
+        GnomeKeyring.find_items_sync(item_type, match, out items);
+        foreach(unowned GnomeKeyring.Found entry in items) {
+            GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
+            if (result != GnomeKeyring.Result.OK) {
+                stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
+            }
+        }
     }
 
     protected override void load_id_cards() {
@@ -59,16 +60,16 @@ public class KeyringStore : KeyringStoreBase {
         GLib.List<GnomeKeyring.Found> items;
         GnomeKeyring.find_items_sync(item_type, match, out items);
         foreach(unowned GnomeKeyring.Found entry in items) {
-           KeyringStoreBase.Attributes new_attrs = new KeyringStoreBase.Attributes();
+            KeyringStoreBase.Attributes new_attrs = new KeyringStoreBase.Attributes();
             for (int i = 0; i < entry.attributes.len; i++) {
                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
                 if (attribute.type == GnomeKeyring.AttributeType.STRING) {
-                   unowned string value = attribute.string_value;
-                   new_attrs.insert(attribute.name, value);
+                    unowned string value = attribute.string_value;
+                    new_attrs.insert(attribute.name, value);
                 }
-           }
+            }
 
-           var id_card = deserialize(new_attrs, entry.secret);
+            var id_card = deserialize(new_attrs, entry.secret);
 
             id_card_list.add(id_card);
         }
@@ -82,7 +83,7 @@ public class KeyringStore : KeyringStoreBase {
             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
             uint32 item_id;
             var hash_attrs = serialize(id_card);
-               hash_attrs.foreach((k, v) => {
+                hash_attrs.foreach((k, v) => {
                 attributes.append_string((string) k, (string) v);
             });
 
@@ -96,17 +97,17 @@ public class KeyringStore : KeyringStoreBase {
                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
             }
         }
-       try {
-           load_id_cards();
-       } catch(GLib.Error e) {
-           logger.error(@"Unable to load ID cards: $(e.message)\n");
-       }
+        try {
+            load_id_cards();
+        } catch(GLib.Error e) {
+            logger.error(@"Unable to load ID cards: $(e.message)\n");
+        }
 
     }
 
     public static bool is_available()
     {
-       return GnomeKeyring.is_available();
+        return GnomeKeyring.is_available();
     }
 
 }
index d76ae74..381e4b4 100644 (file)
@@ -39,14 +39,14 @@ private Collection? find_secret_collection()
 {
     Collection secret_collection = null;
     try {
-       Service service = Service.get_sync(ServiceFlags.OPEN_SESSION);
-       secret_collection = Collection.for_alias_sync(service, COLLECTION_DEFAULT,
-                                                     CollectionFlags.NONE);
+        Service service = Service.get_sync(ServiceFlags.OPEN_SESSION);
+        secret_collection = Collection.for_alias_sync(service, COLLECTION_DEFAULT,
+                                                      CollectionFlags.NONE);
     } catch(GLib.Error e) {
-       stdout.printf("Unable to load secret service: %s\n", e.message);
-       }
-    return secret_collection;
+        stdout.printf("Unable to load secret service: %s\n", e.message);
     }
+    return secret_collection;
+}
 
 public class KeyringStore : KeyringStoreBase {
     /*
@@ -57,58 +57,53 @@ public class KeyringStore : KeyringStoreBase {
     */
     private const SchemaAttributeType sstring = SchemaAttributeType.STRING;
     private static Schema schema = new Schema("org.freedesktop.Secret.Generic", SchemaFlags.NONE,
-                                             "Moonshot", sstring,
-                                             "Issuer", sstring,
-                                             "Username", sstring,
-                                             "DisplayName", sstring,
-                                             "Services", sstring,
-                                             "Rules-Pattern", sstring,
-                                             "Rules-AlwaysConfirm", sstring,
-                                             "CA-Cert", sstring,
-                                             "Server-Cert", sstring,
-                                             "Subject", sstring,
-                                             "Subject-Alt", sstring,
-                                             "TA_DateTime_Added", sstring,
-                                             "StorePassword", sstring);
+                                              "Moonshot", sstring,
+                                              "Issuer", sstring,
+                                              "Username", sstring,
+                                              "DisplayName", sstring,
+                                              "Services", sstring,
+                                              "Rules-Pattern", sstring,
+                                              "Rules-AlwaysConfirm", sstring,
+                                              "CA-Cert", sstring,
+                                              "Server-Cert", sstring,
+                                              "Subject", sstring,
+                                              "Subject-Alt", sstring,
+                                              "TA_DateTime_Added", sstring,
+                                              "StorePassword", sstring);
     private static Collection? secret_collection = find_secret_collection();
 
-
-
-
     /* clear all keyring-stored ids (in preparation to store current list) */
     protected override void clear_keyring() {
-       GLib.List<Item> items;
-       try {
-           items = secret_collection.search_sync(schema, match_attributes,
-                                                 SearchFlags.ALL);
-       } catch (GLib.Error e) {
-           stdout.printf("Failed to find items to delete: %s\n", e.message);
-           return;
-       }
-        foreach(unowned Item  entry in items) {
-           try {
-               bool res = entry.delete_sync();
-               if (!res) {
-                   stdout.printf("Failed to delete item: %s\n", entry.get_label());
-               }
-           } catch (GLib.Error e) {
-               stdout.printf("Error deleting item: %s\n", e.message);
-           }
-       }
+        GLib.List<Item> items;
+        try {
+            items = secret_collection.search_sync(schema, match_attributes, SearchFlags.ALL);
+        } catch (GLib.Error e) {
+            stdout.printf("Failed to find items to delete: %s\n", e.message);
+            return;
+        }
+        foreach(unowned Item entry in items) {
+            try {
+                bool res = entry.delete_sync();
+                if (!res) {
+                    stdout.printf("Failed to delete item: %s\n", entry.get_label());
+                }
+            } catch (GLib.Error e) {
+                stdout.printf("Error deleting item: %s\n", e.message);
+            }
+        }
     }
 
     protected override void load_id_cards()  throws GLib.Error {
         id_card_list.clear();
 
-       GLib.List<Item> items = secret_collection.search_sync(
-                                                             schema, match_attributes,
-                                                             SearchFlags.UNLOCK|SearchFlags.LOAD_SECRETS|SearchFlags.ALL);
+        GLib.List<Item> items = secret_collection.search_sync(schema, match_attributes,
+                                                              SearchFlags.UNLOCK|SearchFlags.LOAD_SECRETS|SearchFlags.ALL);
         foreach(unowned Item entry in items) {
-           var secret = entry.get_secret();
-           string secret_text = null;
-           if (secret != null)
-               secret_text = secret.get_text();
-           var id_card = deserialize(entry.attributes, secret_text);
+            var secret = entry.get_secret();
+            string secret_text = null;
+            if (secret != null)
+                secret_text = secret.get_text();
+            var id_card = deserialize(entry.attributes, secret_text);
             id_card_list.add(id_card);
         }
     }
@@ -117,30 +112,29 @@ public class KeyringStore : KeyringStoreBase {
         logger.trace("store_id_cards");
         clear_keyring();
         foreach (IdCard id_card in this.id_card_list) {
-           try {
-var attributes = serialize(id_card);
-               password_storev_sync(schema, attributes, null, id_card.display_name,
-                                    id_card.store_password?id_card.password: "");
-           } catch(GLib.Error e) {
-               logger.error(@"Unable to store $(id_card.display_name): $(e.message)\n");
-       }
-
+            try {
+                var attributes = serialize(id_card);
+                password_storev_sync(schema, attributes, null, id_card.display_name,
+                                     id_card.store_password?id_card.password: "");
+            } catch(GLib.Error e) {
+                logger.error(@"Unable to store $(id_card.display_name): $(e.message)\n");
+            }
         }
         try {
-           load_id_cards();
-       } catch (GLib.Error e) {
-           logger.error(@"Unable to load ID Cards: $(e.message)\n");
-       }
+            load_id_cards();
+        } catch (GLib.Error e) {
+            logger.error(@"Unable to load ID Cards: $(e.message)\n");
+        }
 
     }
 
     public static bool is_available()
     {
-       if (secret_collection == null) {
-           secret_collection = find_secret_collection();
-       }
+        if (secret_collection == null) {
+            secret_collection = find_secret_collection();
+        }
 
-       return secret_collection != null;
+        return secret_collection != null;
     }
 
 }