/soc/2015/nakulgulati/main: d3f75f0ae65b: hangouts: auth refacto...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Thu Jul 30 15:47:20 EDT 2015


Changeset: d3f75f0ae65b5f62e23a1206b338261139b60b45
Author:	 Nakul Gulati
Date:	 2015-07-29 22:31 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/d3f75f0ae65b

Description:

hangouts: auth refactor to api, fixed includes and api class

diffstat:

 libpurple/protocols/hangouts/Makefile.am    |    6 +-
 libpurple/protocols/hangouts/Makefile.mingw |    2 +
 libpurple/protocols/hangouts/api.c          |  120 +++++++++-
 libpurple/protocols/hangouts/api.h          |    6 +
 libpurple/protocols/hangouts/auth.c         |  305 +++++++++++++++++++++------
 libpurple/protocols/hangouts/auth.h         |   23 +-
 libpurple/protocols/hangouts/hangouts.c     |    6 +-
 libpurple/protocols/hangouts/util.c         |   87 +++++--
 libpurple/protocols/hangouts/util.h         |   16 +-
 9 files changed, 436 insertions(+), 135 deletions(-)

diffs (truncated from 789 to 300 lines):

diff --git a/libpurple/protocols/hangouts/Makefile.am b/libpurple/protocols/hangouts/Makefile.am
--- a/libpurple/protocols/hangouts/Makefile.am
+++ b/libpurple/protocols/hangouts/Makefile.am
@@ -4,10 +4,14 @@
 pkgdir = @PURPLE_PLUGINDIR@
 
 HANGOUTSSOURCES = \
+	api.h \
+	api.c \
 	auth.h \
 	auth.c \
 	hangouts.h \
-	hangouts.c
+	hangouts.c \
+	util.h \
+	util.c
 
 AM_CFLAGS = $(st)
 
diff --git a/libpurple/protocols/hangouts/Makefile.mingw b/libpurple/protocols/hangouts/Makefile.mingw
--- a/libpurple/protocols/hangouts/Makefile.mingw
+++ b/libpurple/protocols/hangouts/Makefile.mingw
@@ -38,8 +38,10 @@ LIB_PATHS +=		-L$(GTK_TOP)/lib \
 ##  SOURCES, OBJECTS
 ##
 C_SRC =	\
+			api.c
 			auth.c
 			hangouts.c
+			util.c
 
 OBJECTS = $(C_SRC:%.c=%.o)
 
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
@@ -19,12 +19,15 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
 
+#include <glib.h>
+
+#include "http.h"
+
 #include "api.h"
-#include "auth.h"
-
+#include "util.h"
 //TODO api call backs
 
