Skip to content

Commit ace29af

Browse files
authored
Merge branch 'main' into main
2 parents b650dbc + 8f718f4 commit ace29af

File tree

8 files changed

+83
-23
lines changed

8 files changed

+83
-23
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

CHANGELOG.md

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

3+
## [6.11.0](https://github.com/firebase/php-jwt/compare/v6.10.2...v6.11.0) (2025-01-23)
4+
5+
6+
### Features
7+
8+
* support octet typed JWK ([#587](https://github.com/firebase/php-jwt/issues/587)) ([7cb8a26](https://github.com/firebase/php-jwt/commit/7cb8a265fa81edf2fa6ef8098f5bc5ae573c33ad))
9+
10+
11+
### Bug Fixes
12+
13+
* 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))
14+
15+
## [6.10.2](https://github.com/firebase/php-jwt/compare/v6.10.1...v6.10.2) (2024-11-24)
16+
17+
18+
### Bug Fixes
19+
20+
* Mitigate PHP8.4 deprecation warnings ([#570](https://github.com/firebase/php-jwt/issues/570)) ([76808fa](https://github.com/firebase/php-jwt/commit/76808fa227f3811aa5cdb3bf81233714b799a5b5))
21+
* support php 8.4 ([#583](https://github.com/firebase/php-jwt/issues/583)) ([e3d68b0](https://github.com/firebase/php-jwt/commit/e3d68b044421339443c74199edd020e03fb1887e))
22+
323
## [6.10.1](https://github.com/firebase/php-jwt/compare/v6.10.0...v6.10.1) (2024-05-18)
424

525

README.md

Lines changed: 2 additions & 1 deletion
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
/*

src/JWK.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/JWT.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static function encode(
204204
?array $head = null
205205
): string {
206206
$header = ['typ' => 'JWT'];
207-
if (isset($head) && \is_array($head)) {
207+
if (isset($head)) {
208208
$header = \array_merge($header, $head);
209209
}
210210
$header['alg'] = $alg;
@@ -387,12 +387,7 @@ public static function jsonDecode(string $input)
387387
*/
388388
public static function jsonEncode(array $input): string
389389
{
390-
if (PHP_VERSION_ID >= 50400) {
391-
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
392-
} else {
393-
// PHP 5.3 only
394-
$json = \json_encode($input);
395-
}
390+
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
396391
if ($errno = \json_last_error()) {
397392
self::handleJsonError($errno);
398393
} 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,31 @@ public function testDecodeByMultiJwkKeySet()
170170
$this->assertSame('bar', $result->sub);
171171
}
172172

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+
173198
public function testParseKey()
174199
{
175200
// Use a known module and exponent, and ensure it parses as expected

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)