Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
auth.php
1<?php
9namespace Bitrix\Im\Call;
10
11class Auth
12{
13 const AUTH_TYPE = 'call';
14
15 const AUTH_CODE_GUEST = 'guest';
16 const PASSWORD_CHECK_METHOD = 'im.videoconf.password.check';
17
19 'im.call.user.register',
20 'im.videoconf.password.check',
21
22 'server.time',
23 'pull.config.get',
24 'pull.watch.extend',
25 ];
26
28 'mobile.browser.const.get',
29 'im.user.get',
30 'im.dialog.users.list',
31
32 // pull
33 'server.time',
34 'pull.config.get',
35 'pull.watch.extend',
36 // im
37 'im.chat.get',
38 'im.message.add',
39 'im.message.update',
40 'im.message.delete',
41 'im.message.like',
42 'im.dialog.writing',
43 'im.dialog.messages.get',
44 'im.dialog.read',
45 'im.disk.folder.get',
46 'im.disk.file.commit',
47 'im.user.list.get',
48 'im.call.create',
49 'im.call.invite',
50 'im.call.answer',
51 'im.call.ping',
52 'im.call.channel.public.list',
53 'im.call.hangup',
54 'im.call.decline',
55 'im.call.getusers',
56 'im.call.get',
57 'im.call.tryjoincall',
58 'local.call.log',
59 'smile.get',
60 // disk
61 'disk.folder.uploadfile',
62 // user
63 'im.call.user.update',
64 //voximplant
65 'voximplant.authorization.get',
66 'voximplant.authorization.onerror',
67 'voximplant.authorization.signonetimekey'
68 ];
69
70 // TODO sync AUTH_ID_PARAM with file /rest/services/rest/index.php
71 const AUTH_ID_PARAM = 'call_auth_id';
72
73 protected static $authQueryParams = [
75 ];
76
77 public static function onRestCheckAuth(array $query, $scope, &$res)
78 {
79 global $USER;
80
81 $authCode = null;
82 foreach(static::$authQueryParams as $key)
83 {
84 if(array_key_exists($key, $query))
85 {
86 $authCode = $query[$key];
87 break;
88 }
89 }
90
91 if ($authCode === null)
92 {
93 return null;
94 }
95
96 $conference = null;
97 $method = \CRestServer::instance()->getMethod();
98 if ($method === self::PASSWORD_CHECK_METHOD)
99 {
100 $conference = Conference::getById((int)$query['videoconf_id']);
101
102 if (!$conference || !$conference->isActive())
103 {
104 $res = [
105 'error' => 'CALL_AUTH_NOT_ACTIVE',
106 'error_description' => 'Call: conference is not active',
107 'additional' => []
108 ];
109
110 return false;
111 }
112 }
113 else
114 {
115 $storage = \Bitrix\Main\Application::getInstance()->getLocalSession('conference_check_' . $query['videoconf_id']);
116 if($storage->get('checked') === true)
117 {
118 //TODO: check conf status by checking start date from cache
119 }
120 else
121 {
122 $conference = Conference::getById((int)$query['videoconf_id']);
123
124 if (!$conference || !$conference->isActive())
125 {
126 $res = [
127 'error' => 'CALL_AUTH_VIDEOCONF_NOT_ACTIVE',
128 'error_description' => 'Call: conference is not active',
129 'additional' => []
130 ];
131
132 return false;
133 }
134
135 if ($conference->isPasswordRequired())
136 {
137 if ($conference->getPassword() === $query['videoconf_password'])
138 {
139 $storage->set('checked', true);
140 }
141 else
142 {
143 $res = [
144 'error' => 'CALL_AUTH_ACCESS_DENIED',
145 'error_description' => 'Call: access to conference is denied',
146 'additional' => []
147 ];
148
149 return false;
150 }
151 }
152 }
153 }
154
155 if ($authCode == self::AUTH_CODE_GUEST)
156 {
157 if (self::checkQueryMethod(self::METHODS_WITHOUT_AUTH))
158 {
159 $res = self::getSuccessfulResult();
160
161 return true;
162 }
163 else
164 {
165 $res = [
166 'error' => 'CALL_AUTH_METHOD_ERROR',
167 'error_description' => 'Call: you don\'t have access to use this method [1]',
168 'additional' => []
169 ];
170
171 return false;
172 }
173 }
174 else if (!preg_match("/^[a-fA-F0-9]{32}$/i", $authCode))
175 {
176 $res = [
177 'error' => 'CALL_AUTH_FAILED',
178 'error_description' => 'Call: user auth failed [code is not correct]',
179 'additional' => []
180 ];
181 }
182
183 if (!self::checkQueryMethod(array_merge(self::METHODS_WITH_AUTH, self::METHODS_WITHOUT_AUTH)))
184 {
185 $res = [
186 'error' => 'CALL_AUTH_METHOD_ERROR',
187 'error_description' => 'Call: you don\'t have access to use this method [2]',
188 'additional' => []
189 ];
190
191 return false;
192 }
193
194 $xmlId = self::AUTH_TYPE."|".$authCode;
195
196 if ($USER->IsAuthorized())
197 {
198 if ($USER->GetParam('EXTERNAL_AUTH_ID') == 'call')
199 {
200 if ($USER->GetParam('XML_ID') == $xmlId)
201 {
202 $res = self::getSuccessfulResult();
203
204 \CUser::SetLastActivityDate($USER->GetID(), true);
205
206 return true;
207 }
208 else
209 {
210 $res = [
211 'error' => 'CALL_AUTH_DIFF_USER',
212 'error_description' => 'Call: you are authorized with a different user [2]',
213 'additional' => ['hash' => mb_substr($USER->GetParam('XML_ID'), mb_strlen(self::AUTH_TYPE) + 1)]
214 ];
215
216 return false;
217 }
218 }
219 else
220 {
221 $res = [
222 'error' => 'CALL_AUTH_PORTAL_USER',
223 'error_description' => 'Call: you are authorized with a portal user [2]',
224 'additional' => []
225 ];
226
227 return false;
228 }
229 }
230
231 $userData = \Bitrix\Main\UserTable::getList(
232 [
233 'select' => ['ID', 'EXTERNAL_AUTH_ID'],
234 'filter' => ['=XML_ID' => $xmlId]
235 ]
236 )->fetch();
237
238 if ($userData && $userData['EXTERNAL_AUTH_ID'] == 'call')
239 {
240 self::authorizeById($userData['ID']);
241
242 $res = self::getSuccessfulResult();
243
244 \CUser::SetLastActivityDate($USER->GetID(), true);
245
246 return true;
247 }
248
249 $res = [
250 'error' => 'CALL_AUTH_FAILED',
251 'error_description' => 'Call: user auth failed [user not found]',
252 'additional' => []
253 ];
254
255 return false;
256 }
257
258 public static function authorizeById($userId, $setCookie = null, $skipAuthorizeCheck = false)
259 {
260 global $USER;
261
262 if (!$skipAuthorizeCheck && $USER->IsAuthorized())
263 {
264 return false;
265 }
266
267 $context = \Bitrix\Main\Context::getCurrent();
268
269 if (is_null($setCookie))
270 {
271 $setCookie = false;
272 if ($context->getRequest()->getCookieRaw('BITRIX_CALL_AUTH'))
273 {
274 $setCookie = true;
275 }
276 }
277
278 if ($USER->GetID() != $userId)
279 {
280 $USER->Authorize($userId, $setCookie, $setCookie, 'public');
281 }
282
283 $cookie = new \Bitrix\Main\Web\Cookie('BITRIX_CALL_AUTH', 'Y', null, false);
284 $cookie->setHttpOnly(false);
285 $context->getResponse()->addCookie($cookie);
286
287 $authCode = str_replace(self::AUTH_TYPE.'|', '', $USER->GetParam('XML_ID'));
288
289 $cookie = new \Bitrix\Main\Web\Cookie('BITRIX_CALL_HASH', $authCode, null, false);
290 $cookie->setHttpOnly(false);
291 $context->getResponse()->addCookie($cookie);
292
293 return true;
294 }
295
296 private static function getSuccessfulResult()
297 {
298 global $USER;
299
300 return [
301 'user_id' => $USER->GetID(),
302 'scope' => implode(',', \CRestUtil::getScopeList()),
303 'parameters_clear' => static::$authQueryParams,
304 'auth_type' => static::AUTH_TYPE,
305 ];
306 }
307
308 private static function checkQueryMethod($whiteListMethods)
309 {
310 if (\CRestServer::instance()->getMethod() == 'batch')
311 {
312 $result = false;
313 foreach (\CRestServer::instance()->getQuery()['cmd'] as $key => $method)
314 {
315 $method = mb_substr($method, 0, mb_strrpos($method, '?'));
316 $result = in_array(mb_strtolower($method), $whiteListMethods);
317 if (!$result)
318 {
319 break;
320 }
321 }
322 }
323 else
324 {
325 $result = in_array(\CRestServer::instance()->getMethod(), $whiteListMethods);
326 }
327
328 return $result;
329 }
330}
const PASSWORD_CHECK_METHOD
Definition auth.php:16
static $authQueryParams
Definition auth.php:73
const METHODS_WITHOUT_AUTH
Definition auth.php:18
const METHODS_WITH_AUTH
Definition auth.php:27
const AUTH_CODE_GUEST
Definition auth.php:15
const AUTH_ID_PARAM
Definition auth.php:71
static authorizeById($userId, $setCookie=null, $skipAuthorizeCheck=false)
Definition auth.php:258
static onRestCheckAuth(array $query, $scope, &$res)
Definition auth.php:77
static getById(int $id)