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

changeset 40022
8faeba4c2dfe
parent 40021
6b1b05adda03
child 40058
8a56f10bd1fb
equal deleted inserted replaced
40021:6b1b05adda03 40022:8faeba4c2dfe
26 * along with this program; if not, write to the Free Software 26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
28 */ 28 */
29 29
30 #include "oauth-purple.h" 30 #include "oauth-purple.h"
31 #include "gg.h"
31 32
32 #include "oauth.h" 33 #include "oauth.h"
33 #include "../utils.h" 34 #include "../utils.h"
34 #include "../xml.h" 35 #include "../xml.h"
35 36
57 g_free(data->sign_url); 58 g_free(data->sign_url);
58 g_free(data); 59 g_free(data);
59 } 60 }
60 61
61 static void 62 static void
62 ggp_oauth_access_token_got(PurpleHttpConnection *http_conn, 63 ggp_oauth_access_token_got(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg,
63 PurpleHttpResponse *response, gpointer user_data) 64 gpointer user_data)
64 { 65 {
65 ggp_oauth_data *data = user_data; 66 ggp_oauth_data *data = user_data;
66 gchar *token, *token_secret; 67 gchar *token, *token_secret;
67 PurpleXmlNode *xml; 68 PurpleXmlNode *xml;
68 const gchar *xml_raw;
69 gboolean succ = TRUE; 69 gboolean succ = TRUE;
70 70
71 xml_raw = purple_http_response_get_data(response, NULL); 71 xml = purple_xmlnode_from_str(msg->response_body->data,
72 xml = purple_xmlnode_from_str(xml_raw, -1); 72 msg->response_body->length);
73 if (xml == NULL) { 73 if (xml == NULL) {
74 purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml"); 74 purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml");
75 ggp_oauth_data_free(data); 75 ggp_oauth_data_free(data);
76 return; 76 return;
77 } 77 }
110 g_free(token_secret); 110 g_free(token_secret);
111 ggp_oauth_data_free(data); 111 ggp_oauth_data_free(data);
112 } 112 }
113 113
114 static void 114 static void
115 ggp_oauth_authorization_done(PurpleHttpConnection *http_conn, 115 ggp_oauth_authorization_done(SoupSession *session, SoupMessage *msg,
116 PurpleHttpResponse *response, gpointer user_data) 116 gpointer user_data)
117 { 117 {
118 ggp_oauth_data *data = user_data; 118 ggp_oauth_data *data = user_data;
119 PurpleAccount *account; 119 PurpleAccount *account;
120 PurpleHttpRequest *req;
121 char *auth; 120 char *auth;
122 const char *method = "POST"; 121 const char *method = "POST";
123 const char *url = "http://api.gadu-gadu.pl/access_token"; 122 const char *url = "http://api.gadu-gadu.pl/access_token";
124 int response_code;
125 123
126 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc); 124 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
127 125
128 account = purple_connection_get_account(data->gc); 126 account = purple_connection_get_account(data->gc);
129 127
130 response_code = purple_http_response_get_code(response); 128 if (msg->status_code != 302) {
131 if (response_code != 302) {
132 purple_debug_error("gg", 129 purple_debug_error("gg",
133 "ggp_oauth_authorization_done: failed (code = %d)", 130 "ggp_oauth_authorization_done: failed (code = %d)",
134 response_code); 131 msg->status_code);
135 ggp_oauth_data_free(data); 132 ggp_oauth_data_free(data);
136 return; 133 return;
137 } 134 }
138 135
139 purple_debug_misc("gg", "ggp_oauth_authorization_done: authorization done, " 136 purple_debug_misc("gg", "ggp_oauth_authorization_done: authorization done, "
142 auth = gg_oauth_generate_header(method, url, 139 auth = gg_oauth_generate_header(method, url,
143 purple_account_get_username(account), 140 purple_account_get_username(account),
144 purple_connection_get_password(data->gc), 141 purple_connection_get_password(data->gc),
145 data->token, data->token_secret); 142 data->token, data->token_secret);
146 143
147 req = purple_http_request_new(url); 144 msg = soup_message_new(method, url);
148 purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX); 145 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
149 purple_http_request_set_method(req, method); 146 soup_message_headers_replace(msg->request_headers, "Authorization", auth);
150 purple_http_request_header_set(req, "Authorization", auth); 147 soup_session_queue_message(session, msg, ggp_oauth_access_token_got, data);
151 purple_http_request(data->gc, req, ggp_oauth_access_token_got, data);
152 purple_http_request_unref(req);
153 148
154 g_free(auth); 149 g_free(auth);
155 } 150 }
156 151
157 static void ggp_oauth_request_token_got(PurpleHttpConnection *http_conn, 152 static void
158 PurpleHttpResponse *response, gpointer user_data) 153 ggp_oauth_request_token_got(SoupSession *session, SoupMessage *msg,
154 gpointer user_data)
159 { 155 {
160 ggp_oauth_data *data = user_data; 156 ggp_oauth_data *data = user_data;
161 PurpleAccount *account; 157 PurpleAccount *account;
162 PurpleHttpRequest *req;
163 PurpleXmlNode *xml; 158 PurpleXmlNode *xml;
164 gchar *request_data; 159 gchar *request_data;
165 gboolean succ = TRUE; 160 gboolean succ = TRUE;
166 const gchar *xml_raw;
167 161
168 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc); 162 PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
169 163
170 account = purple_connection_get_account(data->gc); 164 account = purple_connection_get_account(data->gc);
171 165
172 if (!purple_http_response_is_successful(response)) { 166 if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
173 purple_debug_error("gg", "ggp_oauth_request_token_got: " 167 purple_debug_error("gg", "ggp_oauth_request_token_got: "
174 "requested token not received\n"); 168 "requested token not received\n");
175 ggp_oauth_data_free(data); 169 ggp_oauth_data_free(data);
176 return; 170 return;
177 } 171 }
178 172
179 purple_debug_misc("gg", "ggp_oauth_request_token_got: " 173 purple_debug_misc("gg", "ggp_oauth_request_token_got: "
180 "got request token, doing authorization...\n"); 174 "got request token, doing authorization...\n");
181 175
182 xml_raw = purple_http_response_get_data(response, NULL); 176 xml = purple_xmlnode_from_str(msg->response_body->data,
183 xml = purple_xmlnode_from_str(xml_raw, -1); 177 msg->response_body->length);
184 if (xml == NULL) { 178 if (xml == NULL) {
185 purple_debug_error("gg", "ggp_oauth_request_token_got: " 179 purple_debug_error("gg", "ggp_oauth_request_token_got: "
186 "invalid xml\n"); 180 "invalid xml\n");
187 ggp_oauth_data_free(data); 181 ggp_oauth_data_free(data);
188 return; 182 return;
203 "callback_url=http://www.mojageneracja.pl&request_token=%s&" 197 "callback_url=http://www.mojageneracja.pl&request_token=%s&"
204 "uin=%s&password=%s", data->token, 198 "uin=%s&password=%s", data->token,
205 purple_account_get_username(account), 199 purple_account_get_username(account),
206 purple_connection_get_password(data->gc)); 200 purple_connection_get_password(data->gc));
207 201
208 req = purple_http_request_new("https://login.gadu-gadu.pl/authorize"); 202 msg = soup_message_new("POST", "https://login.gadu-gadu.pl/authorize");
209 purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX); 203 // purple_http_request_set_max_len(msg, GGP_OAUTH_RESPONSE_MAX);
210 /* we don't need any results, nor 302 redirection */ 204 /* we don't need any results, nor 302 redirection */
211 purple_http_request_set_max_redirects(req, 0); 205 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);
212 purple_http_request_set_method(req, "POST"); 206 soup_message_set_request(msg, "application/x-www-form-urlencoded",
213 purple_http_request_header_set(req, "Content-Type", "application/x-www-form-urlencoded"); 207 SOUP_MEMORY_TAKE, request_data, -1);
214 purple_http_request_set_contents(req, request_data, -1); 208 soup_session_queue_message(session, msg, ggp_oauth_authorization_done,
215 purple_http_request(data->gc, req, ggp_oauth_authorization_done, data); 209 data);
216 purple_http_request_unref(req);
217
218 g_free(request_data);
219 } 210 }
220 211
221 void 212 void
222 ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback, 213 ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback,
223 gpointer user_data, const gchar *sign_method, 214 gpointer user_data, const gchar *sign_method,
224 const gchar *sign_url) 215 const gchar *sign_url)
225 { 216 {
217 GGPInfo *info = purple_connection_get_protocol_data(gc);
226 PurpleAccount *account = purple_connection_get_account(gc); 218 PurpleAccount *account = purple_connection_get_account(gc);
227 PurpleHttpRequest *req; 219 SoupMessage *msg;
228 char *auth; 220 char *auth;
229 const char *method = "POST"; 221 const char *method = "POST";
230 const char *url = "http://api.gadu-gadu.pl/request_token"; 222 const char *url = "http://api.gadu-gadu.pl/request_token";
231 ggp_oauth_data *data; 223 ggp_oauth_data *data;
232 224
241 data->callback = callback; 233 data->callback = callback;
242 data->user_data = user_data; 234 data->user_data = user_data;
243 data->sign_method = g_strdup(sign_method); 235 data->sign_method = g_strdup(sign_method);
244 data->sign_url = g_strdup(sign_url); 236 data->sign_url = g_strdup(sign_url);
245 237
246 req = purple_http_request_new(url); 238 msg = soup_message_new(method, url);
247 purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX); 239 // purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
248 purple_http_request_set_method(req, method); 240 soup_message_headers_replace(msg->request_headers, "Authorization", auth);
249 purple_http_request_header_set(req, "Authorization", auth); 241 soup_session_queue_message(info->http, msg, ggp_oauth_request_token_got,
250 purple_http_request(gc, req, ggp_oauth_request_token_got, data); 242 data);
251 purple_http_request_unref(req);
252 243
253 g_free(auth); 244 g_free(auth);
254 } 245 }

mercurial