55 public static function get($clientId, $scope, $additionalParams, $userId)
57 return Application::getAuthProvider()->get($clientId, $scope, $additionalParams, $userId);
67 $authKey = static::getAuthKey($query);
71 $tokenInfo = static::check($authKey);
72 if(is_array($tokenInfo))
74 $error = array_key_exists(
'error', $tokenInfo);
76 if(!$error && !array_key_exists(
'client_id', $tokenInfo))
78 $tokenInfo = array(
'error' =>
'CONNECTION_ERROR',
'error_description' =>
'Error connecting to authorization server');
85 'error' =>
'OVERLOAD_LIMIT',
86 'error_description' =>
'REST API is blocked due to overload.'
103 'error' =>
'ACCESS_DENIED',
104 'error_description' =>
'REST is available only on commercial plans.'
112 if(is_array($clientInfo))
114 \CRestUtil::updateAppStatus($tokenInfo);
117 if(!is_array($clientInfo) || $clientInfo[
'ACTIVE'] !==
'Y')
119 $tokenInfo = array(
'error' =>
'APPLICATION_NOT_FOUND',
'error_description' =>
'Application not found');
124 if(!$error && $tokenInfo[
'expires'] <= time())
126 $tokenInfo = array(
'error' =>
'expired_token',
'error_description' =>
'The access token provided has expired');
130 if(!$error && $scope !== \CRestUtil::GLOBAL_SCOPE && isset($tokenInfo[
'scope']))
132 $tokenScope = explode(
',', $tokenInfo[
'scope']);
133 $tokenScope = \Bitrix\Rest\Engine\RestManager::fillAlternativeScope($scope, $tokenScope);
134 if(!in_array($scope, $tokenScope))
136 $tokenInfo = array(
'error' =>
'insufficient_scope',
'error_description' =>
'The request requires higher privileges than provided by the access token');
141 if(!$error && $tokenInfo[
'user_id'] > 0)
144 if ($USER instanceof \CUser && $USER->isAuthorized())
146 if ((
int)$USER->getId() !== (
int)$tokenInfo[
'user_id'])
149 'error' =>
'authorization_error',
150 'error_description' =>
Loc::getMessage(
'REST_OAUTH_ERROR_LOGOUT_BEFORE'),
155 elseif (!\CRestUtil::makeAuth($tokenInfo))
157 $tokenInfo = array(
'error' =>
'authorization_error',
'error_description' =>
'Unable to authorize user');
160 elseif(!\CRestUtil::checkAppAccess($tokenInfo[
'client_id']))
162 $tokenInfo = array(
'error' =>
'user_access_error',
'error_description' =>
'The user does not have access to the application.');
169 $res[
'parameters_clear'] = static::$authQueryParams;
170 $res[
'auth_type'] = static::AUTH_TYPE;
171 $res[
'parameters_callback'] = array(__CLASS__,
'updateTokenParameters');
173 foreach(static::$authQueryAdditional as $key)
175 if(array_key_exists($key, $query))
177 $res[$key] = $query[$key];
178 $res[
'parameters_clear'][] = $key;
236 protected static function check($accessToken)
238 $authResult = static::getStorage()->restore($accessToken);
239 if($authResult ===
false)
242 $tokenInfo = $client->checkAuth($accessToken);
244 if(is_array($tokenInfo))
246 if($tokenInfo[
'result'])
248 $authResult = $tokenInfo[
'result'];
249 $authResult[
'user_id'] = $authResult[
'parameters'][static::PARAM_LOCAL_USER];
250 unset($authResult[
'parameters'][static::PARAM_LOCAL_USER]);
253 if(!isset($authResult[
'expires']) && isset($authResult[
'expires_in']))
255 $authResult[
'expires'] = time() + $authResult[
'expires_in'];
260 $authResult = $tokenInfo;
261 $authResult[
'access_token'] = $accessToken;
264 static::getStorage()->store($authResult);
268 $authResult = [
'access_token' => $accessToken];
277 if(!is_array($additionalParams))
279 $additionalParams = array();
282 $additionalParams[static::PARAM_LOCAL_USER] = $userId;
283 $additionalParams[static::PARAM_TZ_OFFSET] = \CTimeZone::getOffset();
284 $additionalParams[Session::PARAM_SESSION] =
Session::get();
286 return $additionalParams;