[gnome-online-accounts] oauth2: Make scope optional



commit 31765a8fb60903a25ec30aa00c9ba3d783fd2703
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Oct 3 14:45:29 2013 +0200

    oauth2: Make scope optional
    
    Some providers don't use the scope, so no need for it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704564

 src/goabackend/goaoauth2provider.c |   29 +++++++++++++++++++++++------
 src/goabackend/goaoauth2provider.h |    2 +-
 2 files changed, 24 insertions(+), 7 deletions(-)
---
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index b5d8121..3ee1d39 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -252,7 +252,7 @@ goa_oauth2_provider_build_authorization_uri_default (GoaOAuth2Provider  *provide
  * @authorization_uri: An authorization URI.
  * @escaped_redirect_uri: An escaped redirect URI
  * @escaped_client_id: An escaped client id
- * @escaped_scope: The escaped scope.
+ * @escaped_scope: (allow-none): The escaped scope or %NULL
  *
  * Builds the URI that can be opened in a web browser (or embedded web
  * browser widget) to start authenticating an user.
@@ -281,7 +281,6 @@ goa_oauth2_provider_build_authorization_uri (GoaOAuth2Provider  *provider,
   g_return_val_if_fail (authorization_uri != NULL, NULL);
   g_return_val_if_fail (escaped_redirect_uri != NULL, NULL);
   g_return_val_if_fail (escaped_client_id != NULL, NULL);
-  g_return_val_if_fail (escaped_scope != NULL, NULL);
   return GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->build_authorization_uri (provider,
                                                                                     authorization_uri,
                                                                                     escaped_redirect_uri,
@@ -371,6 +370,14 @@ goa_oauth2_provider_get_redirect_uri (GoaOAuth2Provider *provider)
   return GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->get_redirect_uri (provider);
 }
 
+/* ---------------------------------------------------------------------------------------------------- */
+
+static const gchar *
+goa_oauth2_provider_get_scope_default (GoaOAuth2Provider *provider)
+{
+  return NULL;
+}
+
 /**
  * goa_oauth2_provider_get_scope:
  * @provider: A #GoaOAuth2Provider.
@@ -379,8 +386,8 @@ goa_oauth2_provider_get_redirect_uri (GoaOAuth2Provider *provider)
  * url="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-2.1.1";>scope</ulink>
  * used when requesting authorization.
  *
- * This is a pure virtual method - a subclass must provide an
- * implementation.
+ * This is a virtual method where the default implementation returns
+ * %NULL.
  *
  * Returns: (transfer none): A string owned by @provider - do not free.
  */
@@ -388,9 +395,13 @@ const gchar *
 goa_oauth2_provider_get_scope (GoaOAuth2Provider *provider)
 {
   g_return_val_if_fail (GOA_IS_OAUTH2_PROVIDER (provider), NULL);
-  return GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->get_scope (provider);
+  if (GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->get_scope)
+    return GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->get_scope (provider);
+  return NULL;
 }
 
+/* ---------------------------------------------------------------------------------------------------- */
+
 /**
  * goa_oauth2_provider_get_client_id:
  * @provider: A #GoaOAuth2Provider.
@@ -1003,6 +1014,7 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
   gboolean ret;
   gchar *url;
   GtkWidget *grid;
+  const gchar *scope;
   gchar *escaped_redirect_uri;
   gchar *escaped_client_id;
   gchar *escaped_scope;
@@ -1034,7 +1046,11 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
   /* TODO: use oauth2_proxy_build_login_url_full() */
   escaped_redirect_uri = g_uri_escape_string (goa_oauth2_provider_get_redirect_uri (provider), NULL, TRUE);
   escaped_client_id = g_uri_escape_string (goa_oauth2_provider_get_client_id (provider), NULL, TRUE);
-  escaped_scope = g_uri_escape_string (goa_oauth2_provider_get_scope (provider), NULL, TRUE);
+  scope = goa_oauth2_provider_get_scope (provider);
+  if (scope != NULL)
+    escaped_scope = g_uri_escape_string (goa_oauth2_provider_get_scope (provider), NULL, TRUE);
+  else
+    escaped_scope = NULL;
   url = goa_oauth2_provider_build_authorization_uri (provider,
                                                      goa_oauth2_provider_get_authorization_uri (provider),
                                                      escaped_redirect_uri,
@@ -1798,6 +1814,7 @@ goa_oauth2_provider_class_init (GoaOAuth2ProviderClass *klass)
 
   klass->build_authorization_uri  = goa_oauth2_provider_build_authorization_uri_default;
   klass->get_token_uri            = goa_oauth2_provider_get_token_uri_default;
+  klass->get_scope                = goa_oauth2_provider_get_scope_default;
   klass->get_use_external_browser = goa_oauth2_provider_get_use_external_browser_default;
   klass->get_use_mobile_browser   = goa_oauth2_provider_get_use_mobile_browser_default;
   klass->is_password_node         = goa_oauth2_provider_is_password_node_default;
diff --git a/src/goabackend/goaoauth2provider.h b/src/goabackend/goaoauth2provider.h
index 21036ca..26bfaf6 100644
--- a/src/goabackend/goaoauth2provider.h
+++ b/src/goabackend/goaoauth2provider.h
@@ -85,7 +85,6 @@ struct _GoaOAuth2ProviderClass
   /* pure virtual */
   const gchar *(*get_authorization_uri)        (GoaOAuth2Provider            *provider);
   const gchar *(*get_redirect_uri)             (GoaOAuth2Provider            *provider);
-  const gchar *(*get_scope)                    (GoaOAuth2Provider            *provider);
   const gchar *(*get_client_id)                (GoaOAuth2Provider            *provider);
   const gchar *(*get_client_secret)            (GoaOAuth2Provider            *provider);
   const gchar *(*get_authentication_cookie)    (GoaOAuth2Provider            *provider);
@@ -102,6 +101,7 @@ struct _GoaOAuth2ProviderClass
                                                 const gchar                  *escaped_client_id,
                                                 const gchar                  *escaped_scope);
   const gchar *(*get_token_uri)                (GoaOAuth2Provider            *provider);
+  const gchar *(*get_scope)                    (GoaOAuth2Provider            *provider);
   gboolean     (*get_use_external_browser)     (GoaOAuth2Provider            *provider);
   gboolean     (*get_use_mobile_browser)       (GoaOAuth2Provider            *provider);
   void         (*add_account_key_values)       (GoaOAuth2Provider            *provider,


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