Skip to content

Commit 8c88ee3

Browse files
authored
Merge pull request #45 Add IAM access authentication
2 parents e2afcca + 08ff2cf commit 8c88ee3

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# CHANGELOG
2-
1+
* added access token authentication
32

43
## 1.5.0 (2023-02-22)
54

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ YDB supports the following authentication methods:
3434
- Metadata URL
3535
- Anonymous
3636

37+
## Access token
38+
39+
Use your access token:
40+
41+
```php
42+
<?php
43+
44+
use YdbPlatform\Ydb\Ydb;
45+
46+
$config = [
47+
48+
// Database path
49+
'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx',
50+
51+
// Database endpoint
52+
'endpoint' => 'ydb.serverless.yandexcloud.net:2135',
53+
54+
// Auto discovery (dedicated server only)
55+
'discovery' => false,
56+
57+
// IAM config
58+
'iam_config' => [
59+
'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!)
60+
61+
// Access token authentication
62+
'access_token' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
63+
],
64+
];
65+
66+
$ydb = new Ydb($config);
67+
```
68+
3769
## OAuth token
3870

3971
You should obtain [a new OAuth token](https://cloud.yandex.com/docs/iam/concepts/authorization/oauth-token).
@@ -67,7 +99,6 @@ $config = [
6799
];
68100

69101
$ydb = new Ydb($config);
70-
71102
```
72103

73104
## JWT + private key

src/Iam.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public function config($key, $default = null)
7373
*/
7474
public function token($force = false)
7575
{
76-
if ($force || !($token = $this->loadToken()))
76+
if ($token = $this->config('access_token')){
77+
return $token;
78+
}
79+
else if ($force || !($token = $this->loadToken()))
7780
{
7881
$token = $this->newToken();
7982
}
@@ -184,6 +187,7 @@ protected function parseConfig(array $config)
184187
$stringParams = [
185188
'temp_dir',
186189
'root_cert_file',
190+
'access_token',
187191
'oauth_token',
188192
'key_id',
189193
'service_account_id',
@@ -272,6 +276,9 @@ protected function initConfig()
272276
throw new Exception('Private key [' . $privateKeyFile . '] is missing.');
273277
}
274278
}
279+
else if ($this->config('access_token')){
280+
$this->logger()->info('YDB: Authentication method: Access token');
281+
}
275282
else if ($this->config('oauth_token'))
276283
{
277284
$this->logger()->info('YDB: Authentication method: OAuth token');

0 commit comments

Comments
 (0)