Skip to content

Commit 6c6291b

Browse files
authored
Merge pull request #500 from cakephp/2.next
Bump firebase/php-jwt to v5.5.
2 parents 9dc2d73 + 5bfd36e commit 6c6291b

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require-dev": {
2121
"cakephp/cakephp": "^4.0",
2222
"cakephp/cakephp-codesniffer": "^4.0",
23-
"firebase/php-jwt": "^5.0",
23+
"firebase/php-jwt": "^5.5",
2424
"phpunit/phpunit": "^8.5 || ^9.3"
2525
},
2626
"suggest": {

docs/en/authenticators.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ example.
102102
- **queryParam**: The query param to check for the token. The default
103103
is ``token``.
104104
- **tokenPrefix**: The token prefix. Default is ``bearer``.
105-
- **algorithms**: An array of hashing algorithms for Firebase JWT.
106-
Default is an array ``['HS256']``.
105+
- **algorithm**: The hashing algorithm for Firebase JWT.
106+
Default is ``'HS256'``.
107107
- **returnPayload**: To return or not return the token payload directly
108108
without going through the identifiers. Default is ``true``.
109109
- **secretKey**: Default is ``null`` but you’re **required** to pass a
110110
secret key if you’re not in the context of a CakePHP application that
111111
provides it through ``Security::salt()``.
112112

113+
You need to add the lib `firebase/php-jwt <https://github.com/firebase/php-jwt>`_
114+
v5.5 or above to your app to use the ``JwtAuthenticator``.
115+
113116
By default the ``JwtAuthenticator`` uses ``HS256`` symmetric key algorithm and uses
114117
the value of ``Cake\Utility\Security::salt()`` as encryption key.
115118
For enhanced security one can instead use the ``RS256`` asymmetric key algorithm.
@@ -137,7 +140,7 @@ Add the following to your ``Application`` class::
137140
$service->loadIdentifier('Authentication.JwtSubject');
138141
$service->loadAuthenticator('Authentication.Jwt', [
139142
'secretKey' => file_get_contents(CONFIG . '/jwt.pem'),
140-
'algorithms' => ['RS256'],
143+
'algorithm' => 'RS256',
141144
'returnPayload' => false
142145
]);
143146
}
@@ -209,7 +212,7 @@ See https://en.wikipedia.org/wiki/Basic_access_authentication
209212

210213
.. note::
211214

212-
This authenticator will halt the request when authentication credentials are missing or invalid.
215+
This authenticator will halt the request when authentication credentials are missing or invalid.
213216

214217
Configuration options:
215218

@@ -223,7 +226,7 @@ See https://en.wikipedia.org/wiki/Digest_access_authentication
223226

224227
.. note::
225228

226-
This authenticator will halt the request when authentication credentials are missing or invalid.
229+
This authenticator will halt the request when authentication credentials are missing or invalid.
227230

228231
Configuration options:
229232

src/Authenticator/JwtAuthenticator.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Cake\Utility\Security;
2222
use Exception;
2323
use Firebase\JWT\JWT;
24+
use Firebase\JWT\Key;
2425
use Psr\Http\Message\ServerRequestInterface;
2526
use RuntimeException;
2627
use stdClass;
@@ -34,7 +35,7 @@ class JwtAuthenticator extends TokenAuthenticator
3435
'header' => 'Authorization',
3536
'queryParam' => 'token',
3637
'tokenPrefix' => 'bearer',
37-
'algorithms' => ['HS256'],
38+
'algorithm' => 'HS256',
3839
'returnPayload' => true,
3940
'secretKey' => null,
4041
'subjectKey' => IdentifierInterface::CREDENTIAL_JWT_SUBJECT,
@@ -64,6 +65,14 @@ public function __construct(IdentifierInterface $identifier, array $config = [])
6465
}
6566
$this->setConfig('secretKey', \Cake\Utility\Security::getSalt());
6667
}
68+
69+
if (isset($config['algorithms'])) {
70+
deprecationWarning(
71+
'The `algorithms` array config is deprecated, use the `algorithm` string config instead.'
72+
. ' This is due to the new recommended usage of `firebase/php-jwt`.'
73+
. 'See https://github.com/firebase/php-jwt/releases/tag/v5.5.0'
74+
);
75+
}
6776
}
6877

6978
/**
@@ -148,10 +157,17 @@ public function getPayload(?ServerRequestInterface $request = null): ?object
148157
*/
149158
protected function decodeToken(string $token): ?object
150159
{
151-
return JWT::decode(
152-
$token,
153-
$this->getConfig('secretKey'),
154-
$this->getConfig('algorithms')
155-
);
160+
$algorithms = $this->getConfig('algorithms');
161+
if ($algorithms) {
162+
return JWT::decode(
163+
$token,
164+
$this->getConfig('secretKey'),
165+
$algorithms
166+
);
167+
}
168+
169+
$key = new Key($this->getConfig('secretKey'), $this->getConfig('algorithm'));
170+
171+
return JWT::decode($token, $key);
156172
}
157173
}

tests/TestCase/Authenticator/JwtAuthenticatorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ public function testAuthenticateViaHeaderToken()
107107
$this->assertInstanceOf(ArrayAccess::class, $result->getData());
108108
}
109109

110+
/**
111+
* @deprecated
112+
*/
113+
public function testUsingDeprecatedConfig()
114+
{
115+
$this->request = ServerRequestFactory::fromGlobals(
116+
['REQUEST_URI' => '/']
117+
);
118+
$this->request = $this->request->withAddedHeader('Authorization', 'Bearer ' . $this->token);
119+
120+
$this->deprecated(function () {
121+
$authenticator = new JwtAuthenticator($this->identifiers, [
122+
'secretKey' => 'secretKey',
123+
'subjectKey' => 'subjectId',
124+
'algorithms' => ['HS256'],
125+
]);
126+
});
127+
}
128+
110129
/**
111130
* testAuthenticateViaQueryParamToken
112131
*

0 commit comments

Comments
 (0)