-G_DEFINE_TYPE (HangoutsApi, hangouts_api, G_TYPE_OBJECT);
+G_DEFINE_TYPE(HangoutsApi, hangouts_api, G_TYPE_OBJECT);
 
 enum _HangoutsConversationOffTheRecordStatus
 {
@@ -34,8 +37,8 @@ enum _HangoutsConversationOffTheRecordSt
 struct _HangoutsApiPrivate
 {
   PurpleConnection *gc;
-  //HangoutsAuthCookies *cookies
   PurpleHttpCookieJar *cookie_jar;
+  GHashTable *cookies_tab;
 
   gchar *galx;
   gchar *api_key;
@@ -58,14 +61,13 @@ hangouts_api_dispose (GObject *obj)
 {
   HangoutsApiPrivate *priv = HANGOUTS_API(obj)->priv;
 
-  if (G_LIKELY (priv->gc != NULL))
+  if (G_LIKELY(priv->gc != NULL))
     {
       purple_http_conn_cancel_all (priv->gc);
     }
 
-  //hangouts_auth_cookies_free (priv->cookies);
-
-  purple_http_cookie_free (priv->cookie_jar);
+  g_hash_table_destroy (priv->cookies_tab);
+  purple_http_cookie_jar_unref(priv->cookie_jar);
 
   g_free (priv->galx);
   g_free (priv->access_token);
@@ -81,8 +83,7 @@ hangouts_api_dispose (GObject *obj)
 static void
 hangouts_api_class_init (HangoutsApiClass *klass)
 {
-  purple_debug_misc ("hangouts-prpl", "new class init");
-  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
   g_type_class_add_private (klass, sizeof(HangoutsApiPrivate));
   gobject_class->dispose = hangouts_api_dispose;
 }
@@ -90,18 +91,16 @@ hangouts_api_class_init (HangoutsApiClas
 static void
 hangouts_api_init (HangoutsApi *api)
 {
-  purple_debug_misc ("hangouts-prpl", "api init");
   HangoutsApiPrivate *priv;
 
-  priv = G_TYPE_INSTANCE_GET_PRIVATE (api, HANGOUTS_TYPE_API,
-				      HangoutsApiPrivate);
+  priv = G_TYPE_INSTANCE_GET_PRIVATE(api, HANGOUTS_TYPE_API,
+				     HangoutsApiPrivate);
   api->priv = priv;
 }
 
 HangoutsApi *
 hangouts_api_new (PurpleConnection *gc)
 {
-  purple_debug_misc ("hangouts-prpl", "new api obj");
   HangoutsApi *api;
   HangoutsApiPrivate *priv;
 
@@ -109,10 +108,100 @@ hangouts_api_new (PurpleConnection *gc)
   priv = api->priv;
 
   priv->gc = gc;
+  priv->cookies_tab = g_hash_table_new (g_str_hash, g_str_equal);
 
   return api;
 }
 
+static void
+hangouts_api_fetch_cookies_cb (PurpleHttpConnection *http_conn,
+			       PurpleHttpResponse *response, gpointer user_data)
+{
+  purple_debug_misc ("hangouts-prpl", "second call back");
+  HangoutsApi *api;
+
+  api = user_data;
+
+  PurpleHttpCookieJar *cj = purple_http_conn_get_cookie_jar (http_conn);
+
+  GList *headers = purple_http_response_get_headers_by_name (response,
+							     "Set-Cookie");
+
+  hangouts_util_parse_header (api->priv->cookies_tab, headers);
+}
+
+void
+hangouts_api_fetch_login_cookies (HangoutsApi *api)
+{
+  PurpleConnection *gc;
+  PurpleAccount *acct;
+  PurpleHttpRequest *request;
+  HangoutsApiPrivate *priv;
+  gchar *username, *password, *url, *request_body;
+
+  priv = api->priv;
+  gc = priv->gc;
+  acct = purple_connection_get_account (gc);
+  username = purple_account_get_username (acct);
+  password = purple_connection_get_password (gc);
+
+  url = "https://accounts.google.com/ServiceLoginAuth";
+  request_body = g_strdup_printf (
+      "Email=%s"
+      "&Passwd=%s"
+      "&PersistentCookie=yes"
+      "&GALX=%s"
+      "&continue=https:\/\/talkgadget.google.com\/talkgadget\/gauth?verify=true"
+      "&skipvpage=true",
+      username, password, priv->galx);
+  request = purple_http_request_new (url);
+
+  purple_http_request_set_method (request, "POST");
+  purple_http_request_set_max_len (request, -1);
+  purple_http_request_header_add (request, "Content-Type",
+				  "application/x-www-form-urlencoded");
+  purple_http_request_set_contents (request, request_body, -1);
+  purple_http_request_set_cookie_jar (request, priv->cookie_jar);
+  purple_http_request_set_max_redirects (request, 0);
+  purple_http_request (gc, request, hangouts_api_fetch_cookies_cb, api);
+}
+
+static void
+hangouts_api_get_galx_cookie_cb (PurpleHttpConnection *http_conn,
+				 PurpleHttpResponse *response,
+				 gpointer user_data)
+{
+  HangoutsApi *api = user_data;
+  HangoutsApiPrivate *priv = api->priv;
+
+  priv->cookie_jar = purple_http_conn_get_cookie_jar (http_conn);
+  priv->galx = purple_http_cookie_jar_get (priv->cookie_jar, "GALX");
+
+  purple_debug_misc ("hangouts-prpl", "galx: %s", priv->galx);
+
+  hangouts_api_fetch_login_cookies (api);
+}
+
+void
+hangouts_api_get_galx_cookie (HangoutsApi *api)
+{
+  gchar *url;
+  PurpleHttpRequest *request;
+  PurpleHttpCookieJar *cookieJar;
+  HangoutsApiPrivate *priv;
+  PurpleConnection *gc;
+
+  priv = api->priv;
+  gc = priv->gc;
+  url = "https://accounts.google.com/ServiceLogin?service=ah&passive=true";
+  request = purple_http_request_new (url);
+  cookieJar = purple_http_cookie_jar_new ();
+
+  purple_http_request_set_method (request, "GET");
+  purple_http_request_set_cookie_jar (request, cookieJar);
+  purple_http_request (gc, request, hangouts_api_get_galx_cookie_cb, api);
+}
+
 gchar *
 hangouts_api_get_request_header (HangoutsApi *api)
 {
@@ -152,9 +241,6 @@ hangouts_api_request (HangoutsApi *api, 
     }
 
   purple_http_request_header_add (request, "content-type", content_type);
-  /*purple_http_request_header_add (
-   request, "Authorization",
-   hangouts_auth_get_authorization_header (priv->access_token));*/
   purple_http_request_set_method (request, "POST");
   purple_http_request_set_contents (request, request_body, -1);
   purple_http_request (priv->gc, request, info->callback, api);
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
@@ -66,6 +66,12 @@ hangouts_api_get_type (void);
 HangoutsApi *
 hangouts_api_new (PurpleConnection *gc);
 
+void
+hangouts_api_get_galx_cookie (HangoutsApi *api);
+
+void
+hangouts_api_fetch_login_cookies (HangoutsApi *api);
+
 gchar *
 hangouts_api_get_request_header (HangoutsApi *api);
 
diff --git a/libpurple/protocols/hangouts/auth.c b/libpurple/protocols/hangouts/auth.c
--- a/libpurple/protocols/hangouts/auth.c
+++ b/libpurple/protocols/hangouts/auth.c
@@ -19,106 +19,261 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
 
-#include "http.h"
-#include "http.c"
-#include "auth.h"
-#include "api.c"
-
 #include <json-glib/json-glib.h>
 
-struct _HangoutsAuthCookies
+#include "http.h"
+#include "auth.h"
+
+typedef struct
 {
-  GHashTable *cookies_tab;
-};
+  PurpleConnection *gc;
+  gpointer user_data;
+  gchar *token;
+  gchar *token_secret;
+  gchar *sign_method;
+  gchar *sign_url;
+} _hangouts_oauth_data;
 
-static void
-hangouts_auth_fetch_cookies_cb (PurpleHttpConnection *http_conn,
-				PurpleHttpResponse *response,
-				gpointer user_data)
+typedef struct
 {
-  purple_debug_misc ("hangouts-prpl", "second call back");
+  PurpleConnection *gc;
+  gpointer user_data;
+  gchar *client_id;
+  gchar *client_secret;
+  gchar *authorization_code;
+  gchar *redirect_uri;
+} _hangouts_oauth_token_request_data;
 
-  PurpleHttpCookieJar *cj = purple_http_conn_get_cookie_jar (http_conn);
+typedef struct
+{
+  gchar *APISID;
+  gchar *HSID;
+  gchar *NID;
+  gchar *SAPISID;
+  gchar *SID;
+  gchar *SSID;
+} _hangouts_oauth_cookies;
 
-  GList *headers = purple_http_response_get_headers_by_name (response,
-							     "Set-Cookie");
-  /*TODO: parse headers and store cookies*/



More information about the Commits mailing list