Skip to content

Commit ee61d10

Browse files
committed
jwt replacement
1 parent b5802fe commit ee61d10

File tree

4 files changed

+143
-102
lines changed

4 files changed

+143
-102
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"ext-json": "*",
1717
"google/protobuf": "~3.15.8",
1818
"grpc/grpc": "^1.35",
19-
"lcobucci/jwt": "~4.1.5",
2019
"phpseclib/phpseclib": "^2.0|^3.0",
2120
"psr/log": "^1|^2|^3"
2221
},

src/Iam.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace YdbPlatform\Ydb;
44

55
use DateTime;
6-
use Lcobucci\JWT;
76
use DateTimeImmutable;
87
use Grpc\ChannelCredentials;
98
use Psr\Log\LoggerInterface;
10-
use YdbPlatform\Ydb\Jwt\Signer\Sha256;
119
use YdbPlatform\Ydb\Contracts\IamTokenContract;
1210

1311
use function filter_var;
@@ -98,7 +96,7 @@ public function newToken()
9896
{
9997
$token = $this->getJwtToken();
10098
$request_data = [
101-
'jwt' => $token->toString(),
99+
'jwt' => $token,
102100
];
103101
}
104102
else
@@ -285,27 +283,18 @@ protected function initConfig()
285283
}
286284

287285
/**
288-
* @return JWT\Token
286+
* @return string
289287
*/
290288
protected function getJwtToken()
291289
{
292290
$now = new DateTimeImmutable;
293291

294-
$key = JWT\Signer\Key\InMemory::plainText($this->config('private_key'));
295-
296-
$config = JWT\Configuration::forSymmetricSigner(
297-
new Sha256,
298-
$key
299-
);
300-
301-
$token = $config->builder()
292+
$token = (new Jwt\Jwt($this->config('private_key'), $this->config('key_id')))
302293
->issuedBy($this->config('service_account_id'))
303294
->issuedAt($now)
304295
->expiresAt($now->modify('+1 hour'))
305296
->permittedFor(static::IAM_TOKEN_API_URL)
306-
->withHeader('typ', 'JWT')
307-
->withHeader('kid', $this->config('key_id'))
308-
->getToken($config->signer(), $config->signingKey());
297+
->getToken();
309298

310299
return $token;
311300
}

src/Jwt/Jwt.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Jwt;
4+
5+
use DateTimeInterface;
6+
7+
use phpseclib\Crypt\RSA as LegacyRSA;
8+
use phpseclib3\Crypt\RSA;
9+
use phpseclib3\Crypt\PublicKeyLoader;
10+
11+
class Jwt
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $header = [
17+
'typ' => 'JWT',
18+
'alg' => 'PS256',
19+
];
20+
21+
/**
22+
* @var array
23+
*/
24+
protected $payload = [];
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $privateKey;
30+
31+
/**
32+
* @param string $privateKey
33+
* @param string $keyId
34+
*/
35+
public function __construct($privateKey, $keyId)
36+
{
37+
$this->privateKey = $privateKey;
38+
$this->header['kid'] = $keyId;
39+
}
40+
41+
/**
42+
* @param string $value
43+
* @return $this
44+
*/
45+
public function issuedBy($value)
46+
{
47+
$this->payload['iss'] = $value;
48+
return $this;
49+
}
50+
51+
/**
52+
* @param DateTimeInterface $value
53+
* @return $this
54+
*/
55+
public function issuedAt(DateTimeInterface $value)
56+
{
57+
$this->payload['iat'] = $value->format('U');
58+
return $this;
59+
}
60+
61+
/**
62+
* @param DateTimeInterface $value
63+
* @return $this
64+
*/
65+
public function expiresAt(DateTimeInterface $value)
66+
{
67+
$this->payload['exp'] = $value->format('U');
68+
return $this;
69+
}
70+
71+
/**
72+
* @param string $value
73+
* @return $this
74+
*/
75+
public function permittedFor($value)
76+
{
77+
$this->payload['aud'] = $value;
78+
return $this;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getToken()
85+
{
86+
$segments = [];
87+
88+
$segments[] = $this->urlEncode($this->header);
89+
$segments[] = $this->urlEncode($this->payload);
90+
$segments[] = $this->urlEncode($this->sign(implode('.', $segments)));
91+
92+
return implode('.', $segments);
93+
}
94+
95+
/**
96+
* @param string
97+
* @return string
98+
*/
99+
public function sign($input)
100+
{
101+
if (class_exists(LegacyRSA::class))
102+
{
103+
$rsa = new LegacyRSA;
104+
$rsa->loadKey($this->privateKey);
105+
$rsa->setHash('sha256');
106+
$rsa->setMGFHash('sha256');
107+
$rsa->setSignatureMode(LegacyRSA::SIGNATURE_PSS);
108+
}
109+
else
110+
{
111+
$rsa = PublicKeyLoader::load($this->privateKey);
112+
$rsa->withPadding(RSA::SIGNATURE_PSS);
113+
}
114+
115+
return $rsa->sign($input);
116+
}
117+
118+
/**
119+
* @param array $value
120+
* @return string
121+
*/
122+
public function jsonEncode($value)
123+
{
124+
return json_encode($value, JSON_UNESCAPED_SLASHES);
125+
}
126+
127+
/**
128+
* @param string|array $value
129+
* @return string
130+
*/
131+
public function urlEncode($value)
132+
{
133+
if (is_array($value))
134+
{
135+
$value = $this->jsonEncode($value);
136+
}
137+
return str_replace('=', '', strtr(base64_encode($value), '+/', '-_'));
138+
}
139+
}

src/Jwt/Signer/Sha256.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)