Skip to content

Commit 014056b

Browse files
authored
Merge branch 'main' into consistent-error-text
2 parents 5d57672 + 1b9e871 commit 014056b

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 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: [ "7.4", "8.0", "8.1", "8.2" ]
13+
php: [ "7.4", "8.0", "8.1", "8.2", "8.3" ]
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.0"
38+
php-version: "8.2"
3939
- name: Run Script
4040
run: |
4141
composer global require friendsofphp/php-cs-fixer
@@ -49,7 +49,7 @@ jobs:
4949
- name: Install PHP
5050
uses: shivammathur/setup-php@v2
5151
with:
52-
php-version: '8.0'
52+
php-version: '8.2'
5353
- name: Run Script
5454
run: |
5555
composer install

CHANGELOG.md

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

3+
## [6.10.0](https://github.com/firebase/php-jwt/compare/v6.9.0...v6.10.0) (2023-11-28)
4+
5+
6+
### Features
7+
8+
* allow typ header override ([#546](https://github.com/firebase/php-jwt/issues/546)) ([79cb30b](https://github.com/firebase/php-jwt/commit/79cb30b729a22931b2fbd6b53f20629a83031ba9))
9+
310
## [6.9.0](https://github.com/firebase/php-jwt/compare/v6.8.1...v6.9.0) (2023-10-04)
411

512

src/CachedKeySet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private function rateLimitExceeded(): bool
213213

214214
$cacheItem = $this->cache->getItem($this->rateLimitCacheKey);
215215
if (!$cacheItem->isHit()) {
216-
$cacheItem->expiresAfter(1); // # of calls are cached each minute
216+
$cacheItem->expiresAfter(60); // # of calls are cached each minute
217217
}
218218

219219
$callsPerMinute = (int) $cacheItem->get();

src/JWT.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,14 @@ public static function encode(
203203
string $keyId = null,
204204
array $head = null
205205
): string {
206-
$header = ['typ' => 'JWT', 'alg' => $alg];
206+
$header = ['typ' => 'JWT'];
207+
if (isset($head) && \is_array($head)) {
208+
$header = \array_merge($header, $head);
209+
}
210+
$header['alg'] = $alg;
207211
if ($keyId !== null) {
208212
$header['kid'] = $keyId;
209213
}
210-
if (isset($head) && \is_array($head)) {
211-
$header = \array_merge($head, $header);
212-
}
213214
$segments = [];
214215
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($header));
215216
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($payload));

tests/JWTTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,26 @@ public function testGetHeaders()
518518
$this->assertEquals($headers->typ, 'JWT');
519519
$this->assertEquals($headers->alg, 'HS256');
520520
}
521+
522+
public function testAdditionalHeaderOverrides()
523+
{
524+
$msg = JWT::encode(
525+
['message' => 'abc'],
526+
'my_key',
527+
'HS256',
528+
'my_key_id',
529+
[
530+
'cty' => 'test-eit;v=1',
531+
'typ' => 'JOSE', // override type header
532+
'kid' => 'not_my_key_id', // should not override $key param
533+
'alg' => 'BAD', // should not override $alg param
534+
]
535+
);
536+
$headers = new stdClass();
537+
JWT::decode($msg, new Key('my_key', 'HS256'), $headers);
538+
$this->assertEquals('test-eit;v=1', $headers->cty, 'additional field works');
539+
$this->assertEquals('JOSE', $headers->typ, 'typ override works');
540+
$this->assertEquals('my_key_id', $headers->kid, 'key param not overridden');
541+
$this->assertEquals('HS256', $headers->alg, 'alg param not overridden');
542+
}
521543
}

0 commit comments

Comments
 (0)