[shotwell/wip/pluggable-auth: 14/16] wip: google: Persistency handling in authenticator



commit f2e949410559c61a6a06c8d2d9e777ffcb08cb7f
Author: Jens Georg <mail jensge org>
Date:   Thu Feb 9 22:38:42 2017 +0100

    wip: google: Persistency handling in authenticator
    
    Signed-off-by: Jens Georg <mail jensge org>

 .../shotwell/GoogleAuthenticator.vala              |   39 ++++++++++++++++---
 .../shotwell/ShotwellAuthenticatorFactory.vala     |    4 +-
 plugins/common/RESTSupport.vala                    |   10 -----
 plugins/shotwell-publishing/PicasaPublishing.vala  |   34 ++--------------
 plugins/shotwell-publishing/YouTubePublishing.vala |   40 +++----------------
 5 files changed, 46 insertions(+), 81 deletions(-)
---
diff --git a/plugins/authenticator/shotwell/GoogleAuthenticator.vala 
b/plugins/authenticator/shotwell/GoogleAuthenticator.vala
index c263200..f0fb754 100644
--- a/plugins/authenticator/shotwell/GoogleAuthenticator.vala
+++ b/plugins/authenticator/shotwell/GoogleAuthenticator.vala
@@ -42,14 +42,19 @@ namespace Publishing.Authenticator.Shotwell.Google {
         public override bool is_authenticated() {
             return (access_token != null);
         }
+
+        public void deauthenticate() {
+            access_token = null;
+            refresh_token = null;
+        }
     }
