/soc/2015/nakulgulati/main: f558a057d430: hangouts: api use refr...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Wed Aug 12 14:47:52 EDT 2015


Changeset: f558a057d43064df0ee67bb9a3a30ea4405bd0e5
Author:	 Nakul Gulati
Date:	 2015-08-13 02:39 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/f558a057d430

Description:

hangouts: api use refresh token as password

diffstat:

 libpurple/protocols/hangouts/api.c      |  52 ++++++++++++++++++--------------
 libpurple/protocols/hangouts/api.h      |   4 +-
 libpurple/protocols/hangouts/hangouts.c |  31 ++++++++++++------
 3 files changed, 52 insertions(+), 35 deletions(-)

diffs (210 lines):

diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -49,12 +49,6 @@ struct _HangoutsApiCookies
 
 };
 
-struct _HangoutsApiOAuthData
-{
-  gchar *refresh_token;
-  gchar *expires_in;
-};
-
 struct _HangoutsApiPrivate
 {
   PurpleConnection *gc;
@@ -63,7 +57,6 @@ struct _HangoutsApiPrivate
   HangoutsChannel *channel;
 
   HangoutsApiCookies *cookies;
-  HangoutsApiOAuthData *oauth_data;
 
   gchar *api_key;
   gchar *access_token;
@@ -72,8 +65,9 @@ struct _HangoutsApiPrivate
   gchar *header_date;
   gchar *header_version;
   gchar *header_id;
+  gchar *last_sync_timestamp;
   gchar *pvt_token;
-  gchar *last_sync_timestamp;
+  gchar *refresh_token;
 };
 
 struct _HangoutsApiHttpInfo
@@ -96,7 +90,6 @@ hangouts_api_dispose (GObject *obj)
   purple_http_cookie_jar_unref (priv->cookie_jar);
 
   g_free (priv->cookies);
-  g_free (priv->oauth_data);
   g_free (priv->access_token);
   g_free (priv->api_key);
   g_free (priv->client_id);
@@ -105,6 +98,8 @@ hangouts_api_dispose (GObject *obj)
   g_free (priv->header_id);
   g_free (priv->header_version);
   g_free (priv->last_sync_timestamp);
+  g_free (priv->pvt_token);
+  g_free (priv->refresh_token);
 }
 
 static void
@@ -135,13 +130,21 @@ hangouts_api_new (PurpleConnection *gc)
   priv = api->priv;
   priv->channel = hangouts_channel_new (gc);
   priv->cookies = g_new0(HangoutsApiCookies, 1);
-  priv->oauth_data = g_new0(HangoutsApiOAuthData, 1);
   priv->gc = gc;
 
   return api;
 }
 
 void
