-
Notifications
You must be signed in to change notification settings - Fork 68
Получение информации об организациях пользователя #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,6 +187,70 @@ public function getToken(string $code): string | |
|
|
||
| $this->logger->debug('Payload: ', $payload); | ||
|
|
||
| $token = $payload['access_token']; | ||
|
|
||
| $this->config->setToken($token); | ||
| $this->config->setCode($code); | ||
| $this->config->setRefreshToken($payload['refresh_token']); | ||
| $this->config->setState($payload['state']); | ||
|
|
||
| # get object id from token | ||
| $chunks = explode('.', $token); | ||
| $payload = json_decode($this->base64UrlSafeDecode($chunks[1]), true); | ||
| $this->config->setOid($payload['urn:esia:sbj_id']); | ||
|
|
||
| return $token; | ||
| } | ||
|
|
||
| /** | ||
| * Method refresh a token with given scopes | ||
| * | ||
| * @param array $scope | ||
| * @return mixed | ||
| * @throws AbstractEsiaException | ||
| * @throws SignFailException | ||
| */ | ||
| public function refreshToken(array $scope = []) | ||
| { | ||
| $timestamp = $this->getTimeStamp(); | ||
|
|
||
| if (empty($scope) == false) { | ||
| $this->config->setScope($scope); | ||
| } | ||
|
|
||
| $clientSecret = $this->signer->sign( | ||
| $this->config->getScopeString() | ||
| . $timestamp | ||
| . $this->config->getClientId() | ||
| . $this->config->getState() | ||
| ); | ||
|
|
||
| $body = [ | ||
| 'client_id' => $this->config->getClientId(), | ||
| 'code' => $this->config->getCode(), | ||
| 'grant_type' => 'client_credentials', | ||
| 'client_secret' => $clientSecret, | ||
| 'state' => $this->config->getState(), | ||
| 'redirect_uri' => $this->config->getRedirectUrl(), | ||
| 'scope' => $this->config->getScopeString(), | ||
| 'timestamp' => $timestamp, | ||
| 'token_type' => 'Bearer', | ||
| 'refresh_token' => $this->config->getRefreshToken(), | ||
| ]; | ||
|
|
||
| $payload = $this->sendRequest( | ||
| new Request( | ||
| 'POST', | ||
| $this->config->getTokenUrl(), | ||
| [ | ||
| 'Content-Type' => 'application/x-www-form-urlencoded', | ||
| ], | ||
| http_build_query($body) | ||
| ) | ||
| ); | ||
|
|
||
| $this->logger->debug('Payload: ', $payload); | ||
|
|
||
| $token = $payload['access_token']; | ||
| $this->config->setToken($token); | ||
|
|
||
|
|
@@ -214,6 +278,59 @@ public function getPersonInfo(): array | |
| return $this->sendRequest(new Request('GET', $url)); | ||
| } | ||
|
|
||
| /** | ||
| * Fetch list of organization links | ||
| * | ||
| * You must collect token person before | ||
| * calling this method | ||
| * | ||
| * @return array | ||
| * @throws AbstractEsiaException | ||
| * @throws Exceptions\InvalidConfigurationException | ||
| */ | ||
| public function getOrganizationLinks() | ||
| { | ||
| $links = []; | ||
|
|
||
| $url = $this->config->getPersonUrl() . '/orgs'; | ||
| $response = $this->sendRequest(new Request('GET', $url)); | ||
|
|
||
| if (array_key_exists('size', $response) && $response['size'] > 0) { | ||
| $links = $response['elements']; | ||
| } | ||
|
|
||
| return $links; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch organization info from organization link | ||
| * | ||
| * You must collect token person before | ||
| * calling this method | ||
| * | ||
| * @param string $url - organization link | ||
| * @param array $scopes | ||
| * @return array | ||
| * @throws AbstractEsiaException | ||
| * @throws SignFailException | ||
| */ | ||
| public function getOrganizationInfo(string $url, array $scopes = ['org_shortname', 'org_inn']) | ||
| { | ||
| if (preg_match('/\/rs\/orgs\/(\d+)/', $url, $matches) == false) { | ||
| throw new RuntimeException('Please provide correct organization url'); | ||
| } | ||
|
|
||
| $orgId = $matches[1]; | ||
|
|
||
| $scopes = array_map(function ($scope) use ($orgId) { | ||
| return "http://esia.gosuslugi.ru/{$scope}?org_oid={$orgId}"; | ||
| }, $scopes); | ||
|
|
||
| $this->refreshToken($scopes); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А для чего обновлять токен при запросе организации?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для того, чтобы передать скоуп вида http://esia.gosuslugi.ru/org_inn?org_oid=#ид_организации_который_нам_заранее_не_известен#
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Странно, а разве пользователь не должен дать согласие на получение данных из другого скоупа, нежели был изначально запрошен при получение токена? Есть ссылка на документацию где можно почитать об этом?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, странно. Если передать скоуп вида Документация вот https://digital.gov.ru/ru/activity/directions/13/#section-docs
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, пробовал. Там возвращаются не все данные, а только этот набор из списка. Другие данные вытащить не получалось. |
||
|
|
||
| return $this->sendRequest(new Request('GET', $url)); | ||
| } | ||
|
|
||
| /** | ||
| * Fetch contact info about current person | ||
| * | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут наверно должно было использовано свойство
$portalUrlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нет, валидный скоуп именно такой, должен начинаться с http://esia.gosuslugi.ru/
Если мы будем передавать
$portalUrl, то на тестовом стенде ЕСИА это не сработает, потому как там$portalUrlотличается.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может вынести в константу?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, можно.
А как лучше назвать, ESIA_URL ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно думаю
SCOPE_DOMAINThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Или
SCOPE_URL