Skip to content

Commit 6dab355

Browse files
authored
Added StaticAuthentication
Added StaticAuthentication
2 parents a6427a6 + 5b72b75 commit 6dab355

File tree

10 files changed

+215
-3
lines changed

10 files changed

+215
-3
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ jobs:
5757
- name: Install dependencies
5858
run: composer install --prefer-dist --no-progress
5959

60+
- name: Create YDB User
61+
run: docker exec $(docker ps --latest --quiet) /ydb -e grpc://localhost:2136 -d /local scripting yql -s "CREATE USER testuser PASSWORD 'test_password'"
62+
6063
- name: Run tests
6164
run: ./vendor/bin/phpunit \
6265
--coverage-text \

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* added StaticAuthentication
12
* added query timeout and canceled params
23

34
## 1.11.0

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ The following algorithm that is the same for YDB-PHP-SDK applies:
417417
4. Otherwise, if the value of the `YDB_ACCESS_TOKEN_CREDENTIALS` environment variable is set, the **Access token** authentication mode is used, where the this variable value is passed.
418418
5. Otherwise, the **Metadata** authentication mode is used.
419419

420+
## Static credentials
421+
422+
```php
423+
<?php
424+
425+
use YdbPlatform\Ydb\Ydb;
426+
use YdbPlatform\Ydb\Auth\Implement\StaticAuthentication;
427+
428+
$config = [
429+
430+
// Database path
431+
'database' => '/local',
432+
433+
// Database endpoint
434+
'endpoint' => 'localhost:2136',
435+
436+
// Auto discovery (dedicated server only)
437+
'discovery' => false,
438+
439+
// IAM config
440+
'iam_config' => [
441+
'insecure' => true,
442+
],
443+
444+
'credentials' => new StaticAuthentication($user, $password)
445+
];
446+
447+
$ydb = new Ydb($config);
448+
```
449+
420450
## Reading from text file
421451