+hangouts_api_set_refresh_token (HangoutsApi *api, const gchar *refresh_token)
+{
+  HangoutsApiPrivate *priv;
+  priv = api->priv;
+
+  priv->refresh_token = g_strdup (refresh_token);
+}
+
+void
 hangouts_api_parse_header (HangoutsApiCookies *cookies, GList *headers)
 {
   GList *l;
@@ -289,12 +292,12 @@ hangouts_api_auth_with_code_cb (PurpleHt
   JsonObject *json_ob;
   HangoutsApi *api;
   HangoutsApiPrivate *priv;
-  HangoutsApiOAuthData *oauth_data;
+  PurpleAccount *acct;
   gchar *error, *access_token, *refresh_token;
 
   api = user_data;
   priv = api->priv;
-  oauth_data = api->priv->oauth_data;
+  acct = purple_connection_get_account (priv->gc);
   gchar *raw_res = purple_http_response_get_data (response, NULL);
   purple_debug_misc ("hangouts-prpl", "raw:\n%s", raw_res);
 
@@ -313,11 +316,14 @@ hangouts_api_auth_with_code_cb (PurpleHt
     }
 
   if (json_object_has_member (json_ob, "refresh_token"))
-    oauth_data->refresh_token = g_strdup (
-	json_object_get_string_member (json_ob, "refresh_token"));
-
-  //hangouts_api_get_private_token (api);
-  hangouts_channel_get_gsid (api->priv->channel);
+    {
+      priv->refresh_token = g_strdup (
+	  json_object_get_string_member (json_ob, "refresh_token"));
+      purple_debug_misc ("hangouts-prpl", "refresh_token: %s",
+			 priv->refresh_token);
+      purple_account_set_remember_password (acct, TRUE);
+      purple_account_set_password (acct, priv->refresh_token, NULL, NULL);
+    }
 }
 
 void
@@ -357,11 +363,11 @@ hangouts_api_oauth_refresh_token_cb (Pur
   JsonParser *parser;
   JsonObject *json_ob;
   HangoutsApi *api;
-  gchar *error, *access_token, *refresh_token;
+  gchar *raw_res;
+  gchar *error;
 
   api = user_data;
-  gchar *raw_res = purple_http_response_get_data (response, NULL);
-  purple_debug_misc ("hangouts-prpl", "raw:\n%s", raw_res);
+  raw_res = purple_http_response_get_data (response, NULL);
 
   parser = json_parser_new ();
   json_parser_load_from_data (parser, raw_res, -1, NULL);
@@ -389,9 +395,9 @@ hangouts_api_oauth_refresh_token (Hangou
 
   request_data = g_strdup_printf ("client_id=%s"
 				  "&refresh_token=%s"
-				  "&grant_type=%s"
-  HANGOUTS_API_OAUTH2_CLIENT_ID,
-				  priv->oauth_data->refresh_token,
+				  "&grant_type=%s",
+				  HANGOUTS_API_OAUTH2_CLIENT_ID,
+				  g_strcompress (priv->refresh_token),
 				  HANGOUTS_API_OAUTH2_GRANT_TYPE_REFRESH_CODE);
 
   request = purple_http_request_new (HANGOUTS_API_OAUTH2_TOKEN_URL);
diff --git a/libpurple/protocols/hangouts/api.h b/libpurple/protocols/hangouts/api.h
--- a/libpurple/protocols/hangouts/api.h
+++ b/libpurple/protocols/hangouts/api.h
@@ -74,7 +74,6 @@ typedef struct _HangoutsApiClass Hangout
 typedef struct _HangoutsApiPrivate HangoutsApiPrivate;
 typedef struct _HangoutsApiHttpInfo HangoutsApiHttpInfo;
 typedef struct _HangoutsApiCookies HangoutsApiCookies;
-typedef struct _HangoutsApiOAuthData HangoutsApiOAuthData;
 
 struct _HangoutsApi
 {
@@ -93,6 +92,9 @@ hangouts_api_get_type (void);
 HangoutsApi *
 hangouts_api_new (PurpleConnection *gc);
 
+void
+hangouts_api_set_refresh_token (HangoutsApi *api, const gchar *refresh_token);
+
 HangoutsApi *
 hangouts_api_new_from_oauth (PurpleConnection *gc, gchar *access_token);
 
diff --git a/libpurple/protocols/hangouts/hangouts.c b/libpurple/protocols/hangouts/hangouts.c
--- a/libpurple/protocols/hangouts/hangouts.c
+++ b/libpurple/protocols/hangouts/hangouts.c
@@ -50,19 +50,28 @@ static void
 hangouts_login (PurpleAccount *acct)
 {
   PurpleConnection *gc;
+  HangoutsApi *api;
+  const gchar *password;
+
   gc = purple_account_get_connection (acct);
-
-  HangoutsApi *api;
+  password = purple_connection_get_password (gc);
   api = hangouts_api_new (gc);
 
-  purple_notify_uri (acct, HANGOUTS_API_OAUTH2_AUTHORIZATION_CODE_URL);
-  purple_request_input (gc, _ ("Authorization Code"), NULL,
-			_ ("Please paste the authorization code here"),
-			NULL,
-			FALSE, NULL, NULL, _ ("OK"),
-			G_CALLBACK(hangouts_input_cb), _ ("Cancel"), NULL,
-			purple_request_cpar_from_connection (gc), api);
-
+  if (password != NULL)
+    {
+      hangouts_api_set_refresh_token (api, password);
+      hangouts_api_oauth_refresh_token (api);
+    }
+  else
+    {
+      purple_notify_uri (acct, HANGOUTS_API_OAUTH2_AUTHORIZATION_CODE_URL);
+      purple_request_input (gc, _ ("Authorization Code"), NULL,
+			    _ ("Please paste the authorization code here"),
+			    NULL,
+			    FALSE, NULL, NULL, _ ("OK"),
+			    G_CALLBACK(hangouts_input_cb), _ ("Cancel"), NULL,
+			    purple_request_cpar_from_connection (gc), api);
+    }
 }
 
 static void
@@ -100,7 +109,7 @@ hangouts_protocol_init (PurpleProtocol *
 {
   protocol->id = "prpl-hangouts";
   protocol->name = "Hangouts";
-  purple_debug_misc ("hangouts-prpl", "init");
+  protocol->options = OPT_PROTO_NO_PASSWORD;
 }
 
 static void



More information about the Commits mailing list