From 6f739fde180ac3af1b1283be04b31d50e49d371c Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 15 Oct 2024 23:49:34 +0200 Subject: [PATCH] just use the keyring for the rest of the authn lmao --- src/ui/setup.vala | 104 +++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 65 deletions(-) diff --git a/src/ui/setup.vala b/src/ui/setup.vala index 55d34d2..7fc9a93 100644 --- a/src/ui/setup.vala +++ b/src/ui/setup.vala @@ -16,10 +16,6 @@ public class Ui.Setup : Adw.PreferencesDialog { public signal void connected (Subsonic api); - private Sqlite.Database db; - private Sqlite.Statement db_get; - private Sqlite.Statement db_set; - private static Secret.Schema secret_schema = new Secret.Schema ( "eu.callcc.audrey", Secret.SchemaFlags.NONE, @@ -27,30 +23,6 @@ public class Ui.Setup : Adw.PreferencesDialog { "username", Secret.SchemaAttributeType.STRING ); - construct { - var app_config_dir = Path.build_filename (Environment.get_user_config_dir (), "audrey"); - - try { - File.new_build_filename (app_config_dir).make_directory_with_parents (); - } catch (Error e) { - // just ignore if the directory already existed - } - - int ec = Sqlite.Database.open (Path.build_filename (app_config_dir, "config.db"), out this.db); - assert (ec == Sqlite.OK); - - ec = this.db.exec (""" - CREATE TABLE IF NOT EXISTS Setup (key UNIQUE, value); - """, null, null); - assert (ec == Sqlite.OK); - - ec = this.db.prepare_v2 ("""SELECT value FROM Setup WHERE key IS ?1""", -1, out this.db_get); - assert (ec == Sqlite.OK); - - ec = this.db.prepare_v2 ("""INSERT OR REPLACE INTO Setup VALUES (?1, ?2)""", -1, out this.db_set); - assert (ec == Sqlite.OK); - } - [GtkCallback] private void on_authn_changed () { this.authn_can_validate = true; } @@ -87,35 +59,47 @@ public class Ui.Setup : Adw.PreferencesDialog { } public void load () { - this.db_get.bind_text (1, "server_url"); - if (this.db_get.step () == Sqlite.ROW) { - this.server_url = this.db_get.column_text (0); - } else { - this.server_url = ""; - } - assert (this.db_get.reset () == Sqlite.OK); - - this.db_get.bind_text (1, "username"); - if (this.db_get.step () == Sqlite.ROW) { - this.username = this.db_get.column_text (0); - } else { - this.username = ""; - } - assert (this.db_get.reset () == Sqlite.OK); - this.authn_can_edit = false; - Secret.password_lookup.begin (secret_schema, null, (obj, res) => { - try { - string? password = Secret.password_lookup.end (res); - this.password = password ?? ""; - } catch (Error e) { - error ("could not look up password in keyring: %s", e.message); - } + Secret.password_searchv.begin ( + secret_schema, + new HashTable (null, null), + Secret.SearchFlags.NONE, + null, + (obj, res) => { + try { + var list = Secret.password_searchv.end (res); + if (list == null) { + // didn't find shit, leave all empty + this.server_url = ""; + this.username = ""; + this.password = ""; + // TODO: onboarding + this.authn_can_edit = true; + this.authn_can_validate = true; + } else { + var first = list.data; + assert (first != null); - // first connection - this.authn_can_validate = true; - this.on_authn_validate_activated (); - }, "server-url", this.server_url, "username", this.username); + this.server_url = first.attributes["server-url"]; + this.username = first.attributes["username"]; + + first.retrieve_secret.begin (null, (obj, res) => { + try { + var value = first.retrieve_secret.end (res); + this.password = value.get_text () ?? ""; + } catch (Error e) { + error ("could not retrieve password from credentials: %s", e.message); + } + + // first connection + this.authn_can_validate = true; + this.on_authn_validate_activated (); + }); + } + } catch (Error e) { + error ("could not search for password in keyring: %s", e.message); + } + }); } private void salt_password (string password, out string token, out string salt) { @@ -136,16 +120,6 @@ public class Ui.Setup : Adw.PreferencesDialog { } public void save () { - this.db_set.bind_text (1, "server_url"); - this.db_set.bind_text (2, this.server_url); - assert (this.db_set.step () == Sqlite.DONE); - assert (this.db_set.reset () == Sqlite.OK); - - this.db_set.bind_text (1, "username"); - this.db_set.bind_text (2, this.username); - assert (this.db_set.step () == Sqlite.DONE); - assert (this.db_set.reset () == Sqlite.OK); - this.authn_can_edit = false; Secret.password_store.begin (secret_schema, null, "Subsonic password", this.password, null, (obj, res) => { try {