9namespace Bitrix\Rest\APAuth;
11use Bitrix\Main\Authentication\ApplicationManager;
12use Bitrix\Main\Authentication\ApplicationPasswordTable;
13use Bitrix\Main\Context;
14use Bitrix\Main\Localization\Loc;
15use Bitrix\Main\Type\DateTime;
16use Bitrix\Main\UserTable;
17use Bitrix\Rest\Engine\Access;
18use Bitrix\Rest\Engine\Access\HoldEntity;
19use Bitrix\Rest\Preset\IntegrationTable;
26 'UID' =>
'aplogin',
'PASSWORD' =>
'ap',
33 private const TEMPORARY_FORBIDDEN_INTEGRATION = [
'export-email-new-contact',
'contact-add'];
38 foreach(static::$authQueryParams as
$key)
50 if (!defined(
'REST_APAUTH_ALLOW_HTTP') && !Context::getCurrent()->getRequest()->isHttps())
52 $res =
array(
'error' =>
'INVALID_REQUEST',
'error_description' =>
'Https required.');
56 $tokenInfo = static::check(
$auth, $scope);
58 if (is_array($tokenInfo))
60 $error = array_key_exists(
'error', $tokenInfo);
62 if (!
$error && HoldEntity::is(HoldEntity::TYPE_WEBHOOK,
$auth[static::$authQueryParams[
'PASSWORD']]))
65 'error' =>
'OVERLOAD_LIMIT',
66 'error_description' =>
'REST API is blocked due to overload.'
74 !Access::isAvailableAPAuthByPasswordId((
int)$tokenInfo[
'password_id'])
76 Access::needCheckCount()
77 && !Access::isAvailableCount(Access::ENTITY_TYPE_WEBHOOK, $tokenInfo[
'password_id'])
83 'error' =>
'ACCESS_DENIED',
84 'error_description' =>
'REST is available only by subscription.'
89 if (!
$error && $tokenInfo[
'user_id'] > 0)
91 $tokenInfo[
'scope'] = implode(
',', static::getPasswordScope($tokenInfo[
'password_id']));
96 if ((
int)
$USER->GetID() !== (
int)$tokenInfo[
'user_id'])
99 'error' =>
'authorization_error',
100 'error_description' => Loc::getMessage(
'REST_AP_AUTH_ERROR_LOGOUT_BEFORE'),
105 elseif (!\CRestUtil::makeAuth($tokenInfo))
107 $tokenInfo =
array(
'error' =>
'authorization_error',
'error_description' =>
'Unable to authorize user');
114 'LAST_IP' => Context::getCurrent()->getRequest()->getRemoteAddress(),
117 unset($tokenInfo[
'application_id']);
123 $res[
'parameters_clear'] = static::$authQueryParams;
124 $res[
'auth_type'] = static::AUTH_TYPE;
134 $result =
array(
'error' =>
'INVALID_CREDENTIALS',
'error_description' =>
'Invalid request credentials');
136 $uid =
$auth[static::$authQueryParams[
'UID']];
149 'select' =>
array(
'ID'),
151 $userInfo =
$dbRes->fetch();
158 '=USER_ID' => $userInfo[
'ID'],
159 '=PASSWORD' =>
$auth[static::$authQueryParams[
'PASSWORD']],
162 'select' =>
array(
'ID')
164 $passwordInfo =
$dbRes->fetch();
168 $passwordInfo = static::checkOldPassword($userInfo[
'ID'],
$auth[static::$authQueryParams[
'PASSWORD']]);
173 if (static::checkPermission($passwordInfo[
"ID"], $scope) ===
true)
175 $forbiddenIntegration = IntegrationTable::query()
177 ->where(
'PASSWORD_ID', $passwordInfo[
"ID"])
178 ->whereIn(
'ELEMENT_CODE', static::TEMPORARY_FORBIDDEN_INTEGRATION)
184 if (!empty($forbiddenIntegration))
187 'error' =>
'QUERY_LIMIT_EXCEEDED',
188 'error_description' =>
'Rate limit exceeded. Too many requests in a given amount of time.'
194 'user_id' => $userInfo[
"ID"],
195 'password_id' => $passwordInfo[
"ID"],
201 $result =
array(
'error' =>
'insufficient_scope',
'error_description' =>
'The request requires higher privileges than provided by the webhook token');
212 if($appPassword !==
false)
217 if($appManager->checkScope($appPassword[
"APPLICATION_ID"]) ===
true)
219 return static::convertOldPassword($appPassword,
$password);
229 $dbRes = ApplicationPasswordTable::getById($appPassword[
'ID']);
230 $oldPassword =
$dbRes->fetch();
233 ApplicationPasswordTable::delete($appPassword[
'ID']);
235 'USER_ID' => $oldPassword[
'USER_ID'],
238 'TITLE' => $oldPassword[
'SYSCOMMENT'],
239 'COMMENT' => $oldPassword[
'COMMENT'],
240 'DATE_CREATE' => $oldPassword[
'DATE_CREATE'],
241 'DATE_LOGIN' => $oldPassword[
'DATE_LOGIN'],
242 'LAST_IP' => $oldPassword[
'LAST_IP'],
246 $passwordId =
$result->getId();
248 foreach(static::$integrationScope as $scope)
251 'PASSWORD_ID' => $passwordId,
267 if($scope === \CRestUtil::GLOBAL_SCOPE)
272 $scopeList = static::getPasswordScope($passwordId);
274 return in_array($scope, $scopeList);
279 if (!array_key_exists($passwordId, static::$scopeCache))
281 static::$scopeCache[$passwordId] = [];
284 ->setSelect([
'PERM'])
285 ->where(
'PASSWORD_ID', $passwordId)
290 static::$scopeCache[$passwordId][] =
$perm[
'PERM'];
294 return static::$scopeCache[$passwordId];
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getList(array $parameters=array())
static update($primary, array $data)
static checkOldPassword($userId, $password)
static check($auth, $scope)
static getPasswordScope($passwordId)
static convertOldPassword($appPassword, $password)
static checkPermission($passwordId, $scope)
static onRestCheckAuth(array $query, $scope, &$res)
static fillAlternativeScope($scope, $scopeList)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key