Skip to content

Commit 46deaa0

Browse files
authored
Merge pull request #107 from ydb-platform/create-private-refresh-ratio-token-parametr
Created refresh token ratio parameter
2 parents 362fd7b + fd4540a commit 46deaa0

12 files changed

+99
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* created refresh token ratio parameter
2+
13
## 1.9.0
24

35
* added microseconds in Timestamp type

src/Auth/Auth.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public abstract function getName(): string;
1313

1414
protected $logger;
1515

16+
protected $refreshTokenRatio;
17+
1618
public function logger(){
1719
return $this->logger;
1820
}
@@ -21,6 +23,25 @@ public function setLogger($logger){
2123
$this->logger = $logger;
2224
}
2325

26+
/**
27+
* @return float
28+
*/
29+
public function getRefreshTokenRatio(): float
30+
{
31+
return $this->refreshTokenRatio;
32+
}
33+
34+
/**
35+
* @param float $refreshTokenRatio
36+
*/
37+
public function setRefreshTokenRatio($refreshTokenRatio): void
38+
{
39+
if($refreshTokenRatio<=0||$refreshTokenRatio>=1){
40+
throw new \Exception("Refresh token ratio. Expected number between 0 and 1.");
41+
}
42+
$this->refreshTokenRatio = $refreshTokenRatio;
43+
}
44+
2445
/**
2546
* @param string $expiresAt
2647
* @return int

src/Auth/Implement/AccessTokenAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(string $access_token)
1818

1919
public function getTokenInfo(): TokenInfo
2020
{
21-
return new TokenInfo($this->access_token, time()+24*60*60);
21+
return new TokenInfo($this->access_token, time()+24*60*60, $this->refreshTokenRatio);
2222
}
2323

2424
public function getName(): string

src/Auth/Implement/AnonymousAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct()
1313

1414
public function getTokenInfo(): TokenInfo
1515
{
16-
return new TokenInfo("", time()+24*3600);
16+
return new TokenInfo("", time()+24*3600, $this->refreshTokenRatio);
1717
}
1818

1919
public function getName(): string

src/Auth/Implement/JwtWithJsonAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getTokenInfo(): TokenInfo
3535
'jwt' => $jwt_token,
3636
];
3737
$token = $this->requestToken($request_data);
38-
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt));
38+
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt), $this->refreshTokenRatio);
3939
}
4040

4141
public function getName(): string

src/Auth/Implement/JwtWithPrivateKeyAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getTokenInfo(): TokenInfo
2929
'jwt' => $jwt_token,
3030
];
3131
$token = $this->requestToken($request_data);
32-
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt));
32+
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt), $this->refreshTokenRatio);
3333
}
3434

3535
public function getName(): string

src/Auth/Implement/MetadataAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MetadataAuthentication extends \YdbPlatform\Ydb\Auth\Auth
1212
public function getTokenInfo(): TokenInfo
1313
{
1414
$token = $this->requestTokenFromMetadata();
15-
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt));
15+
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt), $this->refreshTokenRatio);
1616
}
1717

1818
public function getName(): string

src/Auth/Implement/OAuthTokenAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getTokenInfo(): TokenInfo
2222
'yandexPassportOauthToken' => $this->oauth_token,
2323
];
2424
$token = $this->requestToken($request_data);
25-
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt));
25+
return new TokenInfo($token->iamToken, $this->convertExpiresAt($token->expiresAt), $this->refreshTokenRatio);
2626
}
2727

2828
public function getName(): string

src/Auth/TokenInfo.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class TokenInfo
66
{
7-
const _PRIVATE_REFRESH_RATIO = 0.1;
87
/**
98
* @var string
109
*/
@@ -16,11 +15,11 @@ class TokenInfo
1615

1716
private $refreshAt;
1817

19-
public function __construct(string $token, int $expiresAt)
18+
public function __construct(string $token, int $expiresAt, float $refreshRatio = 0.1)
2019
{
2120
$this->token = $token;
2221
$this->expiresAt = $expiresAt;
23-
$this->refreshAt = time() + round(TokenInfo::_PRIVATE_REFRESH_RATIO*($this->expiresAt-time()),0);
22+
$this->refreshAt = time() + round($refreshRatio*($this->expiresAt-time()),0);
2423
}
2524

