From ae1b6e3256b3aebe046997628dccd4df6f5128f7 Mon Sep 17 00:00:00 2001 From: Ruslan Shikhmagomedov Date: Fri, 10 Apr 2020 03:16:24 +0300 Subject: [PATCH 1/3] Add method refresh token --- src/Esia/Config.php | 38 +++++++++++++++++++++++++++ src/Esia/OpenId.php | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/Esia/Config.php b/src/Esia/Config.php index 569b92f..0fdb192 100644 --- a/src/Esia/Config.php +++ b/src/Esia/Config.php @@ -39,6 +39,9 @@ class Config private $token = ''; private $oid = ''; + private $refreshToken = ''; + private $state = ''; + private $code = ''; /** * Config constructor. @@ -121,6 +124,11 @@ public function getScope(): array return $this->scope; } + public function setScope(array $scope): void + { + $this->scope = $scope; + } + public function getScopeString(): string { return implode(' ', $this->scope); @@ -151,6 +159,36 @@ public function setToken(string $token): void $this->token = $token; } + public function setRefreshToken(string $refreshToken): void + { + $this->refreshToken = $refreshToken; + } + + public function getRefreshToken(): string + { + return $this->refreshToken; + } + + public function setState(string $state): void + { + $this->state = $state; + } + + public function getState(): string + { + return $this->state; + } + + public function getCode(): string + { + return $this->code; + } + + public function setCode(string $code): void + { + $this->code = $code; + } + public function getClientId(): string { return $this->clientId; diff --git a/src/Esia/OpenId.php b/src/Esia/OpenId.php index 8ae2a1e..481db98 100755 --- a/src/Esia/OpenId.php +++ b/src/Esia/OpenId.php @@ -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); From 8a6a68a7ef9452359ff1b4ce2d12a33ad2a030dc Mon Sep 17 00:00:00 2001 From: Ruslan Shikhmagomedov Date: Sun, 12 Apr 2020 20:19:57 +0300 Subject: [PATCH 2/3] Get organozation info --- src/Esia/OpenId.php | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/Esia/OpenId.php b/src/Esia/OpenId.php index 481db98..5970cfe 100755 --- a/src/Esia/OpenId.php +++ b/src/Esia/OpenId.php @@ -278,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); + + return $this->sendRequest(new Request('GET', $url)); + } + /** * Fetch contact info about current person * From c6332f3a483390976f587edb489193667ce4e2b5 Mon Sep 17 00:00:00 2001 From: Ruslan Shikhmagomedov Date: Sun, 12 Apr 2020 20:36:53 +0300 Subject: [PATCH 3/3] Fix https link Http link is invalid --- src/Esia/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Esia/Config.php b/src/Esia/Config.php index 0fdb192..2b1e8c3 100644 --- a/src/Esia/Config.php +++ b/src/Esia/Config.php @@ -11,7 +11,7 @@ class Config private $privateKeyPath; private $certPath; - private $portalUrl = 'http://esia-portal1.test.gosuslugi.ru/'; + private $portalUrl = 'https://esia-portal1.test.gosuslugi.ru/'; private $tokenUrlPath = 'aas/oauth2/te'; private $codeUrlPath = 'aas/oauth2/ac'; private $personUrlPath = 'rs/prns';