35 foreach(static::$authQueryParams as $key)
37 if(array_key_exists($key, $query))
39 $auth[$key] = $query[$key];
43 if(count($auth) === count(static::$authQueryParams))
48 $res = array(
'error' =>
'INVALID_REQUEST',
'error_description' =>
'Https required.');
52 $tokenInfo = static::check($auth, $scope);
54 if(is_array($tokenInfo))
56 $error = array_key_exists(
'error', $tokenInfo);
61 'error' =>
'OVERLOAD_LIMIT',
62 'error_description' =>
'REST API is blocked due to overload.'
79 'error' =>
'ACCESS_DENIED',
80 'error_description' =>
'REST is available only on commercial plans.'
85 if(!$error && $tokenInfo[
'user_id'] > 0)
87 $tokenInfo[
'scope'] = implode(
',', static::getPasswordScope($tokenInfo[
'password_id']));
90 if ($USER instanceof \CUser && $USER->isAuthorized())
92 if ((
int)$USER->GetID() !== (
int)$tokenInfo[
'user_id'])
95 'error' =>
'authorization_error',
96 'error_description' =>
Loc::getMessage(
'REST_AP_AUTH_ERROR_LOGOUT_BEFORE'),
101 elseif (!\CRestUtil::makeAuth($tokenInfo))
103 $tokenInfo = array(
'error' =>
'authorization_error',
'error_description' =>
'Unable to authorize user');
108 PasswordTable::update($tokenInfo[
'password_id'], array(
113 unset($tokenInfo[
'application_id']);
119 $res[
'parameters_clear'] = static::$authQueryParams;
120 $res[
'auth_type'] = static::AUTH_TYPE;
131 protected static function check($auth, $scope)
133 $result = array(
'error' =>
'INVALID_CREDENTIALS',
'error_description' =>
'Invalid request credentials');
135 $uid = $auth[static::$authQueryParams[
'UID']];
137 if(strval(intval($uid)) === $uid)
139 $userInfo = array(
'ID' => intval($uid));
143 $dbRes = UserTable::getList(array(
148 'select' => array(
'ID'),
150 $userInfo = $dbRes->fetch();
155 $dbRes = PasswordTable::getList(array(
157 '=USER_ID' => $userInfo[
'ID'],
158 '=PASSWORD' => $auth[static::$authQueryParams[
'PASSWORD']],
161 'select' => array(
'ID')
163 $passwordInfo = $dbRes->fetch();
167 $passwordInfo = static::checkOldPassword($userInfo[
'ID'], $auth[static::$authQueryParams[
'PASSWORD']]);
172 if(static::checkPermission($passwordInfo[
"ID"], $scope) ===
true)
175 'user_id' => $userInfo[
"ID"],
176 'password_id' => $passwordInfo[
"ID"],
181 $result = array(
'error' =>
'insufficient_scope',
'error_description' =>
'The request requires higher privileges than provided by the webhook token');
209 $dbRes = ApplicationPasswordTable::getById($appPassword[
'ID']);
210 $oldPassword = $dbRes->fetch();
213 ApplicationPasswordTable::delete($appPassword[
'ID']);
214 $result = PasswordTable::add(array(
215 'USER_ID' => $oldPassword[
'USER_ID'],
216 'PASSWORD' => $password,
218 'TITLE' => $oldPassword[
'SYSCOMMENT'],
219 'COMMENT' => $oldPassword[
'COMMENT'],
220 'DATE_CREATE' => $oldPassword[
'DATE_CREATE'],
221 'DATE_LOGIN' => $oldPassword[
'DATE_LOGIN'],
222 'LAST_IP' => $oldPassword[
'LAST_IP'],
224 if($result->isSuccess())
226 $passwordId = $result->getId();
228 foreach(static::$integrationScope as $scope)
230 PermissionTable::add(array(
231 'PASSWORD_ID' => $passwordId,
247 if($scope === \CRestUtil::GLOBAL_SCOPE)
252 $scopeList = static::getPasswordScope($passwordId);
253 $scopeList = \Bitrix\Rest\Engine\RestManager::fillAlternativeScope($scope, $scopeList);
254 return in_array($scope, $scopeList);
259 if(!array_key_exists($passwordId, static::$scopeCache))
261 static::$scopeCache[$passwordId] = array();
263 $dbRes = PermissionTable::getList(array(
265 '=PASSWORD_ID' => $passwordId,
267 'select' => array(
'PERM')
269 while($perm = $dbRes->fetch())
271 static::$scopeCache[$passwordId][] = $perm[
'PERM'];
275 return static::$scopeCache[$passwordId];