2625
/**

src/Iam.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
1111
use YdbPlatform\Ydb\Auth\Implement\JwtWithJsonAuthentication;
1212
use YdbPlatform\Ydb\Auth\Implement\JwtWithPrivateKeyAuthentication;
13+
use YdbPlatform\Ydb\Auth\Implement\MetadataAuthentication;
1314
use YdbPlatform\Ydb\Auth\Implement\OAuthTokenAuthentication;
1415
use YdbPlatform\Ydb\Contracts\IamTokenContract;
1516

@@ -153,6 +154,10 @@ protected function parseConfig(array $config)
153154
$parsedConfig["credentials"] = $config["credentials"];
154155
}
155156

157+
if (isset($config["refresh_token_ratio"])){
158+
$parsedConfig["refresh_token_ratio"] = $config["refresh_token_ratio"];
159+
}
160+
156161
foreach ($stringParams as $param)
157162
{
158163
$parsedConfig[$param] = (string)($config[$param] ?? '');
@@ -197,6 +202,8 @@ protected function initConfig()
197202
else if ($this->config('use_metadata'))
198203
{
199204
$this->logger()->info('YDB: Authentication method: Metadata URL');
205+
$this->config['credentials'] = new MetadataAuthentication();
206+
$this->config['credentials']->setLogger($this->logger());
200207
}
201208
else if ($serviceFile = $this->config('service_file'))
202209
{
@@ -237,6 +244,10 @@ protected function initConfig()
237244
$this->config['credentials'] = new OAuthTokenAuthentication($oauthToken);
238245
$this->config['credentials']->setLogger($this->logger());
239246
}
247+
248+
if ($this->config('credentials') !== null){
249+
$this->config['credentials']->setRefreshTokenRatio($this->config('refresh_token_ratio', 0.1));
250+
}
240251
else
241252
{
242253
throw new Exception('No authentication method is used.');

tests/CheckRefreshTokenRatioTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Exception;
7+
use YdbPlatform\Ydb\Ydb;
8+
use YdbPlatform\Ydb\YdbTable;
9+
10+
class CheckRefreshTokenRatioTest extends TestCase
11+
{
12+
public function testAnonymousConnection()
13+
{
14+
$awaitException = [0, 1];
15+
$awaitNormal = [0.05, 0.9];
16+
$config = [
17+
18+
// Database path
19+
'database' => '/local',
20+
21+
// Database endpoint
22+
'endpoint' => 'localhost:2136',
23+
24+
// Auto discovery (dedicated server only)
25+
'discovery' => false,
26+
27+
// IAM config
28+
'iam_config' => [
29+
'anonymous' => true,
30+
'insecure' => true
31+
],
32+
];
33+
34+
$ydb = new Ydb($config);
35+
self::assertEquals(0.1,
36+
$ydb->iam()->config('credentials')->getRefreshTokenRatio()
37+
);
38+
39+
foreach ($awaitNormal as $ratio){
40+
$config['iam_config']['refresh_token_ratio'] = $ratio;
41+
$ydb = new Ydb($config);
42+
self::assertEquals($ratio,
43+
$ydb->iam()->config('credentials')->getRefreshTokenRatio()
44+
);
45+
}
46+
47+
foreach ($awaitException as $ratio) {
48+
$config['iam_config']['refresh_token_ratio'] = $ratio;
49+
$this->expectExceptionObject(new \Exception("Refresh token ratio. Expected number between 0 and 1."));
50+
51+
$ydb = new Ydb($config);
52+
$ydb->iam()->config('credentials')->getRefreshTokenRatio();
53+
}
54+
55+
}
56+
}

tests/RefreshTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function test(){
8888
MetaGetter::getMeta($session)["x-ydb-auth-ticket"][0]
8989
);
9090
// Check that sdk used old token when failed refreshing
91-
usleep(TokenInfo::_PRIVATE_REFRESH_RATIO*$TOKEN_LIVE_TIME*1000*1000); // waiting 10% from token live time
91+
usleep($ydb->iam()->config('credentials')->getRefreshTokenRatio()*$TOKEN_LIVE_TIME*1000*1000); // waiting 10% from token live time
9292
$session->query('select 1 as res');
9393
self::assertEquals(
9494
2,

0 commit comments

Comments
 (0)