libpurple/protocols/gg/oauth/oauth-purple.c

changeset 41878
64e455cfb6b4
parent 41700
9f6a2c90800e
child 41921
24e5305ebcb6
equal deleted inserted replaced
41877:11e7400101d2 41878:64e455cfb6b4
39 39
40 #define GGP_OAUTH_RESPONSE_MAX 10240 40 #define GGP_OAUTH_RESPONSE_MAX 10240
41 41
42 typedef struct 42 typedef struct
43 { 43 {
44 SoupMessage *msg;
44 PurpleConnection *gc; 45 PurpleConnection *gc;
45 ggp_oauth_request_cb callback; 46 ggp_oauth_request_cb callback;
46 gpointer user_data; 47 gpointer user_data;
47 gchar *token; 48 gchar *token;
48 gchar *token_secret; 49 gchar *token_secret;
50 gchar *sign_method, *sign_url; 51 gchar *sign_method, *sign_url;
51 } ggp_oauth_data; 52 } ggp_oauth_data;
52 53
53 static void ggp_oauth_data_free(ggp_oauth_data *data) 54 static void ggp_oauth_data_free(ggp_oauth_data *data)
54 { 55 {
56 g_object_unref(data->msg);
55 g_free(data->token); 57 g_free(data->token);
56 g_free(data->token_secret); 58 g_free(data->token_secret);
57 g_free(data->sign_method); 59 g_free(data->sign_method);
58 g_free(data->sign_url); 60 g_free(data->sign_url);
59 g_free(data); 61 g_free(data);
60 } 62 }
61 63
62 static void 64 static void
63 ggp_oauth_access_token_got(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg, 65 ggp_oauth_access_token_got(GObject *source, GAsyncResult *result,
64 gpointer user_data) 66 gpointer user_data)
65 { 67 {
66 ggp_oauth_data *data = user_data; 68 ggp_oauth_data *data = user_data;
69 GBytes *response_body = NULL;
70 const char *buffer = NULL;
71 gsize size = 0;
67 gchar *token = NULL, *token_secret = NULL; 72 gchar *token = NULL, *token_secret = NULL;
68 PurpleXmlNode *xml; 73 PurpleXmlNode *xml;
69 gboolean succ = TRUE; 74 gboolean succ = TRUE;
70 75 GError *error = NULL;
71 xml = purple_xmlnode_from_str(msg->response_body->data, 76
72 msg->response_body->length); 77 response_body = soup_session_send_and_read_finish(SOUP_SESSION(source),
78 result, &error);
79 if(response_body == NULL) {
80 purple_debug_error("gg", "ggp_oauth_access_token_got: failed: %s",
81 error->message);
82 ggp_oauth_data_free(data);
83 g_error_free(error);
84 return;
85 }
86
87 buffer = g_bytes_get_data(response_body, &size);
88 xml = purple_xmlnode_from_str(buffer, size);
89 g_bytes_unref(response_body);
90
73 if (xml == NULL) { 91 if (xml == NULL) {
74 purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml"); 92 purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml");
75 ggp_oauth_data_free(data); 93 ggp_oauth_data_free(data);
76 return; 94 return;
77 } 95 }
112 g_free(token_secret); 130 g_free(token_secret);
113 ggp_oauth_data_free(data); 131 ggp_oauth_data_free(data);
114 } 132 }
115 133
116 static void 134 static void
117 ggp_oauth_authorization_done(SoupSession *session, SoupMessage *msg, 135 ggp_oauth_authorization_done(GObject *source, GAsyncResult *result,
118 gpointer user_data) 136 gpointer user_data)
119 { 137 {
120 ggp_oauth_data *data = user_data; 138 ggp_oauth_data *data = user_data;
121 PurpleAccount *account; 139 PurpleAccount *account;
122 SoupStatus status_code; 140 SoupStatus status_code;
123 char *auth; 141 char *auth;
142 SoupMessage *msg = NULL;
124 const char *method = "POST"; 143 const char *method = "POST";
125 const char *url = "http://api.gadu-gadu.pl/access_token"; 144 const char *url = "http://api.gadu-gadu.pl/access_token";
126 145
127 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc); 146 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
128 147
129 account = purple_connection_get_account(data->gc); 148 account = purple_connection_get_account(data->gc);
130 149
131 status_code = soup_message_get_status(msg); 150 status_code = soup_message_get_status(data->msg);
132 if (status_code != 302) { 151 if (status_code != 302) {
133 purple_debug_error("gg", 152 purple_debug_error("gg",
134 "ggp_oauth_authorization_done: failed (code = %d)", 153 "ggp_oauth_authorization_done: failed (code = %d)",
135 status_code); 154 status_code);
136 ggp_oauth_data_free(data); 155 ggp_oauth_data_free(data);
143 auth = gg_oauth_generate_header(method, url, 162 auth = gg_oauth_generate_header(method, url,
144 purple_account_get_username(account), 163 purple_account_get_username(account),
145 purple_connection_get_password(data->gc), 164 purple_connection_get_password(data->gc),
146 data->token, data->token_secret); 165 data->token, data->token_secret);
147 166
148 msg = soup_message_new(method, url); 167 g_clear_object(&data->msg);
168 data->msg = msg = soup_message_new(method, url);
149 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX); 169 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
150 soup_message_headers_replace(soup_message_get_request_headers(msg), 170 soup_message_headers_replace(soup_message_get_request_headers(msg),
151 "Authorization", auth); 171 "Authorization", auth);
152 soup_session_queue_message(session, msg, ggp_oauth_access_token_got, data); 172 soup_session_send_and_read_async(SOUP_SESSION(source), msg,
173 G_PRIORITY_DEFAULT, NULL,
174 ggp_oauth_access_token_got, data);
153 175
154 g_free(auth); 176 g_free(auth);
155 } 177 }
156 178
157 static void 179 static void
158 ggp_oauth_request_token_got(SoupSession *session, SoupMessage *msg, 180 ggp_oauth_request_token_got(GObject *source, GAsyncResult *result,
159 gpointer user_data) 181 gpointer user_data)
160 { 182 {
183 SoupSession *session = SOUP_SESSION(source);
161 ggp_oauth_data *data = user_data; 184 ggp_oauth_data *data = user_data;
185 GBytes *response_body = NULL;
186 const char *buffer = NULL;
187 gsize size = 0;
162 PurpleAccount *account; 188 PurpleAccount *account;
163 PurpleXmlNode *xml; 189 PurpleXmlNode *xml;
190 SoupMessage *msg = NULL;
164 gchar *request_data; 191 gchar *request_data;
165 GBytes *body = NULL; 192 GBytes *body = NULL;
166 gboolean succ = TRUE; 193 gboolean succ = TRUE;
194 GError *error = NULL;
167 195
168 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc); 196 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
169 197
170 account = purple_connection_get_account(data->gc); 198 account = purple_connection_get_account(data->gc);
171 199
172 if (!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(msg))) { 200 if(!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(data->msg))) {
173 purple_debug_error("gg", "ggp_oauth_request_token_got: " 201 purple_debug_error("gg", "ggp_oauth_request_token_got: "
174 "requested token not received\n"); 202 "requested token not received\n");
175 ggp_oauth_data_free(data); 203 ggp_oauth_data_free(data);
176 return; 204 return;
177 } 205 }
178 206
207 response_body = soup_session_send_and_read_finish(session, result, &error);
208 if(response_body == NULL) {
209 purple_debug_error("gg", "ggp_oauth_access_token_got: failed: %s",
210 error->message);
211 ggp_oauth_data_free(data);
212 g_error_free(error);
213 return;
214 }
215
179 purple_debug_misc("gg", "ggp_oauth_request_token_got: " 216 purple_debug_misc("gg", "ggp_oauth_request_token_got: "
180 "got request token, doing authorization...\n"); 217 "got request token, doing authorization...\n");
181 218
182 xml = purple_xmlnode_from_str(msg->response_body->data, 219 buffer = g_bytes_get_data(response_body, &size);
183 msg->response_body->length); 220 xml = purple_xmlnode_from_str(buffer, size);
221 g_bytes_unref(response_body);
222
184 if (xml == NULL) { 223 if (xml == NULL) {
185 purple_debug_error("gg", "ggp_oauth_request_token_got: " 224 purple_debug_error("gg", "ggp_oauth_request_token_got: "
186 "invalid xml\n"); 225 "invalid xml\n");
187 ggp_oauth_data_free(data); 226 ggp_oauth_data_free(data);
188 return; 227 return;
203 "callback_url=http://www.mojageneracja.pl&request_token=%s&" 242 "callback_url=http://www.mojageneracja.pl&request_token=%s&"
204 "uin=%s&password=%s", data->token, 243 "uin=%s&password=%s", data->token,
205 purple_account_get_username(account), 244 purple_account_get_username(account),
206 purple_connection_get_password(data->gc)); 245 purple_connection_get_password(data->gc));
207 246
208 msg = soup_message_new("POST", "https://login.gadu-gadu.pl/authorize"); 247 g_clear_object(&data->msg);
248 data->msg = msg = soup_message_new("POST",
249 "https://login.gadu-gadu.pl/authorize");
209 // purple_http_request_set_max_len(msg, GGP_OAUTH_RESPONSE_MAX); 250 // purple_http_request_set_max_len(msg, GGP_OAUTH_RESPONSE_MAX);
210 /* we don't need any results, nor 302 redirection */ 251 /* we don't need any results, nor 302 redirection */
211 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT); 252 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);
212 body = g_bytes_new_take(request_data, strlen(request_data)); 253 body = g_bytes_new_take(request_data, strlen(request_data));
213 soup_message_set_request_body_from_bytes(msg, 254 soup_message_set_request_body_from_bytes(msg,
214 "application/x-www-form-urlencoded", 255 "application/x-www-form-urlencoded",
215 body); 256 body);
216 g_bytes_unref(body); 257 g_bytes_unref(body);
217 soup_session_queue_message(session, msg, ggp_oauth_authorization_done, 258 soup_session_send_and_read_async(session, msg, G_PRIORITY_DEFAULT, NULL,
218 data); 259 ggp_oauth_authorization_done, data);
219 } 260 }
220 261
221 void 262 void
222 ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback, 263 ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback,
223 gpointer user_data, const gchar *sign_method, 264 gpointer user_data, const gchar *sign_method,
242 data->callback = callback; 283 data->callback = callback;
243 data->user_data = user_data; 284 data->user_data = user_data;
244 data->sign_method = g_strdup(sign_method); 285 data->sign_method = g_strdup(sign_method);
245 data->sign_url = g_strdup(sign_url); 286 data->sign_url = g_strdup(sign_url);
246 287
247 msg = soup_message_new(method, url); 288 data->msg = msg = soup_message_new(method, url);
248 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX); 289 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
249 soup_message_headers_replace(soup_message_get_request_headers(msg), 290 soup_message_headers_replace(soup_message_get_request_headers(msg),
250 "Authorization", auth); 291 "Authorization", auth);
251 soup_session_queue_message(info->http, msg, ggp_oauth_request_token_got, 292 soup_session_send_and_read_async(info->http, msg, G_PRIORITY_DEFAULT, NULL,
252 data); 293 ggp_oauth_request_token_got, data);
253 294
254 g_free(auth); 295 g_free(auth);
255 } 296 }

mercurial