422452
```php
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Auth\Implement;
4+
5+
use YdbPlatform\Ydb\Auth\IamAuth;
6+
use YdbPlatform\Ydb\Auth\TokenInfo;
7+
use YdbPlatform\Ydb\Auth\UseConfigInterface;
8+
use YdbPlatform\Ydb\Jwt\Jwt;
9+
use YdbPlatform\Ydb\Ydb;
10+
11+
class StaticAuthentication extends IamAuth implements UseConfigInterface
12+
{
13+
protected $user;
14+
protected $password;
15+
protected $token;
16+
/**
17+
* @var Ydb
18+
*/
19+
protected $ydb;
20+
21+
public function __construct(string $user, string $password)
22+
{
23+
$this->user = $user;
24+
$this->password = $password;
25+
}
26+
27+
public function getTokenInfo(): TokenInfo
28+
{
29+
$this->token = $this->ydb->auth()->getToken($this->user, $this->password);
30+
$jwtData = Jwt::decodeHeaderAndPayload($this->token);
31+
$expiresIn = $this->convertExpiresAt($jwtData['payload']['exp']);
32+
$ratio = $this->getRefreshTokenRatio();
33+
34+
return new TokenInfo($this->token, $expiresIn, $ratio);
35+
}
36+
37+
public function getName(): string
38+
{
39+
return "Static";
40+
}
41+
42+
public function setYdbConnectionConfig(array $config)
43+
{
44+
unset($config['credentials']);
45+
$config['credentials'] = new AnonymousAuthentication();
46+
$this->ydb = new Ydb($config, $this->logger);
47+
}
48+
}

src/Auth/UseConfigInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Auth;
4+
5+
interface UseConfigInterface
6+
{
7+
public function setYdbConnectionConfig(array $config);
8+
}

src/AuthService.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace YdbPlatform\Ydb;
3+
use Ydb\Auth\V1\AuthServiceClient as ServiceClient;
4+
class AuthService{
5+
6+
use Traits\RequestTrait;
7+
use Traits\ParseResultTrait;
8+
use Traits\LoggerTrait;
9+
10+
/**
11+
* @var ServiceClient
12+
*/
13+
protected $client;
14+
/**
15+
* @var
16+
*/
17+
protected $logger;
18+
/**
19+
* @var array|mixed
20+
*/
21+
protected $meta;
22+
/**
23+
* @var Iam
24+
*/
25+
protected $credentials;
26+
27+
public function __construct(Ydb $ydb, $logger)
28+
{
29+
$this->ydb = $ydb;
30+
$this->logger = $logger;
31+
$this->client = new ServiceClient($ydb->endpoint(), [
32+
'credentials' => $ydb->iam()->getCredentials(),
33+
]);
34+
$this->credentials = $ydb->iam();
35+
$this->meta = [
36+
'x-ydb-database' => [$ydb->database()],
37+
'x-ydb-sdk-build-info' => ['ydb-php-sdk/' . Ydb::VERSION],
38+
];;
39+
}
40+
41+
public function getToken(string $user, string $password){
42+
$data = [];
43+
$data["user"] = $user;
44+
$data["password"] = $password;
45+
$data["skip_get_token"] = true;
46+
return $this->doRequest('Auth', 'Login', $data)->getToken();
47+
}
48+
}

src/Jwt/Jwt.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,12 @@ public function urlEncode($value)
136136
}
137137
return str_replace('=', '', strtr(base64_encode($value), '+/', '-_'));
138138
}
139+
140+
public static function decodeHeaderAndPayload(string $jwtToken) : array {
141+
$paths = explode(".", $jwtToken);
142+
$result = [];
143+
$result["header"] = json_decode(base64_decode($paths[0]), true);
144+
$result["payload"] = json_decode(base64_decode($paths[1]), true);
145+
return $result;
146+
}
139147
}

src/Traits/RequestTrait.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ protected function doRequest($service, $method, array $data = [])
5757

5858
$this->checkDiscovery();
5959

60-
$this->meta['x-ydb-auth-ticket'] = [$this->credentials->token()];
60+
if(!empty($data["skip_get_token"])){
61+
unset($data["skip_get_token"]);
62+
} else {
63+
$this->meta['x-ydb-auth-ticket'] = [$this->credentials->token()];
64+
}
6165

6266
$this->saveLastRequest($service, $method, $data);
6367

@@ -119,7 +123,11 @@ protected function doStreamRequest($service, $method, $data = [])
119123
{
120124
$this->checkDiscovery();
121125

122-
$this->meta['x-ydb-auth-ticket'] = [$this->credentials->token()];
126+
if(!empty($data["skip_get_token"])){
127+
unset($data["skip_get_token"]);
128+
} else {
129+
$this->meta['x-ydb-auth-ticket'] = [$this->credentials->token()];
130+
}
123131

124132
if (method_exists($this, 'take')) {
125133
$this->take();

src/Ydb.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Psr\Log\LoggerInterface;
7+
use YdbPlatform\Ydb\Auth\UseConfigInterface;
78
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
89
use YdbPlatform\Ydb\Exceptions\RetryableException;
910
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
@@ -41,6 +42,11 @@ class Ydb
4142
*/
4243
protected $iam;
4344

45+
/**
46+
* @var AuthService
47+
*/
48+
protected $auth;
49+
4450
/**
4551
* @var Discovery
4652
*/
@@ -111,7 +117,10 @@ public function __construct($config = [], LoggerInterface $logger = null)
111117

112118
if(isset($config['credentials'])){
113119
$this->iam_config['credentials'] = $config['credentials'];
114-
$config['credentials']->setLogger($this->logger());
120+
$this->iam_config['credentials']->setLogger($this->logger());
121+
if ($this->iam_config['credentials'] instanceof UseConfigInterface){
122+
$this->iam_config['credentials']->setYdbConnectionConfig($config);
123+
}
115124
}
116125

117126
if (!empty($config['discovery']))
@@ -236,6 +245,19 @@ public function discovery()
236245
return $this->discovery;
237246
}
238247

248+
/**
249+
* @return AuthService
250+
*/
251+
public function auth()
252+
{
253+
if (!isset($this->auth))
254+
{
255+
$this->auth = new AuthService($this, $this->logger);
256+
}
257+
258+
return $this->auth;
259+
}
260+
239261
/**
240262
* @return Table
241263
*/

tests/StaticCredentialsTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AccessTokenAuthentication;
7+
use YdbPlatform\Ydb\Auth\Implement\StaticAuthentication;
8+
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
9+
use YdbPlatform\Ydb\Ydb;
10+
11+
class StaticCredentialsTest extends TestCase
12+
{
13+
public function testGetAuthToken()
14+
{
15+
$config = [
16+
17+
// Database path
18+
'database' => '/local',
19+
20+
// Database endpoint
21+
'endpoint' => 'localhost:2136',
22+
23+
// Auto discovery (dedicated server only)
24+
'discovery' => false,
25+
26+
// IAM config
27+
'iam_config' => [
28+
'insecure' => true,
29+
],
30+
'credentials' => new StaticAuthentication('testuser', 'test_password')
31+
];
32+
$ydb = new Ydb($config, new SimpleStdLogger(7));
33+
$ydb->table()->query("SELECT 1;");
34+
self::assertNotEquals("", $ydb->token());
35+
}
36+
}

0 commit comments

Comments
 (0)