Skip to content

Commit fd5d7c9

Browse files
author
Илья
committed
Added StaticAuthentication.php
1 parent 177a491 commit fd5d7c9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Auth/IamAuth.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ public function requestToken($request_data)
5757
throw new Exception('Failed to obtain new iamToken: response status is ' . $status);
5858
}
5959
}
60+
61+
public function decodeHeaderAndPayload(string $jwtToken) : array {
62+
$paths = explode(".", $jwtToken);
63+
$result = [];
64+
$result["header"] = json_decode(base64_decode($paths[0]), true);
65+
$result["payload"] = json_decode(base64_decode($paths[1]), true);
66+
return $result;
67+
}
6068
}

src/Auth/Implement/StaticAuthentication.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace YdbPlatform\Ydb\Auth\Implement;
44

5-
use YdbPlatform\Ydb\Auth\Auth;
5+
use YdbPlatform\Ydb\Auth\IamAuth;
66
use YdbPlatform\Ydb\Auth\TokenInfo;
77
use YdbPlatform\Ydb\Auth\UseConfigInterface;
8-
use YdbPlatform\Ydb\AuthService;
98
use YdbPlatform\Ydb\Ydb;
109

11-
class StaticAuthentication extends Auth implements UseConfigInterface
10+
class StaticAuthentication extends IamAuth implements UseConfigInterface
1211
{
1312
protected $user;
1413
protected $password;
@@ -27,10 +26,11 @@ public function __construct(string $user, string $password)
2726
public function getTokenInfo(): TokenInfo
2827
{
2928
$this->token = $this->ydb->auth()->getToken($this->user, $this->password);
30-
$expiresIn = 12*60*60;
29+
$jwtData = $this->decodeHeaderAndPayload($this->token);
30+
$expiresIn = $this->convertExpiresAt($jwtData['payload']['exp']);
3131
$ratio = $this->getRefreshTokenRatio();
3232

33-
return new TokenInfo($this->token, time()+$expiresIn, $ratio);
33+
return new TokenInfo($this->token, $expiresIn, $ratio);
3434
}
3535

3636
public function getName(): string
@@ -41,7 +41,7 @@ public function getName(): string
4141
public function setYdbConnectionConfig(array $config)
4242
{
4343
unset($config['credentials']);
44-
$config['credentials'] = new AccessTokenAuthentication('');
44+
$config['credentials'] = new AnonymousAuthentication();
4545
$this->ydb = new Ydb($config, $this->logger);
4646
}
4747
}

0 commit comments

Comments
 (0)