Skip to content

Commit

Permalink
Получение нового маркера доступа в обмен на маркер обновления
Browse files Browse the repository at this point in the history
  • Loading branch information
ilimurzin committed Sep 15, 2024
1 parent bbe0995 commit 3f6784c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/Esia/OpenId.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,55 @@ public function getToken(string $code): string
'scope' => $this->config->getScopeString(),
'timestamp' => $timestamp,
'token_type' => 'Bearer',
'refresh_token' => $state,
];

$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);
$this->config->setRefreshToken($payload['refresh_token']);

# 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;
}

public function refreshToken(): string
{
$timestamp = $this->getTimeStamp();
$state = $this->buildState();

$clientSecret = $this->signer->sign(
$this->config->getScopeString()
. $timestamp
. $this->config->getClientId()
. $state
);

$body = [
'client_id' => $this->config->getClientId(),
'grant_type' => 'refresh_token',
'client_secret' => $clientSecret,
'state' => $state,
'redirect_uri' => $this->config->getRedirectUrl(),
'scope' => $this->config->getScopeString(),
'timestamp' => $timestamp,
'token_type' => 'Bearer',
'refresh_token' => $this->config->getRefreshToken(),
];

$payload = $this->sendRequest(
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/OpenIdCliOpensslTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,46 @@ public function testGetTokenRememberRefreshToken(): void
$openId->getToken('test');
self::assertSame($refreshToken, $openId->getConfig()->getRefreshToken());
}

public function testRefreshToken(): void
{
$config = new Config($this->config);
$client = $this->buildClientWithResponses([
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "first"}'),
]);
$openId = new OpenId($config, $client);
$openId->setSigner(new CliSignerPKCS7(
$this->config['certPath'],
$this->config['privateKeyPath'],
$this->config['privateKeyPassword'],
$this->config['tmpPath']
));

$openId->refreshToken();

self::assertSame('first', $openId->getConfig()->getRefreshToken());
}

public function testMultipleRefreshToken(): void
{
$config = new Config($this->config);
$client = $this->buildClientWithResponses([
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "first"}'),
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "second"}'),
]);
$openId = new OpenId($config, $client);
$openId->setSigner(new CliSignerPKCS7(
$this->config['certPath'],
$this->config['privateKeyPath'],
$this->config['privateKeyPassword'],
$this->config['tmpPath']
));

$openId->refreshToken();
$first = $openId->getConfig()->getRefreshToken();
$openId->refreshToken();
$second = $openId->getConfig()->getRefreshToken();

self::assertSame(['first','second'], [$first, $second]);
}
}
30 changes: 30 additions & 0 deletions tests/unit/OpenIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ public function testGetTokenRememberRefreshToken(): void
self::assertSame($refreshToken, $openId->getConfig()->getRefreshToken());
}

public function testRefreshToken(): void
{
$config = new Config($this->config);
$client = $this->buildClientWithResponses([
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "first"}'),
]);
$openId = new OpenId($config, $client);

$openId->refreshToken();

self::assertSame('first', $openId->getConfig()->getRefreshToken());
}

public function testMultipleRefreshToken(): void
{
$config = new Config($this->config);
$client = $this->buildClientWithResponses([
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "first"}'),
new Response(200, [], '{"access_token": "test.' . base64_encode('{"urn:esia:sbj_id": 123}') . '.test", "refresh_token": "second"}'),
]);
$openId = new OpenId($config, $client);

$openId->refreshToken();
$first = $openId->getConfig()->getRefreshToken();
$openId->refreshToken();
$second = $openId->getConfig()->getRefreshToken();

self::assertSame(['first','second'], [$first, $second]);
}

/**
* @throws InvalidConfigurationException
* @throws AbstractEsiaException
Expand Down

0 comments on commit 3f6784c

Please sign in to comment.