Mercurial > pidgin
annotate src/protocols/msn/nexus.c @ 19789:7e36832172a9
[gaim-migrate @ 16601]
change Windows Live ID authentication code style
to simplify the code.
add some comments
committed by Ma Yuan<mayuan2006@gmail.com>
committer: Ethan Blanton <elb@pidgin.im>
author | Ma Yuan <mayuan2006@gmail.com> |
---|---|
date | Sun, 30 Jul 2006 15:55:30 +0000 |
parents | 23258253c7a0 |
children | cabf9165c9ce |
rev | line source |
---|---|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
1 /** |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
2 * @file nexus.c MSN Nexus functions |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
3 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
4 * gaim |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
5 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
6 * Gaim is the legal property of its developers, whose names are too numerous |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
8 * source distribution. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
9 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
10 * This program is free software; you can redistribute it and/or modify |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
11 * it under the terms of the GNU General Public License as published by |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
12 * the Free Software Foundation; either version 2 of the License, or |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
13 * (at your option) any later version. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
14 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
15 * This program is distributed in the hope that it will be useful, |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
18 * GNU General Public License for more details. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
19 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
20 * You should have received a copy of the GNU General Public License |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
21 * along with this program; if not, write to the Free Software |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
23 */ |
8171 | 24 #include "msn.h" |
19784 | 25 #include "soap.h" |
8171 | 26 #include "nexus.h" |
27 #include "notification.h" | |
28 | |
19789 | 29 /*Local Function Prototype*/ |
30 static void nexus_login_connect_cb(gpointer data, GaimSslConnection *gsc,GaimInputCondition cond); | |
31 | |
8171 | 32 /************************************************************************** |
10481 | 33 * Main |
34 **************************************************************************/ | |
35 | |
36 MsnNexus * | |
37 msn_nexus_new(MsnSession *session) | |
38 { | |
39 MsnNexus *nexus; | |
40 | |
41 nexus = g_new0(MsnNexus, 1); | |
42 nexus->session = session; | |
19784 | 43 /*we must use SSL connection to do Windows Live ID authentication*/ |
19785 | 44 nexus->soapconn = msn_soap_new(session,nexus,1); |
45 | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
46 nexus->challenge_data = g_hash_table_new_full(g_str_hash, |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
47 g_str_equal, g_free, g_free); |
10481 | 48 |
49 return nexus; | |
50 } | |
51 | |
52 void | |
53 msn_nexus_destroy(MsnNexus *nexus) | |
54 { | |
55 if (nexus->challenge_data != NULL) | |
56 g_hash_table_destroy(nexus->challenge_data); | |
57 | |
19784 | 58 msn_soap_destroy(nexus->soapconn); |
10481 | 59 g_free(nexus); |
60 } | |
61 | |
62 /************************************************************************** | |
8171 | 63 * Login |
64 **************************************************************************/ | |
65 static void | |
19789 | 66 nexus_login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) |
8171 | 67 { |
19784 | 68 MsnSoapConn * soapconn = data; |
8171 | 69 MsnSession *session; |
70 | |
19784 | 71 session = soapconn->session; |
8171 | 72 g_return_if_fail(session != NULL); |
73 | |
19784 | 74 msn_session_set_error(session, MSN_ERROR_AUTH, _("Windows Live ID authentication:Unable to connect")); |
10568 | 75 /* the above line will result in nexus being destroyed, so we don't want |
76 * to destroy it here, or we'd crash */ | |
8171 | 77 } |
78 | |
19789 | 79 /*process the SOAP reply, get the Authentication Info*/ |
8171 | 80 static void |
19784 | 81 nexus_login_read_cb(gpointer data, gint source, GaimInputCondition cond) |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
82 { |
19784 | 83 MsnSoapConn * soapconn = data; |
84 MsnNexus *nexus; | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
85 MsnSession *session; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
86 |
19789 | 87 char *base, *c; |
88 char *msn_twn_t,*msn_twn_p; | |
89 char *login_params; | |
90 char **elems, **cur, **tokens; | |
91 char * cert_str; | |
92 | |
19784 | 93 nexus = soapconn->parent; |
94 g_return_if_fail(nexus != NULL); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
95 session = nexus->session; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
96 g_return_if_fail(session != NULL); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
97 |
19786 | 98 // gaim_debug_misc("msn", "TWN Server Reply: {%s}\n", soapconn->read_buf); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
99 |
19789 | 100 /*reply OK, we should process the SOAP body*/ |
101 gaim_debug_info("MaYuan","Windows Live ID Reply OK!\n"); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
102 |
19789 | 103 //TODO: we should parse it using XML |
104 base = g_strstr_len(soapconn->read_buf, soapconn->read_len, TWN_START_TOKEN); | |
105 base += strlen(TWN_START_TOKEN); | |
106 c = g_strstr_len(soapconn->read_buf, soapconn->read_len, TWN_END_TOKEN); | |
107 login_params = g_strndup(base, c - base); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
108 |
19789 | 109 // gaim_debug_info("msn", "TWN Cert: {%s}\n", login_params); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
110 |
19789 | 111 /* Parse the challenge data. */ |
112 elems = g_strsplit(login_params, "&", 0); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
113 |
19789 | 114 for (cur = elems; *cur != NULL; cur++){ |
115 tokens = g_strsplit(*cur, "=", 2); | |
116 g_hash_table_insert(nexus->challenge_data, tokens[0], tokens[1]); | |
117 /* Don't free each of the tokens, only the array. */ | |
118 g_free(tokens); | |
119 } | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
120 |
19789 | 121 g_strfreev(elems); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
122 |
19789 | 123 msn_twn_t = (char *)g_hash_table_lookup(nexus->challenge_data, "t"); |
124 msn_twn_p = (char *)g_hash_table_lookup(nexus->challenge_data, "p"); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
125 |
19789 | 126 /*setup the t and p parameter for session*/ |
127 if (session->passport_info.t != NULL){ | |
128 g_free(session->passport_info.t); | |
129 } | |
130 session->passport_info.t = g_strdup(msn_twn_t); | |
19783 | 131 |
19789 | 132 if (session->passport_info.p != NULL) |
133 g_free(session->passport_info.p); | |
134 session->passport_info.p = g_strdup(msn_twn_p); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
135 |
19789 | 136 cert_str = g_strdup_printf("t=%s&p=%s",msn_twn_t,msn_twn_p); |
137 msn_got_login_params(session, cert_str); | |
19784 | 138 |
19789 | 139 gaim_debug_info("MaYuan","close nexus connection! \n"); |
140 g_free(cert_str); | |
141 g_free(login_params); | |
142 msn_nexus_destroy(nexus); | |
143 session->nexus = NULL; | |
19784 | 144 |
19789 | 145 return; |
19784 | 146 } |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
147 |
19784 | 148 static void |
149 nexus_login_written_cb(gpointer data, gint source, GaimInputCondition cond) | |
150 { | |
151 MsnSoapConn * soapconn = data; | |
152 | |
153 soapconn->read_cb = nexus_login_read_cb; | |
154 msn_soap_read_cb(data,source,cond); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
155 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
156 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
157 |
19789 | 158 /*when connect, do the SOAP Style windows Live ID authentication */ |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
159 void |
19789 | 160 nexus_login_connect_cb(gpointer data, GaimSslConnection *gsc, |
8171 | 161 GaimInputCondition cond) |
162 { | |
19784 | 163 MsnSoapConn *soapconn; |
164 MsnNexus * nexus; | |
8171 | 165 MsnSession *session; |
166 char *username, *password; | |
19783 | 167 char *request_str, *head, *tail,*challenge_str; |
8171 | 168 |
19783 | 169 gaim_debug_info("MaYuan","starting Windows Live ID authentication\n"); |
19784 | 170 |
171 soapconn = data; | |
172 g_return_if_fail(soapconn != NULL); | |
173 | |
174 nexus = soapconn->parent; | |
8171 | 175 g_return_if_fail(nexus != NULL); |
176 | |
19784 | 177 session = soapconn->session; |
8171 | 178 g_return_if_fail(session != NULL); |
179 | |
10519 | 180 msn_session_set_login_step(session, MSN_LOGIN_STEP_GET_COOKIE); |
10481 | 181 |
19784 | 182 /*prepare the Windows Live ID authentication token*/ |
19783 | 183 username = g_strdup(gaim_account_get_username(session->account)); |
184 password = g_strdup(gaim_connection_get_password(session->account->gc)); | |
185 // g_strdup(gaim_url_encode(gaim_connection_get_password(session->account->gc))); | |
12457 | 186 |
19783 | 187 challenge_str = g_strdup_printf( |
188 "lc=%s&id=%s&tw=%s&fs=%s&ru=%s&ct=%s&kpp=%s&kv=%s&ver=%s&rn=%s&tpf=%s\r\n", | |
8171 | 189 (char *)g_hash_table_lookup(nexus->challenge_data, "lc"), |
190 (char *)g_hash_table_lookup(nexus->challenge_data, "id"), | |
191 (char *)g_hash_table_lookup(nexus->challenge_data, "tw"), | |
192 (char *)g_hash_table_lookup(nexus->challenge_data, "fs"), | |
193 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
19783 | 194 (char *)g_hash_table_lookup(nexus->challenge_data, "ct"), |
8171 | 195 (char *)g_hash_table_lookup(nexus->challenge_data, "kpp"), |
196 (char *)g_hash_table_lookup(nexus->challenge_data, "kv"), | |
197 (char *)g_hash_table_lookup(nexus->challenge_data, "ver"), | |
19783 | 198 (char *)g_hash_table_lookup(nexus->challenge_data, "rn"), |
199 (char *)g_hash_table_lookup(nexus->challenge_data, "tpf") | |
200 ); | |
201 | |
202 /*build the SOAP windows Live ID XML body */ | |
203 tail = g_strdup_printf(TWN_ENVELOP_TEMPLATE,username,password,challenge_str ); | |
8171 | 204 |
19784 | 205 soapconn->login_path = g_strdup(TWN_POST_URL); |
19783 | 206 head = g_strdup_printf( |
207 "POST %s HTTP/1.1\r\n" | |
208 "Accept: text/*\r\n" | |
209 "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n" | |
210 "Host: %s\r\n" | |
211 "Content-Length: %d\r\n" | |
212 "Connection: Keep-Alive\r\n" | |
213 "Cache-Control: no-cache\r\n\r\n", | |
19784 | 214 soapconn->login_path,soapconn->login_host,strlen(tail)); |
8171 | 215 |
19783 | 216 request_str = g_strdup_printf("%s%s", head,tail); |
19786 | 217 // gaim_debug_misc("msn", "TWN Sending: {%s}\n", request_str); |
12457 | 218 |
219 g_free(head); | |
220 g_free(tail); | |
8171 | 221 g_free(username); |
222 g_free(password); | |
223 | |
19784 | 224 /*prepare to send the SOAP request*/ |
225 msn_soap_write(soapconn,request_str,nexus_login_written_cb); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
226 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
227 return; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
228 } |
8171 | 229 |
19783 | 230 /************************************************************************** |
231 * Connect | |
232 **************************************************************************/ | |
233 void | |
234 msn_nexus_connect(MsnNexus *nexus) | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
235 { |
19783 | 236 /* Authenticate via Windows Live ID. */ |
237 gaim_debug_info("MaYuan","msn_nexus_connect...\n"); | |
19784 | 238 |
19789 | 239 msn_soap_init(nexus->soapconn,MSN_TWN_SERVER,1,nexus_login_connect_cb,nexus_login_error_cb); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
240 } |
19784 | 241 |