Skip to content

Commit 017261c

Browse files
committed
Fixed refresh token when it expired
1 parent 318310f commit 017261c

File tree

10 files changed

+103
-3
lines changed

10 files changed

+103
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* fixed refresh token when it expired
12
* fixed retry at BAD_SESSION
23
* added credentials authentication
34
* added CI test

src/Discovery.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Discovery
3030
* @var LoggerInterface
3131
*/
3232
protected $logger;
33+
/**
34+
* @var Iam
35+
*/
36+
protected $credentials;
3337

3438
/**
3539
* @param Ydb $ydb
@@ -43,6 +47,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
4347

4448
$this->meta = $ydb->meta();
4549

50+
$this->credentials = $ydb->iam();
51+
4652
$this->database = $ydb->database();
4753

4854
$this->logger = $logger;
@@ -82,4 +88,4 @@ protected function request($method, array $data = [])
8288
{
8389
return $this->doRequest('Discovery', $method, $data);
8490
}
85-
}
91+
}

src/Operations.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class Operations
2525
* @var LoggerInterface
2626
*/
2727
protected $logger;
28+
/**
29+
* @var Iam
30+
*/
31+
protected $credentials;
2832

2933
/**
3034
* @param Ydb $ydb
@@ -38,6 +42,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
3842

3943
$this->meta = $ydb->meta();
4044

45+
$this->credentials = $ydb->iam();
46+
4147
$this->logger = $logger;
4248
}
4349

src/Scheme.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
4545

4646
$this->meta = $ydb->meta();
4747

48+
$this->credentials = $ydb->iam();
49+
4850
$this->database = $ydb->database();
4951

5052
$this->logger = $logger;

src/Scripting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class Scripting
2525
* @var LoggerInterface
2626
*/
2727
protected $logger;
28+
/**
29+
* @var Iam
30+
*/
31+
protected $credentials;
2832

2933
/**
3034
* @param Ydb $ydb
@@ -38,6 +42,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
3842

3943
$this->meta = $ydb->meta();
4044

45+
$this->credentials = $ydb->iam();
46+
4147
$this->logger = $logger;
4248
}
4349

src/Session.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function __construct(Table $table, $session_id)
8282

8383
$this->client = $table->client();
8484
$this->meta = $table->meta();
85+
$this->credentials = $table->credentials();
8586
$this->path = $table->path();
8687
$this->logger = $table->getLogger();
8788

src/Table.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Table
4141
* @var LoggerInterface
4242
*/
4343
protected $logger;
44+
/**
45+
* @var Iam
46+
*/
47+
protected $credentials;
4448

4549
/**
4650
* @param Ydb $ydb
@@ -54,6 +58,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
5458

5559
$this->meta = $ydb->meta();
5660

61+
$this->credentials = $ydb->iam();
62+
5763
$this->path = $ydb->database();
5864

5965
$this->logger = $logger;
@@ -284,6 +290,14 @@ public function createTable($table, $columns, $primary_key = 'id', $indexes = []
284290
return $this->session()->createTable($table, $columns, $primary_key, $indexes);
285291
}
286292

293+
/**
294+
* @return Iam
295+
*/
296+
public function credentials(): Iam
297+
{
298+
return $this->credentials;
299+
}
300+
287301
/**
288302
* Proxy to Session::copyTable.
289303
*
@@ -407,4 +421,4 @@ protected function streamRequest($method, array $data = [])
407421
return $this->doStreamRequest('Table', $method, $data);
408422
}
409423

410-
}
424+
}

src/Traits/RequestTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ trait RequestTrait
4141
*/
4242
protected function doRequest($service, $method, array $data = [])
4343
{
44+
$this->meta['x-ydb-auth-ticket'] = [$this->credentials->token()];
45+
4446
$this->saveLastRequest($service, $method, $data);
4547

4648
$requestClass = '\\Ydb\\' . $service . '\\' . $method . 'Request';

tests/RefreshTokenTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Auth;
7+
use YdbPlatform\Ydb\Auth\TokenInfo;
8+
use YdbPlatform\Ydb\Ydb;
9+
10+
class TestCredentials extends Auth{
11+
12+
public function getTokenInfo(): TokenInfo
13+
{
14+
return new TokenInfo(time()+1,time()+1);
15+
}
16+
17+
public function getName(): string
18+
{
19+
return "TestCredentials";
20+
}
21+
}
22+
23+
class MetaGetter extends \YdbPlatform\Ydb\Session{
24+
public static function getMeta(\YdbPlatform\Ydb\Session $session){
25+
return $session->meta;
26+
}
27+
}
28+
29+
class RefreshTokenTest extends TestCase
30+
{
31+
public function test(){
32+
33+
$i = 0;
34+
35+
$config = [
36+
37+
// Database path
38+
'database' => '/local',
39+
40+
// Database endpoint
41+
'endpoint' => 'localhost:2136',
42+
43+
// Auto discovery (dedicated server only)
44+
'discovery' => false,
45+
46+
// IAM config
47+
'iam_config' => [
48+
'insecure' => true,
49+
],
50+
'credentials' => new TestCredentials()
51+
];
52+
$ydb = new Ydb($config);
53+
$table = $ydb->table();
54+
$session = $table->session();
55+
$token = MetaGetter::getMeta($session)["x-ydb-auth-ticket"];
56+
usleep(1e6);
57+
$session->query('select 1 as res');
58+
self::assertNotEquals(
59+
$token,
60+
MetaGetter::getMeta($session)["x-ydb-auth-ticket"]
61+
);
62+
}
63+
}

tests/RetryOnBadSessionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function test(){
4444
$session->delete();
4545
$session = $table->session();
4646
SessionManager::setSessionId($session,$oldSessionId);
47-
print_r($session->query('select 1 as res'));
4847
self::assertEquals(
4948
1,
5049
$session->query('select 1 as res')->rows()[0]['res']

0 commit comments

Comments
 (0)