[librest/gwagner/oauth2] oauth2: queue refresh access token func and more tests




commit e1132b2fd531f5f18336d5fd5e981b4fb9513a90
Author: Günther Wagner <info gunibert de>
Date:   Wed Nov 17 23:12:57 2021 +0100

    oauth2: queue refresh access token func and more tests

 rest/rest-oauth2-proxy.c | 84 ++++++++++++++++++++++++++++++++++++++++++++----
 rest/rest-oauth2-proxy.h |  1 +
 tests/oauth2.c           | 29 ++++++++++++++++-
 3 files changed, 106 insertions(+), 8 deletions(-)
---
diff --git a/rest/rest-oauth2-proxy.c b/rest/rest-oauth2-proxy.c
index 4764f43..dcfd719 100644
--- a/rest/rest-oauth2-proxy.c
+++ b/rest/rest-oauth2-proxy.c
@@ -392,10 +392,11 @@ rest_oauth2_proxy_fetch_access_token_cb (SoupMessage *msg,
                                          gpointer     user_data)
 {
   g_autoptr(GTask) task = user_data;
-  RestOAuth2Proxy *self = g_task_get_source_object (task);
+  RestOAuth2Proxy *self;
 
-  g_return_if_fail (G_IS_TASK (task));
-  g_return_if_fail (REST_IS_OAUTH2_PROXY (self));
+  g_assert (G_IS_TASK (task));
+
+  self = g_task_get_source_object (task);
 
   if (error)
     {
@@ -466,6 +467,70 @@ rest_oauth2_proxy_fetch_access_token_finish (RestOAuth2Proxy  *self,
   return g_task_propagate_boolean (G_TASK (result), error);
 }
 
+void
+rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self)
+{
+  RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self);
+  g_autoptr(SoupMessage) msg = NULL;
+  g_autoptr(GHashTable) params = NULL;
+  g_autoptr(GTask) task = NULL;
+  g_autoptr(GError) error = NULL;
+  GBytes *payload;
+
+  task = g_task_new (self, NULL, NULL, NULL);
+
+  g_return_if_fail (REST_IS_OAUTH2_PROXY (self));
+
+  if (priv->refresh_token == NULL)
+    {
+      g_task_return_new_error (task,
+                               REST_OAUTH2_ERROR,
+                               REST_OAUTH2_ERROR_NO_REFRESH_TOKEN,
+                               "No refresh token available");
+      return;
+    }
+
+  params = g_hash_table_new (g_str_hash, g_str_equal);
+
+  g_hash_table_insert (params, "client_id", priv->client_id);
+  g_hash_table_insert (params, "refresh_token", priv->refresh_token);
+  g_hash_table_insert (params, "redirect_uri", priv->redirect_uri);
+  g_hash_table_insert (params, "grant_type", "refresh_token");
+
+#if WITH_SOUP_2
+  msg = soup_form_request_new_from_hash (SOUP_METHOD_POST, priv->tokenurl, params);
+#else
+  msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash 
(params));
+#endif
+  payload = _rest_proxy_send_message (REST_PROXY (self), msg, NULL, &error);
+  if (error)
+    g_task_return_error (task, error);
+
+  REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task));
+}
+
+static void
+rest_oauth2_proxy_refresh_access_token_cb (SoupMessage *msg,
+                                           GBytes      *payload,
+                                           GError      *error,
+                                           gpointer     user_data)
+{
+  g_autoptr(GTask) task = user_data;
+  RestOAuth2Proxy *self;
+
+  g_assert (G_IS_TASK (task));
+
+  self = g_task_get_source_object (task);
+
+  if (error)
+    {
+      g_task_return_error (task, error);
+      return;
+    }
+
+  REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task));
+}
+
 void
 rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy     *self,
                                               GCancellable        *cancellable,
@@ -476,7 +541,6 @@ rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy     *self,
   g_autoptr(SoupMessage) msg = NULL;
   g_autoptr(GHashTable) params = NULL;
   g_autoptr(GTask) task = NULL;
-  GBytes *payload;
 
   task = g_task_new (self, cancellable, callback, user_data);
 
@@ -503,9 +567,15 @@ rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy     *self,
 #else
   msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash 
(params));
 #endif
