1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
|
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.
Copyright (c) 2012-2019 Miranda NG team
Copyright (c) 2007-2012 Boris Krasnovskiy.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include <WinInet.h>
#include "msn_proto.h"
#include "msn_ieembed.h"
#include <openssl\des.h>
extern "C"
{
#include "skylogin/skylogin.h"
};
#define LOGIN_POST_PARAMS "client_id=00000000480BC46C&scope=service%3A%3Askype.com%3A%3AMBI_SSL&response_type=token&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf"
#define AUTH_URL "https://login.live.com/oauth20_authorize.srf?"LOGIN_POST_PARAMS
#define POST_URL "https://login.live.com/ppsecure/post.srf?"LOGIN_POST_PARAMS
/* WinINET delayloading */
typedef BOOL (*pfnInternetGetCookieExA)(LPCSTR, LPCSTR, LPSTR, LPDWORD, DWORD, LPVOID);
pfnInternetGetCookieExA fpInternetGetCookieExA = nullptr;
#define LOAD_FN(name) (##name = (pfn##name)GetProcAddress(hLibSkylogin, #name))
static const char defaultPassportUrl[] = "https://login.live.com/RST2.srf";
static const char authPacket[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<s:Envelope"
" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\""
" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""
" xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\""
" xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\""
" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""
" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\""
" xmlns:wssc=\"http://schemas.xmlsoap.org/ws/2005/02/sc\""
" xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\">"
"<s:Header>"
"<wsa:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</wsa:Action>"
"<wsa:To s:mustUnderstand=\"1\">HTTPS://login.live.com:443//RST2.srf</wsa:To>"
"<wsa:MessageID>%u</wsa:MessageID>"
"<ps:AuthInfo xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" Id=\"PPAuthInfo\">"
"<ps:HostingApp>{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}</ps:HostingApp>"
"<ps:BinaryVersion>5</ps:BinaryVersion>"
"<ps:UIVersion>1</ps:UIVersion>"
"<ps:Cookies />"
"<ps:RequestParams>AQAAAAIAAABsYwQAAAAxMDMz</ps:RequestParams>"
"</ps:AuthInfo>"
"<wsse:Security>"
"<wsse:UsernameToken wsu:Id=\"user\">"
"<wsse:Username>%s</wsse:Username>"
"<wsse:Password>%s</wsse:Password>"
"</wsse:UsernameToken>"
"<wsu:Timestamp Id=\"Timestamp\">"
"<wsu:Created>%S</wsu:Created>"
"<wsu:Expires>%S</wsu:Expires>"
"</wsu:Timestamp>"
"</wsse:Security>"
"</s:Header>"
"<s:Body>"
"<ps:RequestMultipleSecurityTokens xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" Id=\"RSTS\">"
"<wst:RequestSecurityToken Id=\"RST0\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>http://Passport.NET/tb</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST1\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>chatservice.live.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"MBI_SSL\" />"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST2\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>messenger.msn.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"?id=507\" />"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST3\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>messengersecure.live.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"MBI_SSL\" />"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST4\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>contacts.msn.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"MBI\" />"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST5\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>storage.msn.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"MBI\" />"
"</wst:RequestSecurityToken>"
"<wst:RequestSecurityToken Id=\"RST6\">"
"<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
"<wsp:AppliesTo>"
"<wsa:EndpointReference>"
"<wsa:Address>sup.live.com</wsa:Address>"
"</wsa:EndpointReference>"
"</wsp:AppliesTo>"
"<wsp:PolicyReference URI=\"MBI\" />"
"</wst:RequestSecurityToken>"
"</ps:RequestMultipleSecurityTokens>"
"</s:Body>"
"</s:Envelope>";
/////////////////////////////////////////////////////////////////////////////////////////
// Tokens, tokens, tokens.....
GenericToken::GenericToken(const char *pszTokenName) :
m_pszTokenName(pszTokenName),
m_pszToken(nullptr),
m_tExpires(0),
m_proto(nullptr)
{
}
GenericToken::~GenericToken()
{
mir_free(m_pszToken);
}
bool GenericToken::Load()
{
DBVARIANT dbv;
char szTokenName[64];
mir_snprintf(szTokenName, sizeof(szTokenName), "%sToken", m_pszTokenName);
if (m_proto->getString(szTokenName, &dbv) == 0) {
replaceStr(m_pszToken, dbv.pszVal);
db_free(&dbv);
mir_snprintf(szTokenName, sizeof(szTokenName), "%sExpiretime", m_pszTokenName);
m_tExpires = m_proto->getDword(szTokenName, 0);
return true;
}
return false;
}
void GenericToken::Save()
{
char szTokenName[64];
mir_snprintf(szTokenName, sizeof(szTokenName), "%sToken", m_pszTokenName);
m_proto->setString(szTokenName, m_pszToken);
mir_snprintf(szTokenName, sizeof(szTokenName), "%sExpiretime", m_pszTokenName);
m_proto->setDword(szTokenName, m_tExpires);
}
bool GenericToken::Expired(time_t t)
{
return t + 3600 >= m_tExpires;
}
void GenericToken::SetToken(const char *pszToken, time_t tExpires)
{
replaceStr(m_pszToken, pszToken);
m_tExpires = tExpires;
Save();
}
const char *GenericToken::Token()
{
Refresh();
return m_pszToken;
}
void GenericToken::Clear()
{
char szTokenName[64];
mir_free(m_pszToken);
m_pszToken = nullptr;
m_tExpires = 0;
mir_snprintf(szTokenName, sizeof(szTokenName), "%sToken", m_pszTokenName);
m_proto->delSetting(szTokenName);
mir_snprintf(szTokenName, sizeof(szTokenName), "%sExpiretime", m_pszTokenName);
m_proto->delSetting(szTokenName);
}
OAuthToken::OAuthToken(const char *pszTokenName, const char *pszService, bool bPrependT) :
GenericToken(pszTokenName),
m_pszService(pszService),
m_bPreprendT(bPrependT)
{
}
bool OAuthToken::Refresh(bool bForce)
{
if ((!bForce && !Expired()) || !m_proto->authRefreshToken)
return false;
time_t tExpires;
CMStringA szToken, szRefreshToken;
if (!m_proto->RefreshOAuth(m_proto->authRefreshToken, m_pszService, &szToken, &szRefreshToken, &tExpires))
return false;
if (m_bPreprendT)
szToken.Insert(0, "t=");
replaceStr(m_proto->authRefreshToken, szRefreshToken);
m_proto->setString("authRefreshToken", szRefreshToken);
SetToken(szToken, tExpires);
return true;
}
SkypeToken::SkypeToken(const char *pszTokenName) :
GenericToken(pszTokenName)
{
}
bool SkypeToken::Refresh(bool bForce)
{
if (!bForce && !Expired())
return false;
NETLIBHTTPHEADER headers[1] = { { "User-Agent", (char*)MSN_USER_AGENT } };
NETLIBHTTPREQUEST nlhr = { 0 };
nlhr.cbSize = sizeof(nlhr);
nlhr.requestType = REQUEST_POST;
nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_PERSISTENT | NLHRF_REDIRECT;
nlhr.nlc = m_proto->hHttpsConnection;
nlhr.headersCount = _countof(headers);
nlhr.headers = headers;
CMStringA szPOST;
if (m_proto->MyOptions.netId == NETID_SKYPE) {
BYTE digest[16];
char szPassword[100] = { 0 };
int cbPasswd = mir_snprintf(szPassword, sizeof(szPassword), "%s\nskyper\n", m_proto->MyOptions.szEmail);
if (db_get_static(NULL, m_proto->m_szModuleName, "Password", szPassword + cbPasswd, sizeof(szPassword) - cbPasswd - 1))
return false;
mir_md5_hash((BYTE*)szPassword, mir_strlen(szPassword), digest);
mir_base64_encodebuf(digest, sizeof(digest), szPassword, sizeof(szPassword));
nlhr.szUrl = "https://api.skype.com/login/skypetoken";
szPOST.Format("scopes=client&clientVersion=%s&username=%s&passwordHash=%s", msnProductVer, m_proto->MyOptions.szEmail, szPassword);
}
else {
// Get skype_token
nlhr.szUrl = "https://api.skype.com/rps/skypetoken";
szPOST.Format("scopes=client&clientVersion=%s&access_token=%s&partner=999", msnProductVer, m_proto->authSkypeComToken.Token());
}
nlhr.dataLength = szPOST.GetLength();
nlhr.pData = szPOST.GetBuffer();
m_proto->mHttpsTS = clock();
NLHR_PTR nlhrReply(Netlib_HttpTransaction(m_proto->m_hNetlibUser, &nlhr));
m_proto->mHttpsTS = clock();
if (nlhrReply) {
m_proto->hHttpsConnection = nlhrReply->nlc;
if (nlhrReply->resultCode == 200 && nlhrReply->pData) {
JSONROOT root(nlhrReply->pData);
if (root) {
CMStringA szToken((*root)["skypetoken"].as_mstring());
if (!szToken.IsEmpty()) {
time_t tExpires = (*root)["expiresIn"].as_int();
if (tExpires == 0)
tExpires = 86400;
SetToken("skype_token " + szToken, time(0) + tExpires);
return true;
}
}
}
}
else m_proto->hHttpsConnection = nullptr;
return false;
}
const char* SkypeToken::XSkypetoken()
{
Refresh();
if (m_pszToken) {
char *pszRet = strchr(m_pszToken, ' ');
if (pszRet) return pszRet + 1;
}
return nullptr;
}
int debugLogSkyLoginA(void *Pproto, LPCSTR szFormat, ...)
{
CMsnProto *pProto = (CMsnProto *)Pproto;
va_list args;
va_start(args, szFormat);
CMStringA body; body.FormatV(szFormat, args);
va_end(args);
pProto->debugLogA("%s", body.c_str());
return 1;
}
// Performs the MSN Passport login via TLS
int CMsnProto::MSN_GetPassportAuth(void)
{
int retVal = -1;
if (!bPassportAuth && authRefreshToken) {
// Slow authentication required by fetching multiple tokens, i.e. 2-factor auth :(
MSN_RefreshOAuthTokens(false);
return 0;
}
char szPassword[100];
if (db_get_static(NULL, m_szModuleName, "Password", szPassword, sizeof(szPassword)))
return 0;
szPassword[99] = 0;
time_t ts = time(0);
wchar_t szTs1[64], szTs2[64];
TimeZone_PrintTimeStamp(UTC_TIME_HANDLE, ts, L"I", szTs1, _countof(szTs1), 0);
TimeZone_PrintTimeStamp(UTC_TIME_HANDLE, ts + 20 * 60, L"I", szTs2, _countof(szTs2), 0);
CMStringA szAuthInfo(FORMAT, authPacket, int(ts), MyOptions.szEmail, ptrA(HtmlEncode(szPassword)), szTs1, szTs2);
char* szPassportHost = (char*)mir_alloc(256);
if (db_get_static(NULL, m_szModuleName, "MsnPassportHost", szPassportHost, 256))
mir_strcpy(szPassportHost, defaultPassportUrl);
bool defaultUrlAllow = mir_strcmp(szPassportHost, defaultPassportUrl) != 0;
char *tResult = nullptr;
while (retVal == -1) {
unsigned status;
tResult = getSslResult(&szPassportHost, szAuthInfo, nullptr, status);
if (tResult == nullptr) {
if (defaultUrlAllow) {
mir_strcpy(szPassportHost, defaultPassportUrl);
defaultUrlAllow = false;
continue;
}
else {
retVal = 4;
break;
}
}
switch (status) {
case 200:
const char *errurl;
{
errurl = nullptr;
ezxml_t xml = ezxml_parse_str(tResult, mir_strlen(tResult));
ezxml_t tokr = ezxml_get(xml, "S:Body", 0,
"wst:RequestSecurityTokenResponseCollection", 0,
"wst:RequestSecurityTokenResponse", -1);
while (tokr != nullptr) {
ezxml_t toks = ezxml_get(tokr, "wst:RequestedSecurityToken", 0,
"wsse:BinarySecurityToken", -1);
const char* addr = ezxml_txt(ezxml_get(tokr, "wsp:AppliesTo", 0,
"wsa:EndpointReference", 0, "wsa:Address", -1));
ezxml_t xml_expires = ezxml_get(tokr, "wst:Lifetime", 0, "wsu:Expires", -1);
time_t expires;
expires = xml_expires ? IsoToUnixTime(ezxml_txt(xml_expires)) : time(0) + 86400;
if (mir_strcmp(addr, "http://Passport.NET/tb") == 0) {
ezxml_t node = ezxml_get(tokr, "wst:RequestedSecurityToken", 0, "EncryptedData", -1);
free(hotAuthToken);
hotAuthToken = ezxml_toxml(node, 0);
setString("hotAuthToken", hotAuthToken);
node = ezxml_get(tokr, "wst:RequestedProofToken", 0, "wst:BinarySecret", -1);
replaceStr(hotSecretToken, ezxml_txt(node));
setString("hotSecretToken", hotSecretToken);
}
else if (mir_strcmp(addr, "chatservice.live.com") == 0) {
ezxml_t node = ezxml_get(tokr, "wst:RequestedProofToken", 0,
"wst:BinarySecret", -1);
if (toks) {
authStrToken.SetToken(ezxml_txt(toks), expires);
replaceStr(authSecretToken, ezxml_txt(node));
retVal = 0;
}
else {
errurl = ezxml_txt(ezxml_get(tokr, "S:Fault", 0, "psf:pp", 0, "psf:flowurl", -1));
}
}
else if (mir_strcmp(addr, "messenger.msn.com") == 0 && toks) {
const char* tok = ezxml_txt(toks);
char* ch = (char*)strchr(tok, '&');
*ch = 0;
replaceStr(tAuthToken, tok + 2);
replaceStr(pAuthToken, ch + 3);
*ch = '&';
}
else if (mir_strcmp(addr, "contacts.msn.com") == 0 && toks)
authContactToken.SetToken(ezxml_txt(toks), expires);
else if (mir_strcmp(addr, "messengersecure.live.com") == 0 && toks) {
replaceStr(oimSendToken, ezxml_txt(toks));
}
else if (mir_strcmp(addr, "storage.msn.com") == 0 && toks)
authStorageToken.SetToken(ezxml_txt(toks), expires);
tokr = ezxml_next(tokr);
}
if (retVal != 0) {
if (errurl) {
debugLogA("Starting URL: '%s'", errurl);
Utils_OpenUrl(errurl);
}
ezxml_t tokf = ezxml_get(xml, "S:Body", 0, "S:Fault", 0, "S:Detail", -1);
ezxml_t tokrdr = ezxml_child(tokf, "psf:redirectUrl");
if (tokrdr != nullptr) {
mir_strcpy(szPassportHost, ezxml_txt(tokrdr));
debugLogA("Redirected to '%s'", szPassportHost);
}
else {
const char* szFault = ezxml_txt(ezxml_get(tokf, "psf:error", 0, "psf:value", -1));
retVal = mir_strcmp(szFault, "0x80048821") == 0 ? 3 : (tokf ? 5 : 7);
if (retVal != 3 && defaultUrlAllow) {
mir_strcpy(szPassportHost, defaultPassportUrl);
defaultUrlAllow = false;
retVal = -1;
}
else if (retVal != 3 && retVal != 7) {
char err[512];
mir_snprintf(err, "Unknown Authentication error: %s", szFault);
MSN_ShowError(err);
}
}
}
ezxml_free(xml);
}
break;
default:
if (defaultUrlAllow) {
mir_strcpy(szPassportHost, defaultPassportUrl);
defaultUrlAllow = false;
}
else
retVal = 6;
}
mir_free(tResult);
}
if (retVal != 0) {
if (!g_bTerminated) {
switch (retVal) {
case 3:
MSN_ShowError("Your username or password is incorrect");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_WRONGPASSWORD);
break;
case 5:
break;
default:
MSN_ShowError("Unable to contact MS Passport servers check proxy/firewall settings");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_NOSERVER);
break;
}
}
}
else setString("MsnPassportHost", szPassportHost);
mir_free(szPassportHost);
debugLogA("MSN_CheckRedirector exited with errorCode = %d", retVal);
return retVal;
}
static void derive_key(BYTE* der, unsigned char* key, size_t keylen, unsigned char* data, size_t datalen)
{
BYTE hash1[MIR_SHA1_HASH_SIZE], hash2[MIR_SHA1_HASH_SIZE],
hash3[MIR_SHA1_HASH_SIZE], hash4[MIR_SHA1_HASH_SIZE];
const size_t buflen = MIR_SHA1_HASH_SIZE + datalen;
BYTE* buf = (BYTE*)alloca(buflen);
unsigned int len;
HMAC(EVP_sha1(), key, (int)keylen, data, (int)datalen, hash1, &len);
HMAC(EVP_sha1(), key, (int)keylen, hash1, MIR_SHA1_HASH_SIZE, hash3, &len);
memcpy(buf, hash1, MIR_SHA1_HASH_SIZE);
memcpy(buf + MIR_SHA1_HASH_SIZE, data, datalen);
HMAC(EVP_sha1(), key, (int)keylen, buf, buflen, hash2, &len);
memcpy(buf, hash3, MIR_SHA1_HASH_SIZE);
memcpy(buf + MIR_SHA1_HASH_SIZE, data, datalen);
HMAC(EVP_sha1(), key, (int)keylen, buf, buflen, hash4, &len);
memcpy(der, hash2, MIR_SHA1_HASH_SIZE);
memcpy(der + MIR_SHA1_HASH_SIZE, hash4, 4);
}
CMStringA CMsnProto::HotmailLogin(const char* url)
{
unsigned char nonce[24];
Utils_GetRandom(nonce, sizeof(nonce));
size_t key1len;
mir_ptr<BYTE> key1((BYTE*)mir_base64_decode(hotSecretToken, &key1len));
static const unsigned char encdata[] = "WS-SecureConversation";
const size_t data1len = sizeof(nonce) + sizeof(encdata) - 1;
unsigned char* data1 = (unsigned char*)alloca(data1len);
memcpy(data1, encdata, sizeof(encdata) - 1);
memcpy(data1 + sizeof(encdata) - 1, nonce, sizeof(nonce));
unsigned char key2[MIR_SHA1_HASH_SIZE + 4];
derive_key(key2, key1, key1len, data1, data1len);
CMStringA result;
result.Format("%s&da=%s&nonce=", url, mir_urlEncode(hotAuthToken).c_str());
ptrA noncenc(mir_base64_encode(nonce, sizeof(nonce)));
result.Append(mir_urlEncode(noncenc));
BYTE hash[MIR_SHA1_HASH_SIZE];
unsigned int len;
HMAC(EVP_sha1(), key2, sizeof(key2), (BYTE*)result.GetString(), result.GetLength(), hash, &len);
ptrA szHash(mir_base64_encode(hash, sizeof(hash)));
result.AppendFormat("&hash=%s", mir_urlEncode(szHash).c_str());
return result;
}
/* 1 - Login successful
0 - Login failed
-1 - Loading Skylogin library failed
-2 - Functions cannot be loaded from Skylogin library
-3 - Initializing Skylogin library failed
*/
int CMsnProto::MSN_SkypeAuth(const char *pszNonce, char *pszUIC)
{
int iRet = -1;
// Perform login
SkyLogin hLogin = SkyLogin_Init();
if (hLogin) {
iRet = 0;
char szPassword[100];
SkyLogin_SetLogFunction(hLogin, debugLogSkyLoginA, this);
if (!db_get_static(NULL, m_szModuleName, "Password", szPassword, sizeof(szPassword))) {
if (SkyLogin_LoadCredentials(hLogin, MyOptions.szEmail) ||
SkyLogin_PerformLogin(hLogin, MyOptions.szEmail, szPassword)) {
if (SkyLogin_CreateUICString(hLogin, pszNonce, pszUIC))
iRet = 1;
}
SkyLogin_Exit(hLogin);
}
}
return iRet;
}
/* 1 - Login successful
0 - Login failed
*/
int CMsnProto::LoginSkypeOAuth(const char *pRefreshToken)
{
int iRet = 0;
// Perform login
SkyLogin hLogin = SkyLogin_Init();
if (hLogin) {
CMStringA szLoginToken;
SkyLogin_SetLogFunction(hLogin, debugLogSkyLoginA, this);
if (RefreshOAuth(pRefreshToken, "service::login.skype.com::MBI_SSL", &szLoginToken, nullptr, nullptr) &&
SkyLogin_PerformLoginOAuth(hLogin, szLoginToken)) {
char szUIC[1024];
if (SkyLogin_GetCredentialsUIC(hLogin, szUIC)) {
char *pszPartner;
replaceStr(authUIC, szUIC);
iRet = 1;
if (pszPartner = SkyLogin_GetUser(hLogin))
setString("SkypePartner", pszPartner);
}
}
else
iRet = 0;
SkyLogin_Exit(hLogin);
}
return iRet;
}
static int CopyCookies(NETLIBHTTPREQUEST *nlhrReply, NETLIBHTTPHEADER *hdr)
{
int i, nSize = 1;
char *p;
if (hdr) {
hdr->szName = "Cookie";
*hdr->szValue = 0;
}
for (i = 0; i < nlhrReply->headersCount; i++) {
if (mir_strcmpi(nlhrReply->headers[i].szName, "Set-Cookie"))
continue;
if (p = strchr(nlhrReply->headers[i].szValue, ';')) *p = 0;
if (hdr) {
if (*hdr->szValue) mir_strcat(hdr->szValue, "; ");
mir_strcat(hdr->szValue, nlhrReply->headers[i].szValue);
}
else nSize += (int)mir_strlen(nlhrReply->headers[i].szValue) + 2;
}
return nSize;
}
/*
pszService:
service::login.skype.com::MBI_SSL - For LoginSkypeOAuth
service::ssl.live.com::MBI_SSL - For ssl-compact-ticket
service::contacts.msn.com::MBI_SSL - Contact SOAP service -> authContactToken
service::m.hotmail.com::MBI_SSL - ActiveSync contactlist, not used by us
service::storage.live.com::MBI_SSL - Storage service (authStorageToken)
service::skype.com::MBI_SSL - Root of all OAuth tokens
service::skype.net::MBI_SSL - ?
*/
bool CMsnProto::RefreshOAuth(const char *pszRefreshToken, const char *pszService, CMStringA *pszAccessToken, CMStringA *pszOutRefreshToken, time_t *ptExpires)
{
NETLIBHTTPREQUEST nlhr = { 0 };
NETLIBHTTPHEADER headers[3];
bool bRet = false;
CMStringA post;
if (!authCookies) return false;
// initialize the netlib request
nlhr.cbSize = sizeof(nlhr);
nlhr.requestType = REQUEST_POST;
nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_PERSISTENT | NLHRF_REDIRECT;
nlhr.nlc = hHttpsConnection;
nlhr.headersCount = 3;
nlhr.headers = headers;
nlhr.headers[0].szName = "User-Agent";
nlhr.headers[0].szValue = (char*)MSN_USER_AGENT;
nlhr.headers[1].szName = "Content-Type";
nlhr.headers[1].szValue = "application/x-www-form-urlencoded";
nlhr.headers[2].szName = "Cookie";
nlhr.headers[2].szValue = authCookies;
post.Format("client_id=00000000480BC46C&scope=%s&grant_type=refresh_token&refresh_token=%s", mir_urlEncode(pszService).c_str(), pszRefreshToken);
nlhr.pData = (char*)(const char*)post;
nlhr.dataLength = (int)mir_strlen(nlhr.pData);
nlhr.szUrl = "https://login.live.com/oauth20_token.srf";
// Query
mHttpsTS = clock();
NLHR_PTR nlhrReply(Netlib_HttpTransaction(m_hNetlibUser, &nlhr));
mHttpsTS = clock();
if (nlhrReply) {
hHttpsConnection = nlhrReply->nlc;
if (nlhrReply->resultCode == 200 && nlhrReply->pData) {
JSONROOT root(nlhrReply->pData);
if (root) {
bRet = true;
if (pszAccessToken) {
*pszAccessToken = (*root)["access_token"].as_mstring();
if (pszAccessToken->IsEmpty())
bRet = false;
}
if (pszOutRefreshToken) {
*pszOutRefreshToken = (*root)["refresh_token"].as_mstring();
if (pszOutRefreshToken->IsEmpty())
bRet = false;
}
if (ptExpires) {
*ptExpires = (*root)["expires_in"].as_int();
if (*ptExpires == 0)
bRet = false;
else
*ptExpires += time(0);
}
}
}
}
else hHttpsConnection = nullptr;
return bRet;
}
void CMsnProto::LoadAuthTokensDB(void)
{
DBVARIANT dbv;
authSkypeComToken.Load();
authMethod = getDword("authMethod", 0);
bPassportAuth = getBool("PassportAuth", true);
if (getString("authUser", &dbv) == 0) {
replaceStr(authUser, dbv.pszVal);
db_free(&dbv);
}
authSSLToken.Load();
authContactToken.Load();
if (getString("authUIC", &dbv) == 0) {
replaceStr(authUIC, dbv.pszVal);
db_free(&dbv);
}
if (getString("authCookies", &dbv) == 0) {
replaceStr(authCookies, dbv.pszVal);
db_free(&dbv);
}
authStrToken.Load();
if (getString("hotSecretToken", &dbv) == 0) {
replaceStr(hotSecretToken, dbv.pszVal);
db_free(&dbv);
}
if (getString("hotAuthToken", &dbv) == 0) {
free(hotAuthToken);
hotAuthToken = strdup(dbv.pszVal);
db_free(&dbv);
}
authStorageToken.Load();
if (getString("authRefreshToken", &dbv) == 0) {
replaceStr(authRefreshToken, dbv.pszVal);
db_free(&dbv);
}
authSkypeToken.Load();
}
void CMsnProto::SaveAuthTokensDB(void)
{
setDword("authMethod", authMethod);
setString("authUser", authUser);
setString("authUIC", authUIC);
setString("authCookies", authCookies);
setString("authRefreshToken", authRefreshToken);
}
typedef struct
{
/* Internal */
IEEmbed *pEmbed;
/* Input */
CMsnProto *pProto;
NETLIBHTTPREQUEST *nlhr;
NETLIBHTTPREQUEST *nlhrReply;
/* Output */
char *pszURL;
char *pszCookies;
} IEAUTH_PARAM;
LRESULT CALLBACK AuthWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_SIZE:
{
IEAUTH_PARAM *pAuth = (IEAUTH_PARAM*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (pAuth && pAuth->pEmbed) pAuth->pEmbed->ResizeBrowser();
return(0);
}
case WM_CREATE:
{
IEAUTH_PARAM *pAuth = (IEAUTH_PARAM*)((LPCREATESTRUCT)lParam)->lpCreateParams;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pAuth);
pAuth->pEmbed = new IEEmbed(hwnd);
WCHAR *pwszCookies = mir_a2u(pAuth->nlhr->headers[1].szValue);
pAuth->pEmbed->addCookie(pwszCookies);
pAuth->pEmbed->navigate(AUTH_URL);
mir_free(pwszCookies);
return(0);
}
case UM_DOCCOMPLETE:
{
if (!lParam) return 1;
IEAUTH_PARAM *pAuth = (IEAUTH_PARAM*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (wcsstr((WCHAR*)lParam, L"oauth20_authorize")) {
char *pszDoc = pAuth->pEmbed->GetHTMLDoc();
CMStringA post;
if (pszDoc) {
if (pAuth->pProto->parseLoginPage(pszDoc, pAuth->nlhr, &post)) {
pAuth->pEmbed->navigate(pAuth->nlhr);
}
mir_free(pszDoc);
}
}
else if (wcsstr((WCHAR*)lParam, L"access_token=")) {
DWORD cbCookie = 0;
pAuth->pszURL = mir_u2a((WCHAR*)lParam);
/* get_cookie doesn't give us all the necessary cookies, therefore we need to use
* InternetGetCookieExA
*/
if (!fpInternetGetCookieExA) {
HMODULE hMod = LoadLibrary(L"wininet.dll");
if (hMod) fpInternetGetCookieExA = (pfnInternetGetCookieExA)GetProcAddress(hMod, "InternetGetCookieExA");
}
if (fpInternetGetCookieExA &&
fpInternetGetCookieExA("https://login.live.com", nullptr, nullptr, &cbCookie, INTERNET_COOKIE_HTTPONLY, nullptr) &&
(pAuth->pszCookies = (char*)mir_alloc(cbCookie))) {
fpInternetGetCookieExA("https://login.live.com", nullptr, pAuth->pszCookies, &cbCookie, INTERNET_COOKIE_HTTPONLY, nullptr);
}
else pAuth->pszCookies = mir_u2a(pAuth->pEmbed->getCookies());
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
else if (wcsstr((WCHAR*)lParam, L"res=cancel") || wcsstr((WCHAR*)lParam, L"access_denied")) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return(0);
}
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case WM_DESTROY:
{
IEAUTH_PARAM *pAuth = (IEAUTH_PARAM*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (pAuth && pAuth->pEmbed) delete pAuth->pEmbed;
return(0);
}
}
return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}
void __cdecl CMsnProto::msn_IEAuthThread(void *pParam)
{
HWND hWnd;
MSG msg;
WNDCLASSEX wc = { 0 };
static const wchar_t *ClassName = L"SkypeLoginWindow";
CoInitialize(nullptr);
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = sizeof(void*);
wc.hInstance = g_plugin.getInst();
wc.lpfnWndProc = AuthWindowProc;
wc.lpszClassName = ClassName;
RegisterClassEx(&wc);
if ((hWnd = CreateWindowEx(0, ClassName, L"MSN Login", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
HWND_DESKTOP, nullptr, g_plugin.getInst(), pParam))) {
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(ClassName, g_plugin.getInst());
CoUninitialize();
}
bool CMsnProto::parseLoginPage(char *pszHTML, NETLIBHTTPREQUEST *nlhr, CMStringA *post)
{
char *pPPFT, *pEnd;
/* Get PPFT */
if ((pPPFT = strstr(pszHTML, "name=\"PPFT\"")) && (pPPFT = strstr(pPPFT, "value=\"")) && (pEnd = strchr(pPPFT + 7, '"'))) {
*pEnd = 0;
pPPFT += 7;
/* Get POST URL if available */
if ((nlhr->szUrl = strstr(pszHTML, "urlPost:'")) && (pEnd = strchr(nlhr->szUrl + 9, '\''))) {
*pEnd = 0;
nlhr->szUrl += 9;
}
else nlhr->szUrl = POST_URL;
/* Create POST data */
char szPassword[100];
if (db_get_static(NULL, m_szModuleName, "Password", szPassword, sizeof(szPassword)))
return false;
szPassword[99] = 0;
post->Format("PPFT=%s&login=%s&passwd=%s", mir_urlEncode(pPPFT).c_str(), mir_urlEncode(MyOptions.szEmail).c_str(), mir_urlEncode(szPassword).c_str());
/* Do the login and get the required tokens */
nlhr->dataLength = post->GetLength();
nlhr->pData = (char*)post->GetString();
return true;
}
return false;
}
// -1 - Refresh failed
// 0 - No need to refresh
// 1 - Refresh succeeded
int CMsnProto::MSN_RefreshOAuthTokens(bool bJustCheck)
{
time_t t;
int i = authMethod == 2 ? 0 : 1, iRet;
OAuthToken *tokens[] = { &authStrToken, &authContactToken, &authStorageToken, &authSSLToken, &authSkypeComToken };
time(&t);
if (bJustCheck) {
for (; i < sizeof(tokens) / sizeof(tokens[0]); i++)
if (tokens[i]->Expired(t)) return 1;
return 0;
}
for (iRet = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
if (tokens[i]->Expired())
iRet = tokens[i]->Refresh(true) ? 1 : -1;
}
return iRet;
}
void CMsnProto::MSN_SendATH(ThreadData *info)
{
if (MyOptions.netId != NETID_SKYPE) {
/* MSN account login */
switch (authMethod) {
case 1:
info->sendPacketPayload("ATH", "CON\\USER",
"<user><ssl-compact-ticket>t=%s</ssl-compact-ticket>"
"<uic>%s</uic>"
"<id>%s</id><alias>%s</alias></user>\r\n",
(const char*)authSSLToken ? ptrA(HtmlEncode(authSSLToken)).get() : "",
authUIC,
GetMyUsername(NETID_MSN), GetMyUsername(NETID_SKYPE));
break;
case 2:
info->sendPacketPayload("ATH", "CON\\USER",
"<user><ssl-compact-ticket>%s</ssl-compact-ticket>"
"<uic>%s</uic>"
"<ssl-site-name>chatservice.live.com</ssl-site-name>"
"</user>\r\n",
(const char*)authStrToken ? ptrA(HtmlEncode(authStrToken)).get() : "",
authUIC);
break;
}
}
else {
info->sendPacketPayload("ATH", "CON\\USER",
"<user><uic>%s</uic><id>%s</id></user>\r\n",
authUIC, MyOptions.szEmail);
}
}
// -1 - Error on login sequence
// 0 - Login failed (invalid username?)
// 1 - Login via Skype login server succeeded
// 2 - Login via Skypeweb succeeded
int CMsnProto::MSN_AuthOAuth(void)
{
int retVal = -1;
NETLIBHTTPREQUEST nlhr = { 0 };
NETLIBHTTPHEADER headers[3];
if (bAskingForAuth) return 0;
// Is there already a valid token and we can skip this?
if (!authSkypeComToken.Expired() && !mir_strcmp(authUser, MyOptions.szEmail)) {
MSN_RefreshOAuthTokens(false);
return authMethod;
}
// initialize the netlib request
nlhr.cbSize = sizeof(nlhr);
nlhr.requestType = REQUEST_GET;
nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_PERSISTENT | NLHRF_REDIRECT;
nlhr.nlc = hHttpsConnection;
nlhr.headersCount = 1;
nlhr.headers = headers;
nlhr.headers[0].szName = "User-Agent";
nlhr.headers[0].szValue = (char*)MSN_USER_AGENT;
// Get oauth20 login data
nlhr.szUrl = AUTH_URL;
mHttpsTS = clock();
NETLIBHTTPREQUEST *nlhrReply = Netlib_HttpTransaction(m_hNetlibUser, &nlhr);
mHttpsTS = clock();
if (nlhrReply) {
hHttpsConnection = nlhrReply->nlc;
if (nlhrReply->resultCode == 200 && nlhrReply->pData) {
CMStringA post;
/* Get POST-Data and URL */
if (parseLoginPage(nlhrReply->pData, &nlhr, &post)) {
/* Get Cookies */
nlhr.headers[1].szValue = (char*)alloca(CopyCookies(nlhrReply, nullptr));
CopyCookies(nlhrReply, &nlhr.headers[1]);
if (*nlhr.headers[1].szValue) nlhr.headersCount++;
/* Setup headers */
nlhr.headers[nlhr.headersCount].szName = "Content-Type";
nlhr.headers[nlhr.headersCount++].szValue = "application/x-www-form-urlencoded";
/* Do the login and get the required tokens */
nlhr.requestType = REQUEST_POST;
nlhr.flags &= (~NLHRF_REDIRECT);
mHttpsTS = clock();
nlhr.nlc = hHttpsConnection;
NETLIBHTTPREQUEST *nlhrReply2 = Netlib_HttpTransaction(m_hNetlibUser, &nlhr);
mHttpsTS = clock();
if (nlhrReply2) {
char *pszURL = nullptr, *pAccessToken, *pEnd;
hHttpsConnection = nlhrReply2->nlc;
bPassportAuth = true;
if (nlhrReply2->resultCode == 302) {
/* Extract access_token from Location can be found */
for (int i = 0; i < nlhrReply2->headersCount; i++) {
if (!mir_strcmpi(nlhrReply2->headers[i].szName, "Location")) {
pszURL = nlhrReply2->headers[i].szValue;
break;
}
}
}
else {
/* There may be a problem with login, i.e. M$ security measures. Open up browser
* window in order to let user login there. May also be used for 2-factor auth */
if (nlhrReply2->resultCode == 200 && nlhrReply2->pData) {
UINT uThreadId;
IEAUTH_PARAM param = { nullptr, this, &nlhr, nlhrReply2, nullptr, nullptr };
bAskingForAuth = true;
WaitForSingleObject(ForkThreadEx(&CMsnProto::msn_IEAuthThread, ¶m, &uThreadId), INFINITE);
pszURL = param.pszURL;
mir_free(authCookies);
authCookies = nlhr.headers[1].szValue = param.pszCookies;
Netlib_FreeHttpRequest(nlhrReply2);
nlhrReply2 = nullptr;
bAskingForAuth = false;
bPassportAuth = false;
}
}
setByte("PassportAuth", bPassportAuth);
Netlib_FreeHttpRequest(nlhrReply);
nlhrReply = nlhrReply2;
if (pszURL &&
(pAccessToken = strstr(pszURL, "access_token=")) &&
(pEnd = strchr(pAccessToken + 13, '&'))) {
char *pRefreshToken, *pExpires;
bool bLogin = false;
pRefreshToken = strstr(pszURL, "refresh_token=");
pExpires = strstr(pszURL, "expires_in=");
*pEnd = 0;
pAccessToken += 13;
/* Extract refresh token */
if (pRefreshToken && (pEnd = strchr(pRefreshToken + 14, '&'))) {
*pEnd = 0;
pRefreshToken += 14;
}
replaceStr(authRefreshToken, pRefreshToken);
/* Extract expire time */
time_t authTokenExpiretime;
time(&authTokenExpiretime);
if (pExpires && (pEnd = strchr(pExpires + 11, '&'))) {
*pEnd = 0;
pExpires += 11;
authTokenExpiretime += atoi(pExpires);
}
else authTokenExpiretime += 86400;
authSkypeComToken.SetToken(mir_urlDecode(pAccessToken), authTokenExpiretime);
/* Copy auth Cookies to class for other web requests like contact list fetching to avoid ActiveSync */
if (nlhrReply) {
mir_free(authCookies);
authCookies = nlhr.headers[1].szValue = (char*)mir_alloc(CopyCookies(nlhrReply, nullptr));
CopyCookies(nlhrReply, &nlhr.headers[1]);
}
int loginRet;
/* Do Login via Skype login server, if not possible switch to SkypeWebExperience login */
if ((loginRet = LoginSkypeOAuth(pRefreshToken)) < 1) {
retVal = 0;
}
else {
/* SkyLogin succeeded, request required tokens */
if (authSSLToken.Refresh(true)) {
replaceStr(authUser, MyOptions.szEmail);
authMethod = retVal = 1;
}
}
authSkypeToken.Clear();
/* If you need Skypewebexperience login, as i.e. skylogin.dll is not available, we do this here */
if (bLogin) {
/* Prepare headers*/
nlhr.headers[2].szValue = "application/json";
nlhr.pData = "{\"trouterurl\":\"https://\",\"connectionid\":\"a\"}";
nlhr.dataLength = (int)mir_strlen(nlhr.pData);
nlhr.szUrl = "https://skypewebexperience.live.com/v1/User/Initialization";
nlhr.nlc = hHttpsConnection;
/* Request MappingContainer */
mHttpsTS = clock();
if (nlhrReply)
Netlib_FreeHttpRequest(nlhrReply);
nlhrReply = Netlib_HttpTransaction(m_hNetlibUser, &nlhr);
mHttpsTS = clock();
if (nlhrReply) {
hHttpsConnection = nlhrReply->nlc;
if (nlhrReply->resultCode == 200 && nlhrReply->pData) {
/* Parse JSON stuff for MappingContainer */
char *pMappingContainer;
if ((pMappingContainer = strstr(nlhrReply->pData, "\"MappingContainer\":\"")) &&
(pEnd = strchr(pMappingContainer + 20, '"'))) {
*pEnd = 0;
pMappingContainer += 20;
replaceStr(authUIC, mir_urlDecode(pMappingContainer));
replaceStr(authUser, MyOptions.szEmail);
authMethod = retVal = 2;
}
else retVal = 0;
}
else retVal = 0;
}
else hHttpsConnection = nullptr;
}
}
}
else hHttpsConnection = nullptr;
}
}
if (nlhrReply)
Netlib_FreeHttpRequest(nlhrReply);
}
else hHttpsConnection = nullptr;
if (retVal <= 0) authSkypeComToken.Clear(); else {
if (bPassportAuth) {
// Fast authentication with just 1 SOAP call supported :)
MSN_GetPassportAuth();
}
else {
// Slow authentication required by fetching multiple tokens, i.e. 2-factor auth :(
if (authMethod == 2) authStrToken.Refresh();
authContactToken.Refresh();
authStorageToken.Refresh();
}
SaveAuthTokensDB();
}
return retVal;
}
int CMsnProto::GetMyNetID(void)
{
return strchr(MyOptions.szEmail, '@') ? NETID_MSN : NETID_SKYPE;
}
const char *CMsnProto::GetMyUsername(int netId)
{
static char szPartner[128];
if (netId == NETID_SKYPE) {
if (MyOptions.netId == NETID_MSN) {
if (db_get_static(NULL, m_szModuleName, "SkypePartner", szPartner, sizeof(szPartner)) == 0)
return szPartner;
}
}
return MyOptions.szEmail;
}
void CMsnProto::FreeAuthTokens(void)
{
mir_free(pAuthToken);
mir_free(tAuthToken);
mir_free(oimSendToken);
mir_free(authSecretToken);
mir_free(hotSecretToken);
mir_free(authUIC);
mir_free(authCookies);
mir_free(authUser);
mir_free(authRefreshToken);
free(hotAuthToken);
}
|