1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
liveid.php
См. документацию.
1<?php
2
3/* TODO: Comments are out of date and incomplete. */
4
17
22{
26 public function __construct($timestamp, $id, $flags, $context, $token)
27 {
28 $this->setTimestamp($timestamp);
29 $this->setId($id);
30 $this->setFlags($flags);
31 $this->setContext($context);
32 $this->setToken($token);
33 }
34
35 /*private*/
37
41 /*public*/
42 function getTimestamp()
43 {
44 return $this->_timestamp;
45 }
46
50 /*private*/
51 function setTimestamp($timestamp)
52 {
53 if (!$timestamp) {
54 //throw new Exception('Error: WLL_User: Null timestamp.');
55 $this->setError('Error: WLL_User: Null timestamp.');
56 return ;
57 }
58
59 if (!preg_match('/^\d+$/', $timestamp) || ($timestamp <= 0)) {
60 //throw new Exception('Error: WLL_User: Invalid timestamp: ' . $timestamp);
61 $this->setError('Error: WLL_User: Invalid timestamp: ' . $timestamp);
62 return ;
63 }
64
65 $this->_timestamp = $timestamp;
66 }
67
68 /*private*/
69 var $_id;
70
74 /*public*/
75 function getId()
76 {
77 return $this->_id;
78 }
79
83 /*private*/
84 function setId($id)
85 {
86 if (!$id) {
87 //throw new Exception('Error: WLL_User: Null id.');
88 $this->setError('Error: WLL_User: Null id.');
89 return ;
90 }
91
92 if (!preg_match('/^\w+$/', $id)) {
93 //throw new Exception('Error: WLL_User: Invalid id: ' . $id);
94 $this->setError('Error: WLL_User: Invalid id: ' . $id);
95 return ;
96 }
97
98 $this->_id = $id;
99 }
100
101 /*private*/
103
108 /*public*/
110 {
112 }
113
117 /*private*/
118 function setFlags($flags)
119 {
120 $this->_usePersistentCookie = false;
121 if (preg_match('/^\d+$/', $flags)) {
122 $this->_usePersistentCookie = (($flags % 2) == 1);
123 }
124 }
125
126 /*private*/
128
133 /*public*/
134 function getContext()
135 {
136 return $this->_context;
137 }
138
142 /*private*/
144 {
145 $this->_context = $context;
146 }
147
148 /*private*/
150
156 /*public*/
157 function getToken()
158 {
159 return $this->_token;
160 }
161
165 /*private*/
166 function setToken($token)
167 {
168 $this->_token = $token;
169 }
170
171
172 var $_error = false;
173
174 function setError($str)
175 {
176 $this->_error = $str;
177 }
178
179 function getError()
180 {
181 if ($this->_error !== false)
182 {
183 return $this->_error;
184 }
185 }
186}
187
192{
196 /*public*/
197 function isValid()
198 {
199 if (!$this->getDelegationToken()) {
200 return false;
201 }
202
203 $now = time();
204 return (($now-300) < $this->getExpiry());
205 }
206
211 /*public*/
212 function refresh()
213 {
214 $wll = $this->_wll;
215 $ct = $wll->refreshConsentToken($this);
216 if (!$ct) {
217 return false;
218 }
219 $this->copy($ct);
220 return true;
221 }
222
223 /*private*/
224 var $_wll;
225
231 public function __construct(
232 $wll, $delegationtoken, $refreshtoken,
233 $sessionkey, $expiry, $offers, $locationID, $context,
234 $decodedtoken, $token
235 )
236 {
237 $this->_wll = $wll;
238 $this->setDelegationToken($delegationtoken);
239 $this->setRefreshToken($refreshtoken);
240 $this->setSessionKey($sessionkey);
241 $this->setExpiry($expiry);
242 $this->setOffers($offers);
243 $this->setLocationID($locationID);
244 $this->setContext($context);
245 $this->setDecodedToken($decodedtoken);
246 $this->setToken($token);
247 }
248
249 /*private*/
251
255 /*public*/
257 {
259 }
260
264 /*private*/
265 function setDelegationToken($delegationtoken)
266 {
267 if (!$delegationtoken) {
268 //throw new Exception('Error: WLL_ConsentToken: Null delegation token.');
269 $this->setError('Error: WLL_ConsentToken: Null delegation token.');
270 return ;
271 }
272 $this->_delegationtoken = $delegationtoken;
273 }
274
275 /*private*/
277
281 /*public*/
283 {
285 }
286
290 /*private*/
291 function setRefreshToken($refreshtoken)
292 {
293 $this->_refreshtoken = $refreshtoken;
294 }
295
296 /*private*/
298
302 /*public*/
303 function getSessionKey()
304 {
305 return $this->_sessionkey;
306 }
307
311 /*private*/
312 function setSessionKey($sessionkey)
313 {
314 if (!$sessionkey) {
315 //throw new Exception('Error: WLL_ConsentToken: Null session key.');
316 $this->setError('Error: WLL_ConsentToken: Null session key.');
317 return ;
318 }
319 $this->_sessionkey = base64_decode(urldecode($sessionkey));
320 }
321
322 /*private*/
324
328 /*public*/
329 function getExpiry()
330 {
331 return $this->_expiry;
332 }
333
337 /*private*/
338 function setExpiry($expiry)
339 {
340 if (!$expiry) {
341 //throw new Exception('Error: WLL_ConsentToken: Null expiry time.');
342 $this->setError('Error: WLL_ConsentToken: Null expiry time.');
343 return ;
344 }
345
346 if (!preg_match('/^\d+$/', $expiry) || ($expiry <= 0)) {
347 //throw new Exception('Error: WLL_ConsentToken: Invalid expiry time: ' . $expiry);
348 $this->setError('Error: WLL_ConsentToken: Invalid expiry time: ' . $expiry);
349 return ;
350 }
351 $this->_expiry = $expiry;
352 }
353
354 /*private*/
356
360 /*public*/
361 function getOffers()
362 {
363 return $this->_offers;
364 }
365
366 /*private*/
368
373 /*public*/
375 {
377 }
378
382 /*private*/
383 function setOffers($offers)
384 {
385 if (!$offers) {
386 //throw new Exception('Error: WLL_ConsentToken: Null offers.');
387 $this->setError('Error: WLL_ConsentToken: Null offers.');
388 return ;
389 }
390
391 $offers = urldecode($offers);
392
393 //Split $offers by ";" and then take only substring before first ":"
394 if(preg_match_all("/(^|;)([^:;]*)/", $offers, $arMatch))
395 {
396 $this->_offers = $arMatch[2];
397 $this->_offers_string = ltrim(implode(",", $arMatch[2]), ",");
398 }
399 else
400 {
401 $this->_offers = array();
402 $this->_offers_string = "";
403 }
404 }
405
406 /*private*/
411 /*public*/
412 function getLocationID()
413 {
414 return $this->_locationID;
415 }
416
420 /*private*/
421 function setLocationID($locationID)
422 {
423 if (!$locationID) {
424 //throw new Exception('Error: WLL_ConsentToken: Null Location ID.');
425 $this->setError('Error: WLL_ConsentToken: Null Location ID.');
426 return ;
427 }
428 $this->_locationID = $locationID;
429 }
430
431 /*private*/
437 /*public*/
438 function getContext()
439 {
440 return $this->_context;
441 }
442
446 /*private*/
448 {
449 $this->_context = $context;
450 }
451
452 /*private*/
457 /*public*/
459 {
461 }
462
466 /*private*/
467 function setDecodedToken($decodedtoken)
468 {
469 $this->_decodedtoken = $decodedtoken;
470 }
471
472 /*private*/
474
478 /*public*/
479 function getToken()
480 {
481 return $this->_token;
482 }
483
487 /*private*/
488 function setToken($token)
489 {
490 $this->_token = $token;
491 }
492
496 /*private*/
497 function copy($ct)
498 {
499 $this->_delegationtoken = $ct->_delegationtoken;
500 $this->_refreshtoken = $ct->_refreshtoken;
501 $this->_sessionkey = $ct->_sessionkey;
502 $this->_expiry = $ct->_expiry;
503 $this->_offers = $ct->_offers;
504 $this->_offers_string = $ct->_offers_string;
505 $this->_locationID = $ct->_locationID;
506 $this->_decodedtoken = $ct->_decodedtoken;
507 $this->_token = $ct->_token;
508 }
509
510 var $_error = false;
511
512 function setError($str)
513 {
514 $this->_error = $str;
515 }
516
517 function getError()
518 {
519 if ($this->_error !== false)
520 {
521 return $this->_error;
522 }
523 }
524}
525
527{
528 /* Implementation of basic methods for Web Authentication support. */
529
530 /*private*/
531 var $_debug = false;
532
538 /*public*/
539 function setDebug($debug)
540 {
541 $this->_debug = $debug;
542 }
543
550 /*private*/
551 function debug($string)
552 {
553 if ($this->_debug) {
554 echo "$string<br>";
555 error_log($string);
556 }
557 }
558
562 /*private*/
563 function fatal($string)
564 {
565 $this->debug($string);
566 //throw new Exception($string);
567 $this->setError($string);
568 }
569
593 public function __construct(
594 $appid=null, $secret=null, $securityalgorithm=null,
595 $force_delauth_nonprovisioned=null,
596 $policyurl=null, $returnurl=null
597 )
598 {
599 $this->setForceDelAuthNonProvisioned($force_delauth_nonprovisioned);
600
601 if ($appid) {
602 $this->setAppId($appid);
603 }
604 if ($secret) {
605 $this->setSecret($secret);
606 }
607 if ($securityalgorithm) {
608 $this->setSecurityAlgorithm($securityalgorithm);
609 }
610 if ($policyurl) {
611 $this->setPolicyUrl($policyurl);
612 }
613 if ($returnurl) {
614 $this->setReturnUrl($returnurl);
615 }
616 }
617
639 /*public static*/
640 function initFromXml($settingsFile)
641 {
642 $o = new WindowsLiveLogin();
643 $settings = $o->parseSettings($settingsFile);
644
645 if (@$settings['debug'] == 'true') {
646 $o->setDebug(true);
647 }
648 else {
649 $o->setDebug(false);
650 }
651
652 if (@$settings['force_delauth_nonprovisioned'] == 'true') {
653 $o->setForceDelAuthNonProvisioned(true);
654 }
655 else {
656 $o->setForceDelAuthNonProvisioned(false);
657 }
658
659 $o->setAppId(@$settings['appid']);
660 $o->setSecret(@$settings['secret']);
661 $o->setOldSecret(@$settings['oldsecret']);
662 $o->setOldSecretExpiry(@$settings['oldsecretexpiry']);
663 $o->setSecurityAlgorithm(@$settings['securityalgorithm']);
664 $o->setPolicyUrl(@$settings['policyurl']);
665 $o->setReturnUrl(@$settings['returnurl']);
666 $o->setBaseUrl(@$settings['baseurl']);
667 $o->setSecureUrl(@$settings['secureurl']);
668 $o->setConsentBaseUrl(@$settings['consenturl']);
669 return $o;
670 }
671
672 /*private*/
674
679 /*public*/
680 function setAppId($appid)
681 {
683 if (!$appid) {
685 return;
686 }
687 $this->fatal('Error: setAppId: Null application ID.');
688 }
689 if (!preg_match('/^\w+$/', $appid)) {
690 $this->fatal("Error: setAppId: Application ID must be alpha-numeric: $appid");
691 }
692 $this->_appid = $appid;
693 }
694
698 /*public*/
699 function getAppId()
700 {
701 if (!$this->_appid) {
702 $this->fatal('Error: getAppId: Application ID was not set. Aborting.');
703 }
704 return $this->_appid;
705 }
706
707 /*private*/
709 /*private*/
711
716 /*public*/
717 function setSecret($secret)
718 {
720 if (!$secret || (strlen($secret) < 16)) {
722 return;
723 }
724 $this->fatal("Error: setSecret: Secret key is expected to be non-null and longer than 16 characters.");
725 }
726
727 $this->_signkey = $this->derive($secret, "SIGNATURE");
728 $this->_cryptkey = $this->derive($secret, "ENCRYPTION");
729 }
730
731 /*private*/
733 /*private*/
735
750 /*public*/
751 function setOldSecret($secret)
752 {
753 if (!$secret) {
754 return;
755 }
756 if (strlen($secret) < 16) {
757 $this->fatal("Error: setOldSecret: Secret key is expected to be non-null and longer than 16 characters.");
758 }
759
760 $this->_oldsignkey = $this->derive($secret, "SIGNATURE");
761 $this->_oldcryptkey = $this->derive($secret, "ENCRYPTION");
762 }
763
764 /*private*/
766
776 /*public*/
777 function setOldSecretExpiry($timestamp)
778 {
779 if (!$timestamp) {
780 return;
781 }
782
783 if (!preg_match('/^\d+$/', $timestamp) || ($timestamp <= 0)) {
784 $this->fatal('Error: setOldSecretExpiry Invalid timestamp: '
785 . $timestamp);
786 }
787
788 $this->_oldsecretexpiry = $timestamp;
789 }
790
794 /*public*/
796 {
798 }
799
800 /*private*/
802
806 /*public*/
807 function setSecurityAlgorithm($securityalgorithm)
808 {
809 $this->_securityalgorithm = $securityalgorithm;
810 }
811
815 /*public*/
817 {
818 $securityalgorithm = $this->_securityalgorithm;
819 if (!$securityalgorithm) {
820 return 'wsignin1.0';
821 }
822 return $securityalgorithm;
823 }
824
825 /*private*/
827
833 /*public*/
834 function setForceDelAuthNonProvisioned($force_delauth_nonprovisioned)
835 {
836 $this->_force_delauth_nonprovisioned = $force_delauth_nonprovisioned;
837 }
838
839 /*private*/
841
845 /*public*/
846 function setPolicyUrl($policyurl)
847 {
849 if (!$policyurl) {
851 $this->fatal("Error: setPolicyUrl: Null policy URL given.");
852 }
853 }
854 $this->_policyurl = $policyurl;
855 }
856
860 /*public*/
861 function getPolicyUrl()
862 {
863 $policyurl = $this->_policyurl;
865 if (!$policyurl) {
866 $this->debug("Warning: In the initial release of Delegated Auth, a Policy URL must be configured in the SDK for both provisioned and non-provisioned scenarios.");
867 if ($_force_delauth_nonprovisioned) {
868 $this->fatal("Error: getPolicyUrl: Policy URL must be set in a Del Auth non-provisioned scenario. Aborting.");
869 }
870 }
871 return $policyurl;
872 }
873
874 /*private*/
876
884 /*public*/
885 function setReturnUrl($returnurl)
886 {
888 if (!$returnurl) {
890 $this->fatal("Error: setReturnUrl: Null return URL given.");
891 }
892 }
893 $this->_returnurl = $returnurl;
894 }
895
899 /*public*/
900 function getReturnUrl()
901 {
903 $returnurl = $this->_returnurl;
904 if (!$returnurl) {
906 $this->fatal("Error: getReturnUrl: Return URL must be set in a Del Auth non-provisioned scenario. Aborting.");
907 }
908 }
909 return $returnurl;
910 }
911
912 /*private*/
914
921 /*public*/
922 function setBaseUrl($baseurl)
923 {
924 $this->_baseurl = $baseurl;
925 }
926
933 /*public*/
934 function getBaseUrl()
935 {
936 $baseurl = $this->_baseurl;
937 if (!$baseurl) {
938 return "http://login.live.com/";
939 }
940 return $baseurl;
941 }
942
943 /*private*/
945
950 /*public*/
951 function setSecureUrl($secureurl)
952 {
953 $this->_secureurl = $secureurl;
954 }
955
960 /*public*/
961 function getSecureUrl()
962 {
963 $secureurl = $this->_secureurl;
964 if (!$secureurl) {
965 return "https://login.live.com/";
966 }
967 return $secureurl;
968 }
969
970 /*private*/
972
977 /*public*/
978 function setConsentBaseUrl($consenturl)
979 {
980 $this->_consenturl = $consenturl;
981 }
982
987 /*public*/
989 {
990 $consenturl = $this->_consenturl;
991 if (!$consenturl) {
992 return "https://consent.live.com/";
993 }
994 return $consenturl;
995 }
996
997 /* Methods for Web Authentication support. */
998
1006 /*public*/
1007 function getLoginUrl($context=null, $market=null)
1008 {
1009 $url = $this->getBaseUrl();
1010 $url .= 'wlogin.srf?appid=' . $this->getAppId();
1011 $url .= '&alg=' . $this->getSecurityAlgorithm();
1012 $url .= ($context ? '&appctx=' . urlencode($context) : '');
1013 $url .= ($market ? '&mkt=' . urlencode($market) : '');
1014 return $url;
1015 }
1016
1021 /*public*/
1022 function getLogoutUrl($market=null)
1023 {
1024 $url = $this->getBaseUrl();
1025 $url .= "logout.srf?appid=" . $this->getAppId();
1026 $url .= ($market ? '&mkt=' . urlencode($market) : '');
1027 return $url;
1028 }
1029
1038 /*public*/
1040 {
1041 $action = @$query['action'];
1042 if ($action != 'login') {
1043 $this->debug("Warning: processLogin: query action ignored: $action");
1044 return;
1045 }
1046 $token = @$query['stoken'];
1047 $context = urldecode(@$query['appctx']);
1048 return $this->processToken($token, $context);
1049 }
1050
1056 /*public*/
1057 function processToken($token, $context=null)
1058 {
1059 if (!$token) {
1060 $this->debug('Error: processToken: Invalid token specified.');
1061 return;
1062 }
1063
1064 $decodedToken = $this->decodeAndValidateToken($token);
1065 if (!$decodedToken) {
1066 $this->debug("Error: processToken: Failed to decode/validate token: $token");
1067 return;
1068 }
1069
1070 $parsedToken = $this->parse($decodedToken);
1071 if (!$parsedToken) {
1072 $this->debug("Error: processToken: Failed to parse token after decoding: $token");
1073 return;
1074 }
1075
1076 $appid = $this->getAppId();
1077 $tokenappid = @$parsedToken['appid'];
1078 if ($appid != $tokenappid) {
1079 $this->debug("Error: processToken: Application ID in token did not match ours: $tokenappid, $appid");
1080 return;
1081 }
1082
1083 $user = null;
1084
1085 //try {
1086 $user = new WLL_User(@$parsedToken['ts'],
1087 @$parsedToken['uid'],
1088 @$parsedToken['flags'],
1089 $context, $token);
1090 //} catch (Exception $e) {
1091 if ($user->getError() !== false)
1092 $this->debug("Error: processToken: Contents of token considered invalid: " + $user->getError());
1093 //}
1094
1095 return $user;
1096 }
1097
1113 /*public*/
1115 {
1116 $type = "image/gif";
1117 $content = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7";
1118 $content = base64_decode($content);
1119 return array($type, $content);
1120 }
1121
1122 /* Methods for Delegated Authentication. */
1123
1124 /*
1125 * Returns the consent URL to use for Delegated Authentication for
1126 * the given comma-delimited list of offers.
1127 *
1128 * If you specify it, 'context' will be returned as-is in the consent
1129 * response for site-specific use.
1130 *
1131 * The registered/configured return URL can also be overridden by
1132 * specifying 'ru' here.
1133 *
1134 * You can change the language in which the consent page is displayed
1135 * by specifying a culture ID (For example, 'fr-fr' or 'en-us') in the
1136 * 'market' parameter.
1137 */
1138 /*public*/
1139 function getConsentUrl($offers, $context=null, $ru=null, $market=null)
1140 {
1141 if (!$offers) {
1142 //throw new Exception('Error: getConsentUrl: Invalid offers list.');
1143 $this->setError('Error: getConsentUrl: Invalid offers list.');
1144 return false;
1145 }
1146 $url = $this->getConsentBaseUrl();
1147 $url .= 'Delegation.aspx?ps=' . urlencode($offers);
1148 $ru = ($ru ? $ru : $this->getReturnUrl());
1149 $url .= ($ru ? '&ru=' . urlencode($ru) : '');
1150 $pl = $this->getPolicyUrl();
1151 $url .= ($pl ? '&pl=' . urlencode($pl) : '');
1152 $url .= ($market ? '&mkt=' . urlencode($market) : '');
1153 if (!$this->_force_delauth_nonprovisioned) {
1154 $url .= '&app=' . $this->getAppVerifier();
1155 }
1156 $url .= ($context ? '&appctx=' . urlencode($context) : '');
1157 return $url;
1158 }
1159
1160 /*
1161 * Returns the URL to use to download a new consent token, given the
1162 * offers and refresh token.
1163 *
1164 * The registered/configured return URL can also be overridden by
1165 * specifying 'ru' here.
1166 */
1167 /*public*/
1168 function getRefreshConsentTokenUrl($offers, $refreshtoken, $ru=null)
1169 {
1171 if (!$offers) {
1172 //throw new Exception('Error: getRefreshConsentTokenUrl: Invalid offers list.');
1173 $this->setError('Error: getRefreshConsentTokenUrl: Invalid offers list.');
1174 return false;
1175 }
1176 if (!$refreshtoken) {
1177 //throw new Exception('Error: getRefreshConsentTokenUrl: Invalid refresh token.');
1178 $this->setError('Error: getRefreshConsentTokenUrl: Invalid refresh token.');
1179 return false;
1180 }
1181
1182 $url = $this->getConsentBaseUrl();
1183 $url .= 'RefreshToken.aspx?ps=' . urlencode($offers);
1184 $url .= '&reft=' . $refreshtoken;
1185 $ru = ($ru ? $ru : $this->getReturnUrl());
1186 $url .= ($ru ? '&ru=' . urlencode($ru) : '');
1187
1188 if (!$this->_force_delauth_nonprovisioned) {
1189 $url .= '&app=' . $this->getAppVerifier();
1190 }
1191
1192 return $url;
1193 }
1194
1195 /*
1196 * Returns the URL for the consent-management user interface.
1197 *
1198 * You can change the language in which the consent page is displayed
1199 * by specifying a culture ID (For example, 'fr-fr' or 'en-us') in the
1200 * 'market' parameter.
1201 */
1202 /*public*/
1203 function getManageConsentUrl($market=null)
1204 {
1205 $url = $this->getConsentBaseUrl();
1206 $url .= 'ManageConsent.aspx';
1207 $url .= ($market ? '?mkt=' . urlencode($market) : '');
1208 return $url;
1209 }
1210
1211 /*
1212 * Processes the POST response from the Delegated Authentication
1213 * service after a user has granted consent. The processConsent
1214 * function extracts the consent token string and returns the result
1215 * of invoking the processConsentToken method.
1216 */
1217 /*public*/
1219 {
1220 $action = @$query['action'];
1221 if ($action != 'delauth') {
1222 $this->debug("Warning: processConsent: query action ignored: $action");
1223 return;
1224 }
1225 $responsecode = @$query['ResponseCode'];
1226 if ($responsecode != 'RequestApproved') {
1227 $this->debug("Warning: processConsent: consent was not successfully granted: $responsecode");
1228 return;
1229 }
1230 $token = @$query['ConsentToken'];
1231 $context = urldecode(@$query['appctx']);
1232 return $this->processConsentToken($token, $context);
1233 }
1234
1235 /*
1236 * Processes the consent token string that is returned in the POST
1237 * response by the Delegated Authentication service after a
1238 * user has granted consent.
1239 */
1240 /*public*/
1241 function processConsentToken($token, $context=null)
1242 {
1243 if (!$token) {
1244 $this->debug('Error: processConsentToken: Null token.');
1245 return;
1246 }
1247
1248 $decodedToken = $token;
1249 $parsedToken = $this->parse(urldecode($decodedToken));
1250 if (!$parsedToken) {
1251 $this->debug("Error: processConsentToken: Failed to parse token: $token");
1252 return;
1253 }
1254
1255 $eact = @$parsedToken['eact'];
1256 if ($eact) {
1257 $decodedToken = $this->decodeAndValidateToken($eact);
1258 if (!$decodedToken) {
1259 $this->debug("Error: processConsentToken: Failed to decode/validate token: $token");
1260 return;
1261 }
1262 $parsedToken = $this->parse($decodedToken);
1263 if (!$parsedToken) {
1264 $this->debug("Error: processConsentToken: Failed to parse token after decoding: $token");
1265 return;
1266 }
1267 $decodedToken = urlencode($decodedToken);
1268 }
1269
1270 $consenttoken = null;
1271
1272 //try {
1273 $consenttoken = new WLL_ConsentToken($this,
1274 @$parsedToken['delt'],
1275 @$parsedToken['reft'],
1276 @$parsedToken['skey'],
1277 @$parsedToken['exp'],
1278 @$parsedToken['offer'],
1279 @$parsedToken['lid'],
1280 $context, $decodedToken, $token);
1281 //} catch (Exception $e) {
1282 if($consenttoken->getError() !== false)
1283 $this->debug("Error: processConsentToken: Contents of token considered invalid: " + $consenttoken->getError());
1284 //}
1285 return $consenttoken;
1286 }
1287
1288 /*
1289 * Attempts to obtain a new, refreshed token and return it. The
1290 * original token is not modified.
1291 */
1292 /*public*/
1293 function refreshConsentToken($token, $ru=null)
1294 {
1295 if (!$token) {
1296 $this->debug("Error: refreshConsentToken: Null consent token.");
1297 return;
1298 }
1299 $this->refreshConsentToken2($token->getOffersString(), $token->getRefreshToken(), $ru);
1300 }
1301
1302 /*
1303 * Helper function to obtain a new, refreshed token and return it.
1304 * The original token is not modified.
1305 */
1306 /*public*/
1307 function refreshConsentToken2($offers_string, $refreshtoken, $ru=null)
1308 {
1309 $body = $this->fetch($this->getRefreshConsentTokenUrl($offers_string, $refreshtoken, $ru));
1310 if (!$body) {
1311 $this->debug("Error: refreshConsentToken2: Failed to obtain a new token.");
1312 return;
1313 }
1314
1315 preg_match('/\{"ConsentToken":"(.*)"\}/', $body, $matches);
1316 if(count($matches) == 2) {
1317 return $matches[1];
1318 }
1319 else {
1320 $this->debug("Error: refreshConsentToken2: Failed to extract token: $body");
1321 return;
1322 }
1323 }
1324
1325 /* Common methods. */
1326
1327 /*
1328 * Decodes and validates the token.
1329 */
1330 /*public*/
1331 function decodeAndValidateToken($token, $cryptkey=null, $signkey=null,
1332 $internal_allow_recursion=true)
1333 {
1334 if (!$cryptkey) {
1335 $cryptkey = $this->_cryptkey;
1336 }
1337 if (!$signkey) {
1338 $signkey = $this->_signkey;
1339 }
1340
1341 $haveoldsecret = false;
1342 $oldsecretexpiry = $this->getOldSecretExpiry();
1343 $oldcryptkey = $this->_oldcryptkey;
1344 $oldsignkey = $this->_oldsignkey;
1345
1346 if ($oldsecretexpiry and (time() < $oldsecretexpiry)) {
1347 if ($oldcryptkey and $oldsignkey) {
1348 $haveoldsecret = true;
1349 }
1350 }
1351 $haveoldsecret = ($haveoldsecret and $internal_allow_recursion);
1352
1353 $stoken = $this->decodeToken($token, $cryptkey);
1354
1355 if ($stoken) {
1356 $stoken = $this->validateToken($stoken, $signkey);
1357 }
1358
1359 if (!$stoken and $haveoldsecret) {
1360 $this->debug("Warning: Failed to validate token with current secret, attempting old secret.");
1361 $stoken =
1362 $this->decodeAndValidateToken($token, $oldcryptkey, $oldsignkey, false);
1363 }
1364
1365 return $stoken;
1366 }
1367
1375 /*public*/
1376 function decodeToken($token, $cryptkey=null)
1377 {
1378 if (!$cryptkey) {
1379 $cryptkey = $this->_cryptkey;
1380 }
1381 if (!$cryptkey) {
1382 $this->fatal("Error: decodeToken: Secret key was not set. Aborting.");
1383 }
1384
1385 $ivLen = 16;
1386 $token = $this->u64($token);
1387 $len = strlen($token);
1388
1389 if (!$token || ($len <= $ivLen) || (($len % $ivLen) != 0)) {
1390 $this->debug("Error: decodeToken: Attempted to decode invalid token.");
1391 return;
1392 }
1393
1394 $iv = substr($token, 0, 16);
1395 $crypted = substr($token, 16);
1396
1397 return openssl_decrypt($crypted, "AES-128-CBC", $cryptkey, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
1398 }
1399
1404 /*public*/
1405 function signToken($token, $signkey=null)
1406 {
1407 if (!$signkey) {
1408 $signkey = $this->_signkey;
1409 }
1410 if (!$signkey) {
1411 $this->fatal("Error: signToken: Secret key was not set. Aborting.");
1412 }
1413
1414 if (!$token) {
1415 $this->debug("Attempted to sign null token.");
1416 return;
1417 }
1418
1419 return hash_hmac("sha256", $token, $signkey, true);
1420 }
1421
1425 /*public*/
1426 function validateToken($token, $signkey=null)
1427 {
1428 if (!$signkey) {
1429 $signkey = $this->_signkey;
1430 }
1431 if (!$token) {
1432 $this->debug("Error: validateToken: Invalid token.");
1433 return;
1434 }
1435
1436 $split = explode("&sig=", $token);
1437 if (count($split) != 2) {
1438 $this->debug("ERROR: validateToken: Invalid token: $token");
1439 return;
1440 }
1441 list($body, $sig) = $split;
1442
1443 $sig = $this->u64($sig);
1444 if (!$sig) {
1445 $this->debug("Error: validateToken: Could not extract signature from token.");
1446 return;
1447 }
1448
1449 $sig2 = $this->signToken($body, $signkey);
1450 if (!$sig2) {
1451 $this->debug("Error: validateToken: Could not generate signature for the token.");
1452 return;
1453 }
1454
1455
1456 if ($sig == $sig2) {
1457 return $token;
1458 }
1459
1460 $this->debug("Error: validateToken: Signature did not match.");
1461 return;
1462 }
1463
1464 /* Implementation of the methods needed to perform Windows Live
1465 application verification as well as trusted sign-in. */
1466
1471 /*public*/
1472 function getAppVerifier($ip=null)
1473 {
1474 $token = 'appid=' . $this->getAppId() . '&ts=' . $this->getTimestamp();
1475 $token .= ($ip ? "&ip={$ip}" : '');
1476 $token .= '&sig=' . $this->e64($this->signToken($token));
1477 return urlencode($token);
1478 }
1479
1500 /*public*/
1501 function getAppLoginUrl($siteid=null, $ip=null, $js=null)
1502 {
1503 $url = $this->getSecureUrl();
1504 $url .= 'wapplogin.srf?app=' . $this->getAppVerifier($ip);
1505 $url .= '&alg=' . $this->getSecurityAlgorithm();
1506 $url .= ($siteid ? "&id=$siteid" : '');
1507 $url .= ($js ? '&js=1' : '');
1508 return $url;
1509 }
1510
1528 /*public*/
1529 function getAppSecurityToken($siteid=null, $ip=null)
1530 {
1531 $body = $this->fetch($this->getAppLoginUrl($siteid, $ip));
1532 if (!$body) {
1533 $this->debug("Error: getAppSecurityToken: Could not fetch the application security token.");
1534 return;
1535 }
1536
1537 preg_match('/\{"token":"(.*)"\}/', $body, $matches);
1538 if(count($matches) == 2) {
1539 return $matches[1];
1540 }
1541 else {
1542 $this->debug("Error: getAppSecurityToken: Failed to extract token: $body");
1543 return;
1544 }
1545 }
1546
1553 /*public*/
1554 function getAppRetCode()
1555 {
1556 return 'appid=' . $this->getAppId();
1557 }
1558
1575 /*public*/
1576 function getTrustedParams($user, $retcode=null)
1577 {
1578 $token = $this->getTrustedToken($user);
1579 if (!$token) {
1580 return;
1581 }
1582 $token = "<wst:RequestSecurityTokenResponse xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"><wst:RequestedSecurityToken><wsse:BinarySecurityToken xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">$token</wsse:BinarySecurityToken></wst:RequestedSecurityToken><wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\"><wsa:EndpointReference xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\"><wsa:Address>uri:WindowsLiveID</wsa:Address></wsa:EndpointReference></wsp:AppliesTo></wst:RequestSecurityTokenResponse>";
1583
1584 $params = array();
1585 $params['wa'] = $this->getSecurityAlgorithm();
1586 $params['wresult'] = $token;
1587
1588 if ($retcode) {
1589 $params['wctx'] = $retcode;
1590 }
1591
1592 return $params;
1593 }
1594
1602 /*public*/
1603 function getTrustedToken($user)
1604 {
1605 if (!$user) {
1606 $this->debug('Error: getTrustedToken: Null user specified.');
1607 return;
1608 }
1609
1610 $token = "appid=" . $this->getAppId() . "&uid=" . urlencode($user)
1611 . "&ts=". $this->getTimestamp();
1612 $token .= "&sig=" . $this->e64($this->signToken($token));
1613 return urlencode($token);
1614 }
1615
1619 /*public*/
1621 {
1622 return $this->getSecureUrl() . 'wlogin.srf';
1623 }
1624
1629 /*public*/
1631 {
1632 return $this->getSecureUrl() . "logout.srf?appid=" + $this->getAppId();
1633 }
1634
1635 /* Helper methods */
1636
1640 /*private*/
1641 function parseSettings($settingsFile)
1642 {
1643 $settings = array(
1644 'appid' => '00163FFF8000E2C5',
1645 'secret' => '12345678901234567890',
1646 'securityalgorithm' => 'wsignin1.0',
1647 );
1648
1649 return $settings;
1650
1651 $doc = new DOMDocument();
1652 if (!$doc->load($settingsFile)) {
1653 $this->fatal("Error: parseSettings: Error while reading $settingsFile");
1654 }
1655
1656 $nl = $doc->getElementsByTagName('windowslivelogin');
1657 if($nl->length != 1) {
1658 $this->fatal("error: parseSettings: Failed to parse settings file:"
1659 . $settingsFile);
1660 }
1661
1662 $topnode = $nl->item(0);
1663 foreach ($topnode->childNodes as $node) {
1664 if ($node->nodeType == XML_ELEMENT_NODE) {
1665 $firstChild = $node->firstChild;
1666 if (!$firstChild) {
1667 $this->fatal("error: parseSettings: Failed to parse settings file:"
1668 . $settingsFile);
1669 }
1670 $settings[$node->nodeName] = $firstChild->nodeValue;
1671 }
1672 }
1673
1674 return $settings;
1675 }
1676
1681 /*private*/
1682 function derive($secret, $prefix)
1683 {
1684 if (!$secret || !$prefix) {
1685 $this->fatal("Error: derive: secret or prefix is null.");
1686 }
1687
1688 $keyLen = 16;
1689 $key = $prefix . $secret;
1690
1691 $key = hash("sha256", $key, true);
1692
1693 if (!$key || (strlen($key) < $keyLen)) {
1694 $this->debug("Error: derive: Unable to derive key.");
1695 return;
1696 }
1697
1698 return substr($key, 0, $keyLen);
1699 }
1700
1707 /*private*/
1708 function parse($input)
1709 {
1710 if (!$input) {
1711 $this->debug("Error: parse: Null input.");
1712 return;
1713 }
1714
1715 $input = explode('&', $input);
1716 $pairs = array();
1717
1718 foreach ($input as $pair) {
1719 $kv = explode('=', $pair);
1720 if (count($kv) != 2) {
1721 $this->debug("Error: parse: Bad input to parse: " . $pair);
1722 return;
1723 }
1724 $pairs[$kv[0]] = $kv[1];
1725 }
1726
1727 return $pairs;
1728 }
1729
1734 /*private*/
1735 function getTimestamp()
1736 {
1737 return time();
1738 }
1739
1743 /*private*/
1744 function e64($input)
1745 {
1746 if (is_null($input)) {
1747 return;
1748 }
1749 return urlencode(base64_encode($input));
1750 }
1751
1755 /*private*/
1756 function u64($input)
1757 {
1758 if(is_null($input))
1759 return;
1760 return base64_decode(urldecode($input));
1761 }
1762
1766 /*private*/
1767 function fetch($url)
1768 {
1769 /*
1770 if (!($handle = fopen($url, "rb"))) {
1771 WindowsLiveLogin::debug("error: fetch: Could not open url: $url");
1772 return;
1773 }
1774
1775 if (!($contents = stream_get_contents($handle))) {
1776 WindowsLiveLogin::debug("Error: fetch: Could not read from url: $url");
1777 }
1778
1779 fclose($handle);
1780 */
1781
1782 //$str = $url."\n\n".$contents."\n\n\n";
1783 //file_put_contents(__FILE__ . '.ftech.log', $str, FILE_APPEND);
1784
1785 $http = new \Bitrix\Main\Web\HttpClient([
1786 "redirect" => false,
1787 ]);
1788 $contents = $http->get($url);
1789
1790 return $contents;
1791 }
1792
1793 var $_error = false;
1794
1795 function setError($str)
1796 {
1797 $this->_error = $str;
1798 }
1799
1800 function getError()
1801 {
1802 if ($this->_error !== false)
1803 {
1804 return $this->_error;
1805 }
1806 }
1807
1809 {
1810 $arResult = Array();
1811 if (
1812 COption::GetOptionString('main', 'new_user_registration', 'Y') == 'Y' &&
1813 COption::GetOptionString('main', 'auth_liveid', 'N') == 'Y'
1814 )
1815 {
1816 $arResult[] = Array(
1817 'ID' => 'LIVEID',
1818 'NAME' => 'LiveID',
1819 );
1820 }
1821 return $arResult;
1822 }
1823
1824 public static function IsAvailable()
1825 {
1826 return function_exists('hash');
1827 }
1828}
$type
Определения options.php:106
$arResult
Определения generate_coupon.php:16
Определения liveid.php:22
getId()
Определения liveid.php:75
getToken()
Определения liveid.php:157
getError()
Определения liveid.php:179
setTimestamp($timestamp)
Определения liveid.php:51
setContext($context)
Определения liveid.php:143
$_usePersistentCookie
Определения liveid.php:102
setToken($token)
Определения liveid.php:166
$_timestamp
Определения liveid.php:36
$_token
Определения liveid.php:149
getContext()
Определения liveid.php:134
$_id
Определения liveid.php:69
setId($id)
Определения liveid.php:84
setError($str)
Определения liveid.php:174
usePersistentCookie()
Определения liveid.php:109
getTimestamp()
Определения liveid.php:42
setFlags($flags)
Определения liveid.php:118
$_error
Определения liveid.php:172
__construct($timestamp, $id, $flags, $context, $token)
Определения liveid.php:26
$_context
Определения liveid.php:127
Определения liveid.php:527
getTrustedParams($user, $retcode=null)
Определения liveid.php:1576
$_appid
Определения liveid.php:673
derive($secret, $prefix)
Определения liveid.php:1682
processToken($token, $context=null)
Определения liveid.php:1057
setAppId($appid)
Определения liveid.php:680
parseSettings($settingsFile)
Определения liveid.php:1641
$_securityalgorithm
Определения liveid.php:801
getError()
Определения liveid.php:1800
getManageConsentUrl($market=null)
Определения liveid.php:1203
getRefreshConsentTokenUrl($offers, $refreshtoken, $ru=null)
Определения liveid.php:1168
processConsent($query)
Определения liveid.php:1218
$_force_delauth_nonprovisioned
Определения liveid.php:826
setOldSecretExpiry($timestamp)
Определения liveid.php:777
refreshConsentToken($token, $ru=null)
Определения liveid.php:1293
setSecret($secret)
Определения liveid.php:717
$_signkey
Определения liveid.php:708
getOldSecretExpiry()
Определения liveid.php:795
getAppId()
Определения liveid.php:699
decodeAndValidateToken($token, $cryptkey=null, $signkey=null, $internal_allow_recursion=true)
Определения liveid.php:1331
setOldSecret($secret)
Определения liveid.php:751
getLoginUrl($context=null, $market=null)
Определения liveid.php:1007
setForceDelAuthNonProvisioned($force_delauth_nonprovisioned)
Определения liveid.php:834
$_oldsignkey
Определения liveid.php:732
$_policyurl
Определения liveid.php:840
$_oldsecretexpiry
Определения liveid.php:765
refreshConsentToken2($offers_string, $refreshtoken, $ru=null)
Определения liveid.php:1307
validateToken($token, $signkey=null)
Определения liveid.php:1426
parse($input)
Определения liveid.php:1708
signToken($token, $signkey=null)
Определения liveid.php:1405
$_debug
Определения liveid.php:531
$_returnurl
Определения liveid.php:875
getTrustedToken($user)
Определения liveid.php:1603
processLogin($query)
Определения liveid.php:1039
setSecurityAlgorithm($securityalgorithm)
Определения liveid.php:807
getSecureUrl()
Определения liveid.php:961
$_oldcryptkey
Определения liveid.php:734
setBaseUrl($baseurl)
Определения liveid.php:922
$_baseurl
Определения liveid.php:913
setError($str)
Определения liveid.php:1795
getLogoutUrl($market=null)
Определения liveid.php:1022
getTimestamp()
Определения liveid.php:1735
fatal($string)
Определения liveid.php:563
e64($input)
Определения liveid.php:1744
fetch($url)
Определения liveid.php:1767
getSecurityAlgorithm()
Определения liveid.php:816
setReturnUrl($returnurl)
Определения liveid.php:885
$_consenturl
Определения liveid.php:971
u64($input)
Определения liveid.php:1756
getTrustedLogoutUrl()
Определения liveid.php:1630
static IsAvailable()
Определения liveid.php:1824
setDebug($debug)
Определения liveid.php:539
getAppRetCode()
Определения liveid.php:1554
debug($string)
Определения liveid.php:551
$_error
Определения liveid.php:1793
getAppVerifier($ip=null)
Определения liveid.php:1472
OnExternalAuthList()
Определения liveid.php:1808
getReturnUrl()
Определения liveid.php:900
getConsentBaseUrl()
Определения liveid.php:988
getTrustedLoginUrl()
Определения liveid.php:1620
initFromXml($settingsFile)
Определения liveid.php:640
getConsentUrl($offers, $context=null, $ru=null, $market=null)
Определения liveid.php:1139
getBaseUrl()
Определения liveid.php:934
setPolicyUrl($policyurl)
Определения liveid.php:846
__construct( $appid=null, $secret=null, $securityalgorithm=null, $force_delauth_nonprovisioned=null, $policyurl=null, $returnurl=null)
Определения liveid.php:593
getAppLoginUrl($siteid=null, $ip=null, $js=null)
Определения liveid.php:1501
getPolicyUrl()
Определения liveid.php:861
getClearCookieResponse()
Определения liveid.php:1114
setSecureUrl($secureurl)
Определения liveid.php:951
$_cryptkey
Определения liveid.php:710
processConsentToken($token, $context=null)
Определения liveid.php:1241
decodeToken($token, $cryptkey=null)
Определения liveid.php:1376
$_secureurl
Определения liveid.php:944
setConsentBaseUrl($consenturl)
Определения liveid.php:978
getAppSecurityToken($siteid=null, $ip=null)
Определения liveid.php:1529
$str
Определения commerceml2.php:63
$contents
Определения commerceml2.php:57
$content
Определения commerceml.php:144
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$context
Определения csv_new_setup.php:223
$GLOBALS[ 'DB'] debug
Определения start.php:56
$settings
Определения product_settings.php:43
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$matches
Определения index.php:22
$action
Определения file_dialog.php:21
$url
Определения iframe.php:7