Skip to content

Commit 2d901bd

Browse files
authored
Merge branch 'main' into main
2 parents f4b20e0 + 43d70ae commit 2d901bd

File tree

10 files changed

+144
-38
lines changed

10 files changed

+144
-38
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
php: [ "8.0", "8.1", "8.2", "8.3" ]
13+
php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ]
1414
name: PHP ${{matrix.php }} Unit Test
1515
steps:
1616
- uses: actions/checkout@v2
@@ -35,7 +35,7 @@ jobs:
3535
- name: Setup PHP
3636
uses: shivammathur/setup-php@v2
3737
with:
38-
php-version: "8.2"
38+
php-version: "8.3"
3939
- name: Run Script
4040
run: |
4141
composer global require friendsofphp/php-cs-fixer
@@ -49,9 +49,9 @@ jobs:
4949
- name: Install PHP
5050
uses: shivammathur/setup-php@v2
5151
with:
52-
php-version: '8.2'
52+
php-version: '8.3'
5353
- name: Run Script
5454
run: |
5555
composer install
56-
composer global require phpstan/phpstan
56+
composer global require phpstan/phpstan:~1.10.0
5757
~/.composer/vendor/bin/phpstan analyse

.php-cs-fixer.dist.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
'native_function_invocation' => [
1717
'strict' => false
1818
],
19+
'nullable_type_declaration' => [
20+
'syntax' => 'question_mark',
21+
],
22+
'nullable_type_declaration_for_default_null_value' => true,
1923
])
2024
->setFinder(
2125
PhpCsFixer\Finder::create()

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# Changelog
22

3+
## [6.11.1](https://github.com/firebase/php-jwt/compare/v6.11.0...v6.11.1) (2025-04-09)
4+
5+
6+
### Bug Fixes
7+
8+
* update error text for consistency ([#528](https://github.com/firebase/php-jwt/issues/528)) ([c11113a](https://github.com/firebase/php-jwt/commit/c11113afa13265e016a669e75494b9203b8a7775))
9+
10+
## [6.11.0](https://github.com/firebase/php-jwt/compare/v6.10.2...v6.11.0) (2025-01-23)
11+
12+
13+
### Features
14+
15+
* support octet typed JWK ([#587](https://github.com/firebase/php-jwt/issues/587)) ([7cb8a26](https://github.com/firebase/php-jwt/commit/7cb8a265fa81edf2fa6ef8098f5bc5ae573c33ad))
16+
17+
18+
### Bug Fixes
19+
20+
* refactor constructor Key to use PHP 8.0 syntax ([#577](https://github.com/firebase/php-jwt/issues/577)) ([29fa2ce](https://github.com/firebase/php-jwt/commit/29fa2ce9e0582cd397711eec1e80c05ce20fabca))
21+
22+
## [6.10.2](https://github.com/firebase/php-jwt/compare/v6.10.1...v6.10.2) (2024-11-24)
23+
24+
25+
### Bug Fixes
26+
27+
* Mitigate PHP8.4 deprecation warnings ([#570](https://github.com/firebase/php-jwt/issues/570)) ([76808fa](https://github.com/firebase/php-jwt/commit/76808fa227f3811aa5cdb3bf81233714b799a5b5))
28+
* support php 8.4 ([#583](https://github.com/firebase/php-jwt/issues/583)) ([e3d68b0](https://github.com/firebase/php-jwt/commit/e3d68b044421339443c74199edd020e03fb1887e))
29+
330
## [6.10.1](https://github.com/firebase/php-jwt/compare/v6.10.0...v6.10.1) (2024-05-18)
431

532

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
4848
print_r($decoded);
4949

5050
// Pass a stdClass in as the third parameter to get the decoded header values
51-
$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers = new stdClass());
51+
$headers = new stdClass();
52+
$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers);
5253
print_r($headers);
5354

5455
/*
@@ -290,7 +291,7 @@ $jwks = ['keys' => []];
290291

291292
// JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\JWT\Key
292293
// objects. Pass this as the second parameter to JWT::decode.
293-
JWT::decode($payload, JWK::parseKeySet($jwks));
294+
JWT::decode($jwt, JWK::parseKeySet($jwks));
294295
```
295296

296297
Using Cached Key Sets
@@ -349,7 +350,7 @@ use InvalidArgumentException;
349350
use UnexpectedValueException;
350351

351352
try {
352-
$decoded = JWT::decode($payload, $keys);
353+
$decoded = JWT::decode($jwt, $keys);
353354
} catch (InvalidArgumentException $e) {
354355
// provided key/key-array is empty or malformed.
355356
} catch (DomainException $e) {
@@ -379,7 +380,7 @@ like this:
379380
use Firebase\JWT\JWT;
380381
use UnexpectedValueException;
381382
try {
382-
$decoded = JWT::decode($payload, $keys);
383+
$decoded = JWT::decode($jwt, $keys);
383384
} catch (LogicException $e) {
384385
// errors having to do with environmental setup or malformed JWT Keys
385386
} catch (UnexpectedValueException $e) {
@@ -394,7 +395,7 @@ instead, you can do the following:
394395

395396
```php
396397
// return type is stdClass
397-
$decoded = JWT::decode($payload, $keys);
398+
$decoded = JWT::decode($jwt, $keys);
398399

399400
// cast to array
400401
$decoded = json_decode(json_encode($decoded), true);

src/CachedKeySet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public function __construct(
8080
ClientInterface $httpClient,
8181
RequestFactoryInterface $httpFactory,
8282
CacheItemPoolInterface $cache,
83-
int $expiresAfter = null,
83+
?int $expiresAfter = null,
8484
bool $rateLimit = false,
85-
string $defaultAlg = null
85+
?string $defaultAlg = null
8686
) {
8787
$this->jwksUri = $jwksUri;
8888
$this->httpClient = $httpClient;
@@ -180,7 +180,7 @@ private function keyIdExists(string $keyId): bool
180180
$jwksResponse = $this->httpClient->sendRequest($request);
181181
if ($jwksResponse->getStatusCode() !== 200) {
182182
throw new UnexpectedValueException(
183-
sprintf('HTTP Error: %d %s for URI "%s"',
183+
\sprintf('HTTP Error: %d %s for URI "%s"',
184184
$jwksResponse->getStatusCode(),
185185
$jwksResponse->getReasonPhrase(),
186186
$this->jwksUri,

src/JWK.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class JWK
5252
*
5353
* @uses parseKey
5454
*/
55-
public static function parseKeySet(array $jwks, string $defaultAlg = null): array
55+
public static function parseKeySet(array $jwks, ?string $defaultAlg = null): array
5656
{
5757
$keys = [];
5858

@@ -93,7 +93,7 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
9393
*
9494
* @uses createPemFromModulusAndExponent
9595
*/
96-
public static function parseKey(array $jwk, string $defaultAlg = null): ?Key
96+
public static function parseKey(array $jwk, ?string $defaultAlg = null): ?Key
9797
{
9898
if (empty($jwk)) {
9999
throw new InvalidArgumentException('JWK must not be empty');
@@ -172,6 +172,12 @@ public static function parseKey(array $jwk, string $defaultAlg = null): ?Key
172172
// This library works internally with EdDSA keys (Ed25519) encoded in standard base64.
173173
$publicKey = JWT::convertBase64urlToBase64($jwk['x']);
174174
return new Key($publicKey, $jwk['alg']);
175+
case 'oct':
176+
if (!isset($jwk['k'])) {
177+
throw new UnexpectedValueException('k not set');
178+
}
179+
180+
return new Key(JWT::urlsafeB64Decode($jwk['k']), $jwk['alg']);
175181
default:
176182
break;
177183
}
@@ -212,7 +218,7 @@ private static function createPemFromCrvAndXYCoordinates(string $crv, string $x,
212218
)
213219
);
214220

215-
return sprintf(
221+
return \sprintf(
216222
"-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n",
217223
wordwrap(base64_encode($pem), 64, "\n", true)
218224
);

src/JWT.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class JWT
9696
public static function decode(
9797
string $jwt,
9898
$keyOrKeyArray,
99-
stdClass &$headers = null
99+
?stdClass &$headers = null
100100
): stdClass {
101101
// Validate JWT
102102
$timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp;
@@ -164,7 +164,7 @@ public static function decode(
164164
// token can actually be used. If it's not yet that time, abort.
165165
if (isset($payload->nbf) && floor($payload->nbf) > ($timestamp + static::$leeway)) {
166166
$ex = new BeforeValidException(
167-
'Cannot handle token with nbf prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
167+
'Cannot handle token with nbf prior to ' . \date(DateTime::ATOM, (int) floor($payload->nbf))
168168
);
169169
$ex->setPayload($payload);
170170
throw $ex;
@@ -175,7 +175,7 @@ public static function decode(
175175
// correctly used the nbf claim).
176176
if (!isset($payload->nbf) && isset($payload->iat) && floor($payload->iat) > ($timestamp + static::$leeway)) {
177177
$ex = new BeforeValidException(
178-
'Cannot handle token with iat prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
178+
'Cannot handle token with iat prior to ' . \date(DateTime::ATOM, (int) floor($payload->iat))
179179
);
180180
$ex->setPayload($payload);
181181
throw $ex;
@@ -210,11 +210,11 @@ public static function encode(
210210
array $payload,
211211
$key,
212212
string $alg,
213-
string $keyId = null,
214-
array $head = null
213+
?string $keyId = null,
214+
?array $head = null
215215
): string {
216216
$header = ['typ' => 'JWT'];
217-
if (isset($head) && \is_array($head)) {
217+
if (isset($head)) {
218218
$header = \array_merge($header, $head);
219219
}
220220
$header['alg'] = $alg;
@@ -397,12 +397,7 @@ public static function jsonDecode(string $input)
397397
*/
398398
public static function jsonEncode(array $input): string
399399
{
400-
if (PHP_VERSION_ID >= 50400) {
401-
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
402-
} else {
403-
// PHP 5.3 only
404-
$json = \json_encode($input);
405-
}
400+
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
406401
if ($errno = \json_last_error()) {
407402
self::handleJsonError($errno);
408403
} elseif ($json === 'null') {

src/Key.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99

1010
class Key
1111
{
12-
/** @var string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate */
13-
private $keyMaterial;
14-
/** @var string */
15-
private $algorithm;
16-
1712
/**
1813
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $keyMaterial
1914
* @param string $algorithm
2015
*/
2116
public function __construct(
22-
$keyMaterial,
23-
string $algorithm
17+
private $keyMaterial,
18+
private string $algorithm
2419
) {
2520
if (
2621
!\is_string($keyMaterial)
@@ -38,10 +33,6 @@ public function __construct(
3833
if (empty($algorithm)) {
3934
throw new InvalidArgumentException('Algorithm must not be empty');
4035
}
41-
42-
// TODO: Remove in PHP 8.0 in favor of class constructor property promotion
43-
$this->keyMaterial = $keyMaterial;
44-
$this->algorithm = $algorithm;
4536
}
4637

4738
/**

tests/JWKTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,64 @@ public function testDecodeByMultiJwkKeySet()
169169

170170
$this->assertSame('bar', $result->sub);
171171
}
172+
173+
public function testDecodeByOctetJwkKeySet()
174+
{
175+
$jwkSet = json_decode(
176+
file_get_contents(__DIR__ . '/data/octet-jwkset.json'),
177+
true
178+
);
179+
$keys = JWK::parseKeySet($jwkSet);
180+
$payload = ['sub' => 'foo', 'exp' => strtotime('+10 seconds')];
181+
foreach ($keys as $keyId => $key) {
182+
$msg = JWT::encode($payload, $key->getKeyMaterial(), $key->getAlgorithm(), $keyId);
183+
$result = JWT::decode($msg, $keys);
184+
185+
$this->assertSame('foo', $result->sub);
186+
}
187+
}
188+
189+
public function testOctetJwkMissingK()
190+
{
191+
$this->expectException(UnexpectedValueException::class);
192+
$this->expectExceptionMessage('k not set');
193+
194+
$badJwk = ['kty' => 'oct', 'alg' => 'HS256'];
195+
$keys = JWK::parseKeySet(['keys' => [$badJwk]]);
196+
}
197+
198+
public function testParseKey()
199+
{
200+
// Use a known module and exponent, and ensure it parses as expected
201+
$jwk = [
202+
'alg' => 'RS256',
203+
'kty' => 'RSA',
204+
'n' => 'hsYvCPtkUV7SIxwkOkJsJfhwV_CMdXU5i0UmY2QEs-Pa7v0-0y-s4EjEDtsQ8Yow6hc670JhkGBcMzhU4DtrqNGROXebyOse5FX0m0UvWo1qXqNTf28uBKB990mY42Icr8sGjtOw8ajyT9kufbmXi3eZKagKpG0TDGK90oBEfoGzCxoFT87F95liNth_GoyU5S8-G3OqIqLlQCwxkI5s-g2qvg_aooALfh1rhvx2wt4EJVMSrdnxtPQSPAtZBiw5SwCnVglc6OnalVNvAB2JArbqC9GAzzz9pApAk28SYg5a4hPiPyqwRv-4X1CXEK8bO5VesIeRX0oDf7UoM-pVAw',
205+
'use' => 'sig',
206+
'e' => 'AQAB',
207+
'kid' => '838c06c62046c2d948affe137dd5310129f4d5d1'
208+
];
209+
210+
$key = JWK::parseKey($jwk);
211+
$this->assertNotNull($key);
212+
213+
$openSslKey = $key->getKeyMaterial();
214+
$pubKey = openssl_pkey_get_public($openSslKey);
215+
$keyData = openssl_pkey_get_details($pubKey);
216+
217+
$expectedPublicKey = <<<EOF
218+
-----BEGIN PUBLIC KEY-----
219+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhsYvCPtkUV7SIxwkOkJs
220+
JfhwV/CMdXU5i0UmY2QEs+Pa7v0+0y+s4EjEDtsQ8Yow6hc670JhkGBcMzhU4Dtr
221+
qNGROXebyOse5FX0m0UvWo1qXqNTf28uBKB990mY42Icr8sGjtOw8ajyT9kufbmX
222+
i3eZKagKpG0TDGK90oBEfoGzCxoFT87F95liNth/GoyU5S8+G3OqIqLlQCwxkI5s
223+
+g2qvg/aooALfh1rhvx2wt4EJVMSrdnxtPQSPAtZBiw5SwCnVglc6OnalVNvAB2J
224+
ArbqC9GAzzz9pApAk28SYg5a4hPiPyqwRv+4X1CXEK8bO5VesIeRX0oDf7UoM+pV
225+
AwIDAQAB
226+
-----END PUBLIC KEY-----
227+
228+
EOF;
229+
230+
$this->assertEquals($expectedPublicKey, $keyData['key']);
231+
}
172232
}

tests/data/octet-jwkset.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"keys": [
3+
{
4+
"kty": "oct",
5+
"alg": "HS256",
6+
"kid": "jwk1",
7+
"k": "xUNfVvQ-WdmXB9qp6qK0SrG-yKW4AJqmcSP66Gm2TrE"
8+
},
9+
{
10+
"kty": "oct",
11+
"alg": "HS384",
12+
"kid": "jwk2",
13+
"k": "z7990HoD72QDX9JKqeQc3l7EtXutco72j2YulZMjeakFVDbFGXGDFG4awOF7eu9l"
14+
},
15+
{
16+
"kty": "oct",
17+
"alg": "HS512",
18+
"kid": "jwk3",
19+
"k": "EmYGSDG5W1UjkPIL7LelG-QMVtsXn7bz5lUxBrkqq3kdFEzkLWVGrXKpZxRe7YcApCe0d4s9lXRQtn5Nzaf49w"
20+
}
21+
]
22+
}

0 commit comments

Comments
 (0)