1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
qrcodeauth.php
См. документацию.
1<?php
2
9namespace Bitrix\Main\Controller;
10
11use Bitrix\Main;
12use Bitrix\Main\Config;
13use Bitrix\Main\Security;
14use Bitrix\Main\Localization\Loc;
15use Bitrix\Pull;
16
18{
19 public function isAllowed()
20 {
21 if (Config\Option::get('main', 'allow_qrcode_auth', 'N') !== 'Y')
22 {
23 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_option'), 'ERR_OPTION'));
24 return false;
25 }
26
27 if (!Main\Loader::includeModule('pull') || !\CPullOptions::GetQueueServerStatus())
28 {
29 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_pull'), 'ERR_PULL'));
30 return false;
31 }
32
33 return true;
34 }
35
36 public function pushTokenAction(string $siteId, string $uniqueId, string $channelTag, string $redirectUrl = '')
37 {
38 $this->pushToken($siteId, $uniqueId, $channelTag, $redirectUrl);
39 }
40
41 public function authenticateAction($token, bool $remember = false)
42 {
43 global $USER;
44
45 if ($token == '')
46 {
47 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_request'), "ERR_PARAMS"));
48 return null;
49 }
50
51 if (!$USER->LoginHitByHash($token, false, true, $remember))
52 {
53 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_auth'), "ERR_AUTH"));
54 return null;
55 }
56
57 return true;
58 }
59
71 public function pushToken(string $siteId, string $uniqueId, string $channelTag, string $redirectUrl = '', ?string $currentUrl = null)
72 {
73 if ($siteId == '' || $uniqueId == '' || $channelTag == '')
74 {
75 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_request'), 'ERR_PARAMS'));
76 return null;
77 }
78
79 $event = new \Bitrix\Main\Event(
80 'main',
81 'OnPushQrCodeAuthToken',
82 [
83 'siteId' => $siteId,
84 'uniqueId' => $uniqueId,
85 'channelTag' => $channelTag,
86 'redirectUrl' => $redirectUrl,
87 ]
88 );
89 $event->send();
90
91 foreach ($event->getResults() as $eventResult)
92 {
93 if ($eventResult->getType() === Main\EventResult::ERROR)
94 {
95 $error = $eventResult->getParameters()['error'] ?? null;
96 if ($error instanceof Main\Error)
97 {
98 $this->addError($error);
99 }
100 if (is_string($error) && $error !== '')
101 {
102 $this->addError(new Main\Error($error, 'ERR_FROM_EVENT'));
103 }
104
105 return null;
106 }
107
108 return true;
109 }
110
111 if ($uniqueId !== static::getUniqueId())
112 {
113 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_unique_id'), 'ERR_UNIQUE_ID'));
114 return null;
115 }
116
117 if (!$this->isAllowed())
118 {
119 return null;
120 }
121
122 $channel = Pull\Model\Channel::createWithTag($channelTag);
123
124 $url = $currentUrl ?? Main\Context::getCurrent()->getRequest()->getRequestedPage();
125
126 $token = \CUser::GetHitAuthHash($url, false, $siteId);
127 if ($token === false)
128 {
129 $token = \CUser::AddHitAuthHash($url, false, $siteId, 60);
130 }
131
133 [$channel],
134 [
135 'module_id' => 'main',
136 'command' => 'qrAuthorize',
137 'expiry' => 60,
138 'params' => [
139 'token' => $token,
140 'redirectUrl' => $redirectUrl,
141 ],
142 ]
143 );
144
145 return true;
146 }
147
148 public static function getUniqueId()
149 {
150 $uniqid = Config\Option::get('main', '~public_uniq_id', '');
151
152 if ($uniqid == '')
153 {
154 $uniqid = Security\Random::getString(16, true);
155 Config\Option::set('main', '~public_uniq_id', $uniqid);
156 }
157
158 return $uniqid;
159 }
160
161 public function configureActions()
162 {
163 return [
164 'authenticate' => [
165 '-prefilters' => [
166 Main\Engine\ActionFilter\Authentication::class,
167 ],
168 ],
169 ];
170 }
171}
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
static set($moduleId, $name, $value="", $siteId="")
Определения option.php:261
static getUniqueId()
Определения qrcodeauth.php:148
authenticateAction($token, bool $remember=false)
Определения qrcodeauth.php:41
pushToken(string $siteId, string $uniqueId, string $channelTag, string $redirectUrl='', ?string $currentUrl=null)
Определения qrcodeauth.php:71
pushTokenAction(string $siteId, string $uniqueId, string $channelTag, string $redirectUrl='')
Определения qrcodeauth.php:36
addError(Error $error)
Определения controller.php:1070
Определения error.php:15
static includeModule($moduleName)
Определения loader.php:67
static getString($length, $caseSensitive=false)
Определения random.php:76
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
static createWithTag(string $tag)
Определения channel.php:17
global $USER
Определения csv_new_run.php:40
$siteId
Определения ajax.php:8
$event
Определения prolog_after.php:141
$error
Определения subscription_card_product.php:20
$url
Определения iframe.php:7