Skip to content

Commit 2146eb9

Browse files
authored
feat: upgrade php-jwt version (#1049)
1 parent 952826a commit 2146eb9

8 files changed

Lines changed: 34 additions & 20 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
options: --health-cmd="pg_isready -h localhost" --health-interval=10s --health-timeout=5s --health-retries=5
3636
strategy:
3737
matrix:
38-
php: [ 7.1, 7.2, 7.3, 7.4, "8.0", 8.1, 8.2 ]
38+
php: [ 7.2, 7.3, 7.4, "8.0", 8.1, 8.2 ]
3939
name: "PHP ${{ matrix.php }} Unit Test"
4040
steps:
4141
- uses: actions/checkout@v2

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616
"psr-0": { "OAuth2": "src/" }
1717
},
1818
"require":{
19-
"php":">=7.1"
19+
"php":">=7.2"
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^7.5||^8.0",
2323
"aws/aws-sdk-php": "^2.8",
24-
"firebase/php-jwt": "^2.2",
24+
"firebase/php-jwt": "^6.4",
2525
"predis/predis": "^1.1",
2626
"thobbs/phpcassa": "dev-master",
27-
"mongodb/mongodb": "^1.1",
2827
"yoast/phpunit-polyfills": "^1.0"
2928
},
3029
"suggest": {
3130
"predis/predis": "Required to use Redis storage",
3231
"thobbs/phpcassa": "Required to use Cassandra storage",
3332
"aws/aws-sdk-php": "~2.8 is required to use DynamoDB storage",
34-
"firebase/php-jwt": "~2.2 is required to use JWT features",
33+
"firebase/php-jwt": "~v6.4 is required to use JWT features",
3534
"mongodb/mongodb": "^1.1 is required to use MongoDB storage"
3635
}
3736
}

phpunit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"
11-
syntaxCheck="false"
1211
bootstrap="test/bootstrap.php"
1312
>
1413
<testsuites>

src/OAuth2/Encryption/FirebaseJwt.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace OAuth2\Encryption;
44

5+
use Firebase\JWT\JWT;
6+
use Firebase\JWT\Key;
7+
58
/**
69
* Bridge file to use the firebase/php-jwt package for JWT encoding and decoding.
710
* @author Francis Chuang <francis.chuang@gmail.com>
@@ -10,38 +13,48 @@ class FirebaseJwt implements EncryptionInterface
1013
{
1114
public function __construct()
1215
{
13-
if (!class_exists('\JWT')) {
16+
if (!class_exists(JWT::class)) {
1417
throw new \ErrorException('firebase/php-jwt must be installed to use this feature. You can do this by running "composer require firebase/php-jwt"');
1518
}
1619
}
1720

1821
public function encode($payload, $key, $alg = 'HS256', $keyId = null)
1922
{
20-
return \JWT::encode($payload, $key, $alg, $keyId);
23+
return JWT::encode($payload, $key, $alg, $keyId);
2124
}
2225

2326
public function decode($jwt, $key = null, $allowedAlgorithms = null)
2427
{
2528
try {
26-
2729
//Maintain BC: Do not verify if no algorithms are passed in.
2830
if (!$allowedAlgorithms) {
29-
$key = null;
31+
$tks = \explode('.', $jwt);
32+
if (\count($tks) === 3) {
33+
[$headb64] = $tks;
34+
$headerRaw = JWT::urlsafeB64Decode($headb64);
35+
if (($header = JWT::jsonDecode($headerRaw))) {
36+
$key = new Key($key, $header->alg);
37+
}
38+
}
39+
} elseif(is_array($allowedAlgorithms)) {
40+
$key = new Key($key, $allowedAlgorithms[0]);
41+
} else {
42+
$key = new Key($key, $allowedAlgorithms);
3043
}
3144

32-
return (array)\JWT::decode($jwt, $key, $allowedAlgorithms);
45+
return (array) JWT::decode($jwt, $key);
3346
} catch (\Exception $e) {
3447
return false;
3548
}
3649
}
3750

3851
public function urlSafeB64Encode($data)
3952
{
40-
return \JWT::urlsafeB64Encode($data);
53+
return JWT::urlsafeB64Encode($data);
4154
}
4255

4356
public function urlSafeB64Decode($b64)
4457
{
45-
return \JWT::urlsafeB64Decode($b64);
58+
return JWT::urlsafeB64Decode($b64);
4659
}
4760
}

src/OAuth2/Storage/CouchbaseDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CouchbaseDB implements AuthorizationCodeInterface,
2828

2929
public function __construct($connection, $config = array())
3030
{
31-
if (! class_exists(Couchbase::class)) {
31+
if (!class_exists(Couchbase::class)) {
3232
throw new \RuntimeException('Missing Couchbase');
3333
}
3434

src/OAuth2/Storage/JwtAccessToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ protected function convertJwtToOAuth2($tokenData)
8484

8585
return $tokenData;
8686
}
87-
}
87+
}

src/OAuth2/Storage/MongoDB.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class MongoDB implements AuthorizationCodeInterface,
3232

3333
public function __construct($connection, $config = array())
3434
{
35+
if (!class_exists(Database::class) || !class_exists(Client::class)) {
36+
throw new \LogicException('Missing MongoDB php extension. Please install mongodb.so');
37+
}
3538
if ($connection instanceof Database) {
3639
$this->db = $connection;
3740
} else {

test/OAuth2/ResponseType/JwtAccessTokenTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ public function testCreateAccessToken()
4040
$this->assertEquals(3600, $delta);
4141
$this->assertEquals($decodedAccessToken['id'], $decodedAccessToken['jti']);
4242
}
43-
43+
4444
public function testExtraPayloadCallback()
4545
{
4646
$jwtconfig = array('jwt_extra_payload_callable' => function() {
4747
return array('custom_param' => 'custom_value');
4848
});
49-
49+
5050
$server = $this->getTestServer($jwtconfig);
5151
$jwtResponseType = $server->getResponseType('token');
52-
52+
5353
$accessToken = $jwtResponseType->createAccessToken('Test Client ID', 123, 'test', false);
5454
$jwt = new Jwt;
5555
$decodedAccessToken = $jwt->decode($accessToken['access_token'], null, false);
56-
56+
5757
$this->assertArrayHasKey('custom_param', $decodedAccessToken);
5858
$this->assertEquals('custom_value', $decodedAccessToken['custom_param']);
5959
}
@@ -162,7 +162,7 @@ private function getTestServer($jwtconfig = array())
162162
$memoryStorage = Bootstrap::getInstance()->getMemoryStorage();
163163

164164
$storage = array(
165-
'access_token' => new JwtAccessTokenStorage($memoryStorage),
165+
'access_token' => new JwtAccessTokenStorage($memoryStorage, $memoryStorage),
166166
'client' => $memoryStorage,
167167
'client_credentials' => $memoryStorage,
168168
);

0 commit comments

Comments
 (0)