/soc/2015/nakulgulati/main: 610d64c48e9b: hangouts: OAuth - auth...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Tue Jun 23 02:53:40 EDT 2015


Changeset: 610d64c48e9b07008ddad7ab5e7cce41140dcced
Author:	 Nakul Gulati
Date:	 2015-06-23 14:52 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/610d64c48e9b

Description:

hangouts: OAuth - auth_with_code, auth_with_refresh_token

diffstat:

 libpurple/protocols/hangouts/auth.c     |  81 ++++++++++++++++++++++++++++++--
 libpurple/protocols/hangouts/auth.h     |   8 ++-
 libpurple/protocols/hangouts/hangouts.c |   2 +-
 3 files changed, 81 insertions(+), 10 deletions(-)

diffs (164 lines):

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
@@ -45,13 +45,13 @@ typedef struct
 } _hangouts_oauth_token_request_data;
 
 static void
-hangouts_got_token_cb(PurpleHttpConnection *http_conn,
+hangouts_auth_with_authentication_code_cb(PurpleHttpConnection *http_conn,
 	PurpleHttpResponse *response, gpointer user_data){
 	purple_debug_misc("hangouts-prpl", "http req call back");
 
 	JsonParser *parser;
 	JsonObject *result;
-	gchar *error;
+	gchar *error, *access_token, *refresh_token;
 
 	gchar *raw_res = purple_http_response_get_data(response,NULL);
 
@@ -60,17 +60,26 @@ hangouts_got_token_cb(PurpleHttpConnecti
 	result = json_node_get_object(json_parser_get_root(parser));
 
 	if (json_object_has_member(result, "error"))
-		//throw error
+		/*TODO Throw error*/
 		error = json_object_get_string_member(result, "error");
 
-	purple_debug_misc("hangouts-prpl", "response data: %s \n error: %s",raw_res, error);
+	if (json_object_has_member(result, "access_token"))
+		/*TODO store token for api_access*/
+		access_token = json_object_get_string_member(result, "access_token");
+
+	if (json_object_has_member(result, "refresh_token"))
+			/*TODO store token for refresh request*/
+		refresh_token = json_object_get_string_member(result, "refresh_token");
+
+
+	purple_debug_misc("hangouts-prpl", "response data: %s \n error: %s, token: %s",raw_res, error, access_token);
 
 
 }
 
 
 void
-hangouts_request_auth(PurpleConnection *gc)
+hangouts_auth_with_authentication_code(PurpleConnection *gc)
 {
 	gchar *request_data;
 	gchar *auth_code;
@@ -87,7 +96,7 @@ hangouts_request_auth(PurpleConnection *
 			OAUTH2_CLIENT_ID,
 			OAUTH2_CLIENT_SECRET,
 			auth_code,
-			OAUTH2_GRANT_TYPE,
+			OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE,
 			OAUTH2_REDIRECT_URI);
 
 	PurpleHttpRequest *request;
@@ -98,9 +107,67 @@ hangouts_request_auth(PurpleConnection *
 	purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded");
 	purple_http_request_set_contents(request, request_data, -1);
 
-	purple_http_request(gc,request,hangouts_got_token_cb,NULL);
+	purple_http_request(gc,request,hangouts_auth_with_authentication_code_cb,NULL);
 
 }
 
+static void
+hangouts_auth_with_refresh_token_cb(PurpleHttpConnection *http_conn,
+	PurpleHttpResponse *response, gpointer user_data){
+	purple_debug_misc("hangouts-prpl", "hangouts_auth_with_refresh_token");
 
+	JsonParser *parser;
+	JsonObject *result;
+	gchar *error, *access_token;
 
+	gchar *raw_res = purple_http_response_get_data(response,NULL);
+
+	parser = json_parser_new();
+	json_parser_load_from_data(parser, raw_res, -1, NULL);
+	result = json_node_get_object(json_parser_get_root(parser));
+
+	if (json_object_has_member(result, "error"))
+		/*TODO Throw error*/
+		error = json_object_get_string_member(result, "error");
+
+	if (json_object_has_member(result, "access_token"))
+		/*TODO store token for refresh request*/
+		access_token = json_object_get_string_member(result, "access_token");
+
+	purple_debug_misc("hangouts-prpl", "response data: %s \n error: %s, token: %s",raw_res, error, access_token);
+
+
+}
+
+void
+hangouts_auth_with_refresh_token(PurpleConnection *gc)
+{
+	/*TODO get refresh from data*/
+
+	gchar *request_data;
+	gchar *refresh_token;
+	PurpleAccount *account = purple_connection_get_account(gc);
+
+	request_data = g_strdup_printf(
+			"client_id=%s&"
+			"client_secret=%s&"
+			"grant_type=%s&"
+			"refresh_token=%s",
+			OAUTH2_CLIENT_ID,
+			OAUTH2_CLIENT_SECRET,
+			OAUTH2_GRANT_TYPE_REFRESH_CODE,
+			refresh_token);
+
+	PurpleHttpRequest *request;
+	const char *method = "POST";
+
+	request = purple_http_request_new(OAUTH2_TOKEN_REQUEST_URL);
+	purple_http_request_set_method(request,method);
+	purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded");
+	purple_http_request_set_contents(request, request_data, -1);
+
+	purple_http_request(gc,request,hangouts_auth_with_refresh_token_cb,NULL);
+
+}
+
+
diff --git a/libpurple/protocols/hangouts/auth.h b/libpurple/protocols/hangouts/auth.h
--- a/libpurple/protocols/hangouts/auth.h
+++ b/libpurple/protocols/hangouts/auth.h
@@ -33,8 +33,9 @@ redirect_uri=" OAUTH2_REDIRECT_URI \
 "&client_id=" OAUTH2_CLIENT_ID "&scope=" OAUTH2_SCOPE
 
 #define OAUTH2_TOKEN_REQUEST_URL "https://accounts.google.com/o/oauth2/token"
-#define OAUTH2_GRANT_TYPE "authorization_code"
 
+#define OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE "authorization_code"
+#define OAUTH2_GRANT_TYPE_REFRESH_CODE "refresh_token"
 
 typedef struct _hangouts_oauth_data hangouts_oauth_data;
 
@@ -42,6 +43,9 @@ typedef struct _hangouts_oauth_token_req
 
 
 void hangouts_oauth_data_free(hangouts_oauth_data *data);
-void hangouts_request_auth(PurpleConnection *gc);
+void hangouts_auth_with_authentication_code(PurpleConnection *gc);
+static void hangouts_auth_with_authentication_code_cb(PurpleHttpConnection *http_conn,
+		PurpleHttpResponse *response, gpointer user_data);
+void hangouts_auth_with_refresh_token(PurpleConnection *gc);
 
 #endif /* _AUTH_H_ */
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
@@ -48,7 +48,7 @@ hangouts_login(PurpleAccount *acct)
 
 	purple_debug_misc("hangouts-prpl", "login; account username: %s;",username);
 
-	hangouts_request_auth(gc);
+	hangouts_auth_with_authentication_code(gc);
 
 }
 



More information about the Commits mailing list