Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
qrcodeauth.php
1<?php
2
10
11use Bitrix\Main;
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($siteId, $uniqueId, $channelTag, $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
69 public function pushToken($siteId, $uniqueId, $channelTag, $redirectUrl = '', $currentUrl = null)
70 {
71 if ($siteId == '' || $uniqueId == '' || $channelTag == '')
72 {
73 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_request'), 'ERR_PARAMS'));
74 return null;
75 }
76
77 $event = new \Bitrix\Main\Event(
78 'main',
79 'OnPushQrCodeAuthToken',
80 [
81 'siteId' => $siteId,
82 'uniqueId' => $uniqueId,
83 'channelTag' => $channelTag,
84 'redirectUrl' => $redirectUrl,
85 ]
86 );
87 $event->send();
88
89 foreach ($event->getResults() as $eventResult)
90 {
91 if ($eventResult->getType() === Main\EventResult::ERROR)
92 {
93 $error = $eventResult->getParameters()['error'] ?? null;
94 if ($error instanceof Main\Error)
95 {
96 $this->addError($error);
97 }
98 if (is_string($error) && $error !== '')
99 {
100 $this->addError(new Main\Error($error, 'ERR_FROM_EVENT'));
101 }
102
103 return null;
104 }
105
106 return true;
107 }
108
109 if ($uniqueId !== static::getUniqueId())
110 {
111 $this->addError(new Main\Error(Loc::getMessage('qrcodeauth_error_unique_id'), 'ERR_UNIQUE_ID'));
112 return null;
113 }
114
115 if (!$this->isAllowed())
116 {
117 return null;
118 }
119
120 $channel = Pull\Model\Channel::createWithTag($channelTag);
121
122 $url = $currentUrl ?? Main\Context::getCurrent()->getRequest()->getRequestedPage();
123
124 $token = \CUser::GetHitAuthHash($url, false, $siteId);
125 if ($token === false)
126 {
127 $token = \CUser::AddHitAuthHash($url, false, $siteId, 60);
128 }
129
130 Pull\Event::add(
131 [$channel],
132 [
133 'module_id' => 'main',
134 'command' => 'qrAuthorize',
135 'expiry' => 60,
136 'params' => [
137 'token' => $token,
138 'redirectUrl' => $redirectUrl,
139 ],
140 ]
141 );
142
143 return true;
144 }
145
146 public static function getUniqueId()
147 {
148 $uniqid = Config\Option::get('main', '~public_uniq_id', '');
149
150 if ($uniqid == '')
151 {
152 $uniqid = Security\Random::getString(16, true);
153 Config\Option::set('main', '~public_uniq_id', $uniqid);
154 }
155
156 return $uniqid;
157 }
158
159 public function configureActions()
160 {
161 return [
162 'authenticate' => [
163 '-prefilters' => [
164 Main\Engine\ActionFilter\Authentication::class,
165 ],
166 ],
167 ];
168 }
169}
authenticateAction($token, bool $remember=false)
pushToken($siteId, $uniqueId, $channelTag, $redirectUrl='', $currentUrl=null)
pushTokenAction($siteId, $uniqueId, $channelTag, $redirectUrl='')
static includeModule($moduleName)
Definition loader.php:69
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29