-  payload = _rest_proxy_send_message (REST_PROXY (self), msg, NULL, NULL);
-
-  REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task));
+  _rest_proxy_queue_message (REST_PROXY (self),
+#if WITH_SOUP_2
+                             g_steal_pointer (&msg),
+#else
+                             msg,
+#endif
+                             cancellable,
+                             rest_oauth2_proxy_refresh_access_token_cb,
+                             g_steal_pointer (&task));
 }
 
 /**
diff --git a/rest/rest-oauth2-proxy.h b/rest/rest-oauth2-proxy.h
index f55f1eb..b3a0486 100644
--- a/rest/rest-oauth2-proxy.h
+++ b/rest/rest-oauth2-proxy.h
@@ -66,6 +66,7 @@ void             rest_oauth2_proxy_fetch_access_token_async    (RestOAuth2Proxy
 gboolean         rest_oauth2_proxy_fetch_access_token_finish   (RestOAuth2Proxy      *self,
                                                                 GAsyncResult         *result,
                                                                 GError              **error);
+void             rest_oauth2_proxy_refresh_access_token        (RestOAuth2Proxy      *self);
 void             rest_oauth2_proxy_refresh_access_token_async  (RestOAuth2Proxy      *self,
                                                                 GCancellable         *cancellable,
                                                                 GAsyncReadyCallback   callback,
diff --git a/tests/oauth2.c b/tests/oauth2.c
index a859e23..5d6d827 100644
--- a/tests/oauth2.c
+++ b/tests/oauth2.c
@@ -164,7 +164,6 @@ static void
 test_refresh_access_token (gconstpointer url)
 {
   GMainContext *async_context = g_main_context_ref_thread_default ();
-  g_autoptr(GError) error = NULL;
 
   g_autofree gchar *tokenurl = g_strdup_printf ("%stoken", (gchar *)url);
   g_autofree gchar *baseurl = g_strdup_printf ("%sapi", (gchar *)url);
@@ -185,12 +184,39 @@ test_refresh_access_token (gconstpointer url)
   rest_oauth2_proxy_set_refresh_token (REST_OAUTH2_PROXY (proxy), "refresh_token");
   rest_oauth2_proxy_refresh_access_token_async (REST_OAUTH2_PROXY (proxy), NULL, 
test_refresh_access_token_finished, &finished);
 
+  finished = FALSE;
+
   while (!finished) {
     g_main_context_iteration (async_context, TRUE);
   }
 
   g_assert_cmpstr ("2YotnFZFEjr1zCsicMWpAA", ==, rest_oauth2_proxy_get_access_token (REST_OAUTH2_PROXY 
(proxy)));
   g_assert_cmpstr ("tGzv3JOkF0XG5Qx2TlKWIA", ==, rest_oauth2_proxy_get_refresh_token (REST_OAUTH2_PROXY 
(proxy)));
+
+  g_main_context_unref (async_context);
+}
+
+static void
+test_access_token_expired (gconstpointer url)
+{
+  g_autofree gchar *tokenurl = g_strdup_printf ("%stoken", (gchar *)url);
+  g_autofree gchar *baseurl = g_strdup_printf ("%sapi", (gchar *)url);
+  g_autoptr(GError) error = NULL;
+
+  g_autoptr(RestProxy) proxy = REST_PROXY (rest_oauth2_proxy_new ("http://www.example.com/auth";,
+                                                                  tokenurl,
+                                                                  "http://www.example.com";,
+                                                                  "client-id",
+                                                                  "client-secret",
+                                                                  baseurl));
+  GDateTime *now = g_date_time_new_now_local ();
+  rest_oauth2_proxy_set_expiration_date (REST_OAUTH2_PROXY (proxy), now);
+
+  g_autoptr(RestProxyCall) call = rest_proxy_new_call (proxy);
+  rest_proxy_call_set_method (call, "GET");
+  rest_proxy_call_set_function (call, "/expired");
+  rest_proxy_call_sync (call, &error);
+  g_assert_error (error, REST_OAUTH2_ERROR, REST_OAUTH2_ERROR_ACCESS_TOKEN_EXPIRED);
 }
 
 gint
@@ -210,6 +236,7 @@ main (gint   argc,
   g_test_add_data_func ("/oauth2/authorization_url", url, test_authorization_url);
   g_test_add_data_func ("/oauth2/fetch_access_token", url, test_fetch_access_token);
   g_test_add_data_func ("/oauth2/refresh_access_token", url, test_refresh_access_token);
+  g_test_add_data_func ("/oauth2/access_token_expired", url, test_access_token_expired);
 
   return g_test_run ();
 }


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