-    
+
     private class GetAccessTokensTransaction : Publishing.RESTSupport.Transaction {
         private const string ENDPOINT_URL = "https://accounts.google.com/o/oauth2/token";;
-        
+
         public GetAccessTokensTransaction(Session session, string auth_code) {
             base.with_endpoint_url(session, ENDPOINT_URL);
-            
+
             add_argument("code", auth_code);
             add_argument("client_id", OAUTH_CLIENT_ID);
             add_argument("client_secret", OAUTH_CLIENT_SECRET);
@@ -85,19 +90,24 @@ namespace Publishing.Authenticator.Shotwell.Google {
         private GLib.HashTable<string, Variant> params = null;
         private WebAuthenticationPane web_auth_pane = null;
         private Session session = null;
+        private string welcome_message = null;
 
-        public Google(string scope, Spit.Publishing.PluginHost host) {
+        public Google(string scope,
+                      string welcome_message,
+                      Spit.Publishing.PluginHost host) {
             this.host = host;
             this.params = new GLib.HashTable<string, Variant>(str_hash, str_equal);
             this.scope = scope;
             this.session = new Session();
+            this.welcome_message = welcome_message;
         }
 
         public void authenticate() {
             var refresh_token = host.get_config_string("refresh_token", null);
             if (refresh_token != null && refresh_token != "") {
-                session.refresh_token = refresh_token;
+                on_refresh_token_available(refresh_token);
                 do_exchange_refresh_token_for_access_token();
+                return;
             }
 
             // FIXME: Find a way for a proper logout
@@ -106,7 +116,7 @@ namespace Publishing.Authenticator.Shotwell.Google {
 
                 host.install_static_message_pane(_("You have already logged in and out of a Google service 
during this Shotwell session.\n\nTo continue publishing to Google services, quit and restart Shotwell, then 
try publishing again."));
             } else {
-                this.do_hosted_web_authentication();
+                this.do_show_service_welcome_pane();
             }
         }
 
@@ -119,6 +129,8 @@ namespace Publishing.Authenticator.Shotwell.Google {
         }
 
         public void logout() {
+            session.deauthenticate();
+            host.set_config_string("refresh_token", "");
         }
 
         private void do_hosted_web_authentication() {
@@ -305,6 +317,7 @@ namespace Publishing.Authenticator.Shotwell.Google {
             // by the time we get a username, the session should be authenticated, or else something
             // really tragic has happened
             assert(session.is_authenticated());
+            host.set_config_string("refresh_token", session.refresh_token);
 
             this.authenticated();
         }
@@ -362,6 +375,18 @@ namespace Publishing.Authenticator.Shotwell.Google {
             host.post_error(err);
         }
 
+        private void do_show_service_welcome_pane() {
+            debug("ACTION: showing service welcome pane.");
+
+            this.host.install_welcome_pane(this.welcome_message, on_service_welcome_login);
+        }
+
+        private void on_service_welcome_login() {
+            debug("EVENT: user clicked 'Login' in welcome pane.");
+
+            this.do_hosted_web_authentication();
+        }
+
+
     }
- 
 }
diff --git a/plugins/authenticator/shotwell/ShotwellAuthenticatorFactory.vala 
b/plugins/authenticator/shotwell/ShotwellAuthenticatorFactory.vala
index 68df5e5..17980e6 100644
--- a/plugins/authenticator/shotwell/ShotwellAuthenticatorFactory.vala
+++ b/plugins/authenticator/shotwell/ShotwellAuthenticatorFactory.vala
@@ -28,10 +28,10 @@ namespace Publishing.Authenticator {
                 case "facebook":
                     return new Shotwell.Facebook.Facebook(host);
                 case "picasa":
-                    return new Shotwell.Google.Google("https://picasaweb.google.com/data/";, host);
+                    return new Shotwell.Google.Google("https://picasaweb.google.com/data/";, _("You are not 
currently logged into Picasa Web Albums.\n\nClick Log in to log into Picasa Web Albums in your Web browser. 
You will have to authorize Shotwell Connect to link to your Picasa Web Albums account."), host);
 
                 case "youtube":
-                    return new Shotwell.Google.Google("https://gdata.youtube.com/";, host);
+                    return new Shotwell.Google.Google("https://gdata.youtube.com/";, _("You are not currently 
logged into YouTube.\n\nYou must have already signed up for a Google account and set it up for use with 
YouTube to continue. You can set up most accounts by using your browser to log into the YouTube site at least 
once."), host);
                 default:
                     return null;
             }
diff --git a/plugins/common/RESTSupport.vala b/plugins/common/RESTSupport.vala
index 0821ea9..f06473c 100644
--- a/plugins/common/RESTSupport.vala
+++ b/plugins/common/RESTSupport.vala
@@ -746,7 +746,6 @@ public string asciify_string(string s) {
 public abstract class GoogleSession : Session {
     public abstract string get_user_name();
     public abstract string get_access_token();
-    public abstract string get_refresh_token();
     public abstract void deauthenticate();
 }
 
@@ -779,11 +778,6 @@ public abstract class GooglePublisher : Object, Spit.Publishing.Publisher {
             return access_token;
         }
         
-        public override string get_refresh_token() {
-            assert(refresh_token != null);
-            return refresh_token;
-        }
-        
         public override void deauthenticate() {
             access_token = null;
             user_name = null;
@@ -832,10 +826,6 @@ public abstract class GooglePublisher : Object, Spit.Publishing.Publisher {
         return session;
     }
 
-    protected void start_oauth_flow(string? refresh_token = null) {
-        authenticator.authenticate();
-    }
-    
     protected abstract void on_login_flow_complete();
     
     protected abstract void do_logout();
diff --git a/plugins/shotwell-publishing/PicasaPublishing.vala 
b/plugins/shotwell-publishing/PicasaPublishing.vala
index cff9beb..f81417e 100644
--- a/plugins/shotwell-publishing/PicasaPublishing.vala
+++ b/plugins/shotwell-publishing/PicasaPublishing.vala
@@ -55,15 +55,12 @@ public class PicasaService : Object, Spit.Pluggable, Spit.Publishing.Service {
 
 namespace Publishing.Picasa {
 
-internal const string SERVICE_WELCOME_MESSAGE = 
-    _("You are not currently logged into Picasa Web Albums.\n\nClick Log in to log into Picasa Web Albums in 
your Web browser. You will have to authorize Shotwell Connect to link to your Picasa Web Albums account.");
 internal const string DEFAULT_ALBUM_NAME = _("Shotwell Connect");
 
 public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
     private bool running;
     private Spit.Publishing.ProgressCallback progress_reporter;
     private PublishingParameters publishing_parameters;
-    private string? refresh_token;
 
     public PicasaPublisher(Spit.Publishing.Service service,
         Spit.Publishing.PluginHost host) {
@@ -77,7 +74,6 @@ public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
             media_type |= p.get_media_type();
         publishing_parameters.set_media_type(media_type);
         
-        this.refresh_token = host.get_config_string("refresh_token", null);
         this.progress_reporter = null;
     }
 
@@ -132,19 +128,9 @@ public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
         get_host().set_config_string("last-album", parameters.get_target_album_name());
     }
 
-    private void on_service_welcome_login() {
-        debug("EVENT: user clicked 'Login' in welcome pane.");
-
-        if (!is_running())
-            return;
-        
-        start_oauth_flow(refresh_token);
-    }
-
     protected override void on_login_flow_complete() {
         debug("EVENT: OAuth login flow complete.");
 
-        get_host().set_config_string("refresh_token", get_session().get_refresh_token());
 
         publishing_parameters.set_user_name(get_session().get_user_name());
         
@@ -296,12 +282,6 @@ public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
         get_host().post_error(err);
     }
 
-    private void do_show_service_welcome_pane() {
-        debug("ACTION: showing service welcome pane.");
-
-        get_host().install_welcome_pane(SERVICE_WELCOME_MESSAGE, on_service_welcome_login);
-    }
-
     private void do_fetch_account_information() {
         debug("ACTION: fetching account and album information.");
 
@@ -425,13 +405,12 @@ public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
 
     protected override void do_logout() {
         debug("ACTION: logging out user.");
-        
         get_session().deauthenticate();
-        refresh_token = null;
-        get_host().unset_config_key("refresh_token");
-          
 
-        do_show_service_welcome_pane();
+        if (this.authenticator.can_logout()) {
+            this.authenticator.logout();
+            this.authenticator.authenticate();
+        }
     }
 
     public override bool is_running() {
@@ -446,10 +425,7 @@ public class PicasaPublisher : Publishing.RESTSupport.GooglePublisher {
 
         running = true;
 
-        if (refresh_token == null)
-            do_show_service_welcome_pane();
-        else
-            start_oauth_flow(refresh_token);
+        this.authenticator.authenticate();
     }
 
     public override void stop() {
diff --git a/plugins/shotwell-publishing/YouTubePublishing.vala 
b/plugins/shotwell-publishing/YouTubePublishing.vala
index ee7c158..1578601 100644
--- a/plugins/shotwell-publishing/YouTubePublishing.vala
+++ b/plugins/shotwell-publishing/YouTubePublishing.vala
@@ -54,8 +54,6 @@ public class YouTubeService : Object, Spit.Pluggable, Spit.Publishing.Service {
 
 namespace Publishing.YouTube {
 
-private const string SERVICE_WELCOME_MESSAGE =
-    _("You are not currently logged into YouTube.\n\nYou must have already signed up for a Google account 
and set it up for use with YouTube to continue. You can set up most accounts by using your browser to log 
into the YouTube site at least once.");
 private const string DEVELOPER_KEY =
     "AI39si5VEpzWK0z-pzo4fonEj9E4driCpEs9lK8y3HJsbbebIIRWqW3bIyGr42bjQv-N3siAfqVoM8XNmtbbp5x2gpbjiSAMTQ";
     
@@ -120,7 +118,6 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
     }
     
     private bool running;
-    private string? refresh_token;
     private PublishingParameters publishing_parameters;
     private Spit.Publishing.ProgressCallback? progress_reporter;
 
@@ -128,7 +125,6 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
         base(service, host, "https://gdata.youtube.com/";);
         
         this.running = false;
-        this.refresh_token = host.get_config_string("refresh_token", null);
         this.publishing_parameters = new PublishingParameters();
         this.progress_reporter = null;
     }
@@ -144,13 +140,10 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
             return;
 
         running = true;
-        
-        if (refresh_token == null)
-            do_show_service_welcome_pane();
-        else
-            start_oauth_flow(refresh_token);
+
+        this.authenticator.authenticate();
     }
-    
+
     public override void stop() {
         debug("YouTubePublisher: stopped.");
 
@@ -200,21 +193,10 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
 
         return result;
     }
-    
-    private void on_service_welcome_login() {
-        debug("EVENT: user clicked 'Login' in welcome pane.");
 
-        if (!is_running())
-            return;
-        
-        start_oauth_flow(refresh_token);
-    }
-    
     protected override void on_login_flow_complete() {
         debug("EVENT: OAuth login flow complete.");
         
-        get_host().set_config_string("refresh_token", get_session().get_refresh_token());
-
         publishing_parameters.set_user_name(get_session().get_user_name());
         
         do_fetch_account_information();
@@ -301,12 +283,6 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
         get_host().post_error(err);
     }
 
-    private void do_show_service_welcome_pane() {
-        debug("ACTION: showing service welcome pane.");
-
-        get_host().install_welcome_pane(SERVICE_WELCOME_MESSAGE, on_service_welcome_login);
-    }
-    
     private void do_fetch_account_information() {
         debug("ACTION: fetching channel information.");
 
@@ -407,13 +383,11 @@ public class YouTubePublisher : Publishing.RESTSupport.GooglePublisher {
 
     protected override void do_logout() {
         debug("ACTION: logging out user.");
-        
-        get_session().deauthenticate();
-        refresh_token = null;
-        get_host().unset_config_key("refresh_token");
-          
 
-        do_show_service_welcome_pane();
+        if (this.authenticator.can_logout()) {
+            this.authenticator.logout();
+            this.authenticator.authenticate();
+        }
     }
 
     protected override Spit.Publishing.Authenticator get_authenticator() {


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