-class IdentitiesManager : Object {
-
- public SList<IdCard> id_card_list;
-
- private const string FILE_NAME = "identities.txt";
-
- public IdentitiesManager ()
- {
- var key_file = new KeyFile ();
-
- var path = get_data_dir ();
- var filename = Path.build_filename (path, FILE_NAME);
-
- try
- {
- key_file.load_from_file (filename, KeyFileFlags.NONE);
- }
- catch (Error e)
- {
- stdout.printf("Error: %s\n", e.message);
+using Gee;
+
+public class Password {
+#if GNOME_KEYRING
+ private unowned string _password;
+ public string password {
+ get {
+ return _password;
}
-
- var identities_uris = key_file.get_groups ();
- foreach (string identity in identities_uris)
- {
- try
- {
- IdCard id_card = new IdCard ();
-
- id_card.issuer = key_file.get_string (identity, "Issuer");
- id_card.username = key_file.get_string (identity, "Username");
- id_card.password = key_file.get_string (identity, "Password");
- id_card.services = key_file.get_string_list (identity, "Services");
- id_card.nai = id_card.username + "@" + id_card.issuer;
-
- id_card_list.prepend (id_card);
- }
- catch (Error e)
- {
- stdout.printf ("Error: %s\n", e.message);
+ set {
+ if (_password != null) {
+ GnomeKeyring.memory_free((void *)_password);
+ _password = null;
}
+ if (value != null)
+ _password = GnomeKeyring.memory_strdup(value);
}
}
+#else
+ public string password { get; set; default = null; }
+#endif
- public void store_id_cards ()
- {
- var key_file = new KeyFile ();
+ public Password(string in_password) {
+ password = in_password;
+ }
- foreach (IdCard id_card in this.id_card_list)
- {
- key_file.set_string (id_card.issuer, "Issuer", id_card.issuer);
- key_file.set_string (id_card.issuer, "Username", id_card.username);
- key_file.set_string (id_card.issuer, "Password", id_card.password);
- key_file.set_string_list (id_card.issuer, "Services", id_card.services);
- }
+ ~Password() {
+ password = null;
+ }
+}
- var text = key_file.to_data (null);
+public class PasswordHashTable : Object {
+ private HashTable<string, Password> password_table;
- try
- {
- var path = get_data_dir ();
- var filename = Path.build_filename (path, FILE_NAME);
- FileUtils.set_contents (filename, text, -1);
- }
- catch (Error e)
- {
- stdout.printf ("Error: %s\n", e.message);
- }
+ private static string ComputeHashKey(IdCard card, IIdentityCardStore store) {
+ return "%s_store_%d".printf( card.display_name, store.get_store_type() );
}
- private string get_data_dir()
- {
- string path;
+ public void CachePassword(IdCard card, IIdentityCardStore store) {
+ password_table.replace(ComputeHashKey(card, store), new Password(card.password));
+ }
- path = Path.build_filename (Environment.get_user_data_dir (),
- Config.PACKAGE_TARNAME);
- if (!FileUtils.test (path, FileTest.EXISTS))
- {
- DirUtils.create (path, 0700);
+ public void RemovePassword(IdCard card, IIdentityCardStore store) {
+ password_table.remove(ComputeHashKey(card, store));
+ }
+ public void RetrievePassword(IdCard card, IIdentityCardStore store) {
+ weak Password password = password_table.lookup(ComputeHashKey(card, store));
+ if (password != null) {
+ card.password = password.password;
}
+ }
+ public PasswordHashTable() {
+ password_table = new HashTable<string, Password>(GLib.str_hash, GLib.str_equal);
+ }
+}
- return path;
+public class IdentityManagerModel : Object {
+ private const string FILE_NAME = "identities.txt";
+ private PasswordHashTable password_table;
+ private IIdentityCardStore store;
+ public LinkedList<IdCard> get_card_list() {
+ var identities = store.get_card_list();
+ identities.sort( (a, b) => {
+ IdCard id_a = (IdCard )a;
+ IdCard id_b = (IdCard )b;
+ if (id_a.IsNoIdentity() && !id_b.IsNoIdentity()) {
+ return -1;
+ } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
+ return 1;
+ }
+ return strcmp(id_a.display_name, id_b.display_name);
+ });
+ if (identities.is_empty || !identities[0].IsNoIdentity())
+ identities.insert(0, IdCard.NewNoIdentity());
+ foreach (IdCard id_card in identities) {
+ if (!id_card.store_password) {
+ password_table.RetrievePassword(id_card, store);
+ }
+ }
+ return identities;
}
+ public signal void card_list_changed();
- public IdCard? load_gss_eap_id_file ()
+ /* This method finds a valid display name */
+ public bool display_name_is_valid (string name,
+ out string? candidate)
{
- IdCard id_card = new IdCard();
- string text;
- string id_card_data[2];
-
- var filename = Path.build_filename (Environment.get_home_dir (),
- ".gss_eap_id");
- try {
- FileUtils.get_contents (filename, out text, null);
- }
- catch (Error e)
+ candidate = null;
+ foreach (IdCard id_card in this.get_card_list())
{
- return null;
+ if (id_card.display_name == name)
+ {
+ if (&candidate != null)
+ {
+ for (int i=0; i<1000; i++)
+ {
+ string tmp = "%s %d".printf (name, i);
+ if (display_name_is_valid (tmp, null))
+ {
+ candidate = tmp;
+ break;
+ }
+ }
+ }
+ return false;
+ }
}
-
- if (text == "")
- return null;
-
- id_card_data = text.split ("\n", 2);
- if (id_card_data[1] != "")
- id_card.password = id_card_data[1];
- id_card_data = id_card_data[0].split ("@", 2);
- id_card.username = id_card_data[0];
- id_card.issuer = id_card_data[1];
- id_card.services = {"email","jabber","irc"};
- id_card.nai = id_card.username + "@" + id_card.issuer;
- id_card.pixbuf = find_icon ("avatar-default", 48);
-
- return id_card;
+ return true;
}
- public void store_gss_eap_id_file (IdCard ?id_card)
- {
- string text = "";
+ public void add_card(IdCard card, bool force_flat_file_store) {
+ if (card.temporary)
+ return;
+
+ string candidate;
+ IIdentityCardStore.StoreType saved_store_type = get_store_type();
- if (id_card != null)
- text = id_card.username + "@" + id_card.issuer + "\n" + id_card.password;
+ if (force_flat_file_store)
+ set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
- var filename = Path.build_filename (Environment.get_home_dir (),
- ".gss_eap_id");
- try
+ if (!display_name_is_valid (card.display_name, out candidate))
{
- FileUtils.set_contents (filename, text, -1);
+ card.display_name = candidate;
}
- catch (Error e)
- {
- stdout.printf ("Error: %s\n", e.message);
+
+ if (!card.store_password)
+ password_table.CachePassword(card, store);
+ store.add_card(card);
+ set_store_type(saved_store_type);
+ card_list_changed();
+ }
+
+ public IdCard update_card(IdCard card) {
+ IdCard retval;
+ if (card.temporary) {
+ retval = card;
+ return retval;
}
+
+ if (!card.store_password)
+ password_table.CachePassword(card, store);
+ else
+ password_table.RemovePassword(card, store);
+ retval = store.update_card(card);
+ card_list_changed();
+ return retval;
+ }
+
+ public void remove_card(IdCard card) {
+ password_table.RemovePassword(card, store);
+ store.remove_card(card);
+ card_list_changed();
+ }
+
+ public void set_store_type(IIdentityCardStore.StoreType type) {
+ if ((store != null) && (store.get_store_type() == type))
+ return;
+ switch (type) {
+#if GNOME_KEYRING
+ case IIdentityCardStore.StoreType.KEYRING:
+ store = new KeyringStore();
+ break;
+#endif
+ case IIdentityCardStore.StoreType.FLAT_FILE:
+ default:
+ store = new LocalFlatFileStore();
+ break;
+ }
+ }
+
+ public IIdentityCardStore.StoreType get_store_type() {
+ return store.get_store_type();
+ }
+
+ public bool HasNonTrivialIdentities() {
+ foreach (IdCard card in this.store.get_card_list()) {
+ // The 'NoIdentity' card is non-trivial if it has services or rules.
+ // All other cards are automatically non-trivial.
+ if ((!card.IsNoIdentity()) ||
+ (card.services.length > 0) ||
+ (card.rules.length > 0)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ private IdentityManagerApp parent;
+
+ public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
+ parent = parent_app;
+ password_table = new PasswordHashTable();
+ set_store_type(store_type);
}
}