[geary/wip/768975-service-info] ServiceInformation: differentiate between auth methods and providers



commit e001e480cf01855ed04e85e1c52989caccc1d90e
Author: Oskar Viljasaar <oskar viljasaar gmail com>
Date:   Sun Oct 15 15:38:28 2017 +0200

    ServiceInformation: differentiate between auth methods and providers
    
    This polishes what is present in ServiceInformation. ServiceInformation
    now has two enumerators to represent how to authenticate with the server,
    and where it should get credentials from.
    
    LocalServiceInformation will now try to load keys related to these from
    its config file, if those are not found then default values (libsecret
    provider, password authentication) are used.
    
    This also fixes a bug where LocalServiceInformation was loading but not
    saving IMAP and SMTP usernames.

 src/engine/api/geary-config.vala                   |    2 +
 .../api/geary-local-service-information.vala       |   16 ++++-
 src/engine/api/geary-service-information.vala      |   72 +++++++++++++++++++-
 3 files changed, 86 insertions(+), 4 deletions(-)
---
diff --git a/src/engine/api/geary-config.vala b/src/engine/api/geary-config.vala
index 30503ab..8e59b67 100644
--- a/src/engine/api/geary-config.vala
+++ b/src/engine/api/geary-config.vala
@@ -36,6 +36,8 @@ namespace Geary.Config {
     public const string SAVE_DRAFTS_KEY = "save_drafts";
     public const string USE_EMAIL_SIGNATURE_KEY = "use_email_signature";
     public const string EMAIL_SIGNATURE_KEY = "email_signature";
+    public const string CREDENTIALS_PROVIDER_KEY = "credentials_provider";
+    public const string CREDENTIALS_METHOD_KEY = "credentials_method";
 
     public static string get_string_value(KeyFile key_file, string group, string key, string def = "") {
         try {
diff --git a/src/engine/api/geary-local-service-information.vala 
b/src/engine/api/geary-local-service-information.vala
index aef3bfa..52de800 100644
--- a/src/engine/api/geary-local-service-information.vala
+++ b/src/engine/api/geary-local-service-information.vala
@@ -17,7 +17,8 @@ public class Geary.LocalServiceInformation : Geary.ServiceInformation {
         this.service = service;
         this.file = config_directory.get_child(Geary.AccountInformation.SETTINGS_FILENAME);
         this.mediator = mediator;
-        this.credentials_method = "METHOD_LIBSECRET";
+        this.credentials_provider = CredentialsProvider.LIBSECRET;
+        this.credentials_method = CredentialsMethod.PASSWORD;
     }
 
     public override void load_settings(KeyFile? existing = null) throws Error {
@@ -63,6 +64,14 @@ public class Geary.LocalServiceInformation : Geary.ServiceInformation {
             key_file, Geary.Config.GROUP, use_ssl_key, this.use_ssl);
         this.use_starttls = Geary.Config.get_bool_value(
             key_file, Geary.Config.GROUP, use_starttls_key, this.use_starttls);
+
+        /* If the credentials provider and method keys are not in the config file,
+         * assume we have the libsecret credentials provider using plain password auth.
+         * Write these values back later when saving the configuration. */
+        this.credentials_provider = CredentialsProvider.from_string(Geary.Config.get_string_value(
+            key_file, Geary.Config.GROUP, Geary.Config.CREDENTIALS_PROVIDER_KEY, 
CredentialsProvider.LIBSECRET.to_string()));
+        this.credentials_method = CredentialsMethod.from_string(Geary.Config.get_string_value(
+            key_file, Geary.Config.GROUP, Geary.Config.CREDENTIALS_METHOD_KEY, 
CredentialsMethod.PASSWORD.to_string()));
     }
 
     public override void load_credentials(KeyFile? existing = null, string? email_address = null) throws 
Error {
@@ -92,12 +101,16 @@ public class Geary.LocalServiceInformation : Geary.ServiceInformation {
     }
 
     public override void save_settings(KeyFile? key_file = null) {
+        key_file.set_value(Geary.Config.GROUP, Geary.Config.CREDENTIALS_PROVIDER_KEY, 
this.credentials_provider.to_string());
+        key_file.set_value(Geary.Config.GROUP, Geary.Config.CREDENTIALS_METHOD_KEY, 
this.credentials_method.to_string());
+
         switch (this.service) {
             case Geary.Service.IMAP:
                 key_file.set_value(Geary.Config.GROUP, Geary.Config.IMAP_HOST, this.host);
                 key_file.set_integer(Geary.Config.GROUP, Geary.Config.IMAP_PORT, this.port);
                 key_file.set_boolean(Geary.Config.GROUP, Geary.Config.IMAP_SSL, this.use_ssl);
                 key_file.set_boolean(Geary.Config.GROUP, Geary.Config.IMAP_STARTTLS, this.use_starttls);
+                key_file.set_string(Geary.Config.GROUP, Geary.Config.IMAP_USERNAME_KEY, 
this.credentials.user);
                 break;
             case Geary.Service.SMTP:
                 key_file.set_value(Geary.Config.GROUP, Geary.Config.SMTP_HOST, this.host);
@@ -106,6 +119,7 @@ public class Geary.LocalServiceInformation : Geary.ServiceInformation {
                 key_file.set_boolean(Geary.Config.GROUP, Geary.Config.SMTP_STARTTLS, this.use_starttls);
                 key_file.set_boolean(Geary.Config.GROUP, Geary.Config.SMTP_USE_IMAP_CREDENTIALS, 
this.smtp_use_imap_credentials);
                 key_file.set_boolean(Geary.Config.GROUP, Geary.Config.SMTP_NOAUTH, this.smtp_noauth);
+                key_file.set_string(Geary.Config.GROUP, Geary.Config.SMTP_USERNAME_KEY, 
this.credentials.user);
                 break;
         }
     }
diff --git a/src/engine/api/geary-service-information.vala b/src/engine/api/geary-service-information.vala
index e3e5760..54a88ee 100644
--- a/src/engine/api/geary-service-information.vala
+++ b/src/engine/api/geary-service-information.vala
@@ -4,9 +4,71 @@
  * (version 2.1 or later).  See the COPYING file in this distribution.
  */
 
+/**
+ * A representation of the different sources Geary might get its credentials from.
+ */
+public enum Geary.CredentialsProvider {
+    LIBSECRET;
+
+    public string to_string() {
+        switch (this) {
+            case LIBSECRET:
+                return "libsecret";
+
+            default:
+                assert_not_reached();
+        }
+    }
+
+    public static CredentialsProvider from_string(string str) throws Error {
+        switch (str) {
+            case "libsecret":
+                return LIBSECRET;
+
+            default:
+                throw new KeyFileError.INVALID_VALUE(
+                    "Unknown credentials provider type: %s", str
+                );
+        }
+    }
+}
+/**
+ * A type representing different methods for authenticating. For now we only
+ * support password-based auth.
+ */
+public enum Geary.CredentialsMethod {
+    PASSWORD;
+
+    public string to_string() {
+        switch (this) {
+            case PASSWORD:
+                return "password";
+
+            default:
+                assert_not_reached();
+        }
+    }
+
+    public static CredentialsMethod from_string(string str) throws Error {
+        switch (str) {
+            case "password":
+                return PASSWORD;
+
+            default:
+                throw new KeyFileError.INVALID_VALUE(
+                    "Unknown credentials method type: %s", str
+                );
+        }
+    }
+}
+
+/**
+ * This class encloses all the information used when connecting with the server,
+ * how to authenticate with it and which credentials to use. Derived classes
+ * implement specific ways of doing that. For now, the only known implementation
+ * resides in Geary.LocalServiceInformation.
+ */
 public abstract class Geary.ServiceInformation : GLib.Object {
-    public const string METHOD_LIBSECRET = "libsecret";
-    public const string METHOD_GOA = "goa";
 
     public string host { get; set; default = ""; }
     public uint16 port { get; set; }
@@ -16,7 +78,10 @@ public abstract class Geary.ServiceInformation : GLib.Object {
     public Geary.Credentials credentials { get; set; default = new Geary.Credentials(null, null); }
     public Geary.Service service { get; set; }
     public Geary.CredentialsMediator? mediator { get; set; default = null; }
-    public string credentials_method { get; set; default = ""; }
+
+    public Geary.CredentialsProvider credentials_provider { get; set; default = 
CredentialsProvider.LIBSECRET; }
+
+    public Geary.CredentialsMethod credentials_method { get; set; default = CredentialsMethod.PASSWORD; }
 
     // Used with SMTP servers
     public bool smtp_noauth { get; set; default = false; }
@@ -37,6 +102,7 @@ public abstract class Geary.ServiceInformation : GLib.Object {
         this.credentials = from.credentials;
         this.service = from.service;
         this.mediator = from.mediator;
+        this.credentials_provider = from.credentials_provider;
         this.credentials_method = from.credentials_method;
         this.smtp_noauth = from.smtp_noauth;
         this.smtp_use_imap_credentials = from.smtp_use_imap_credentials;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]