Skip to content

Commit a7037d6

Browse files
improved code style by importing some use statements, subsrt replacing with strpos
1 parent 5e0c78e commit a7037d6

File tree

3 files changed

+74
-36
lines changed

3 files changed

+74
-36
lines changed

src/JWT.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@
1313

1414
namespace Ahc\Jwt;
1515

16+
use stdClass;
17+
use function array_merge;
18+
use function base64_decode;
19+
use function base64_encode;
20+
use function explode;
21+
use function hash_equals;
22+
use function hash_hmac;
23+
use function is_array;
24+
use function json_decode;
25+
use function json_encode;
26+
use function openssl_pkey_get_details;
27+
use function openssl_sign;
28+
use function openssl_verify;
29+
use function reset;
30+
use function rtrim;
31+
use function strtr;
32+
use function substr_count;
33+
use function time;
34+
use const JSON_UNESCAPED_SLASHES;
35+
use const OPENSSL_ALGO_SHA256;
36+
use const OPENSSL_ALGO_SHA384;
37+
use const OPENSSL_ALGO_SHA512;
38+
1639
/**
1740
* JSON Web Token (JWT) implementation in PHP5.5+.
1841
*
@@ -43,9 +66,9 @@ class JWT
4366
'HS256' => 'sha256',
4467
'HS384' => 'sha384',
4568
'HS512' => 'sha512',
46-
'RS256' => \OPENSSL_ALGO_SHA256,
47-
'RS384' => \OPENSSL_ALGO_SHA384,
48-
'RS512' => \OPENSSL_ALGO_SHA512,
69+
'RS256' => OPENSSL_ALGO_SHA256,
70+
'RS384' => OPENSSL_ALGO_SHA384,
71+
'RS512' => OPENSSL_ALGO_SHA512,
4972
];
5073

5174
/** @var string|resource The signature key. */
@@ -55,7 +78,7 @@ class JWT
5578
protected $keys = [];
5679

5780
/** @var int|null Use setTestTimestamp() to set custom value for time(). Useful for testability. */
58-
protected $timestamp = null;
81+
protected $timestamp;
5982

6083
/** @var string The JWT signing algorithm. Defaults to HS256. */
6184
protected $algo = 'HS256';
@@ -88,9 +111,9 @@ public function __construct(
88111
) {
89112
$this->validateConfig($key, $algo, $maxAge, $leeway);
90113

91-
if (\is_array($key)) {
114+
if (is_array($key)) {
92115
$this->registerKeys($key);
93-
$key = \reset($key); // use first one!
116+
$key = reset($key); // use first one!
94117
}
95118

96119
$this->key = $key;
@@ -109,7 +132,7 @@ public function __construct(
109132
*/
110133
public function registerKeys(array $keys): self
111134
{
112-
$this->keys = \array_merge($this->keys, $keys);
135+
$this->keys = array_merge($this->keys, $keys);
113136

114137
return $this;
115138
}
@@ -129,7 +152,7 @@ public function encode(array $payload, array $header = []): string
129152
$this->validateKid($header);
130153

131154
if (!isset($payload['iat']) && !isset($payload['exp'])) {
132-
$payload['exp'] = ($this->timestamp ?: \time()) + $this->maxAge;
155+
$payload['exp'] = ($this->timestamp ?: time()) + $this->maxAge;
133156
}
134157

135158
$header = $this->urlSafeEncode($header);
@@ -151,11 +174,11 @@ public function encode(array $payload, array $header = []): string
151174
*/
152175
public function decode(string $token, bool $verify = true): array
153176
{
154-
if (\substr_count($token, '.') < 2) {
177+
if (substr_count($token, '.') < 2) {
155178
throw new JWTException('Invalid token: Incomplete segments', static::ERROR_TOKEN_INVALID);
156179
}
157180

158-
$token = \explode('.', $token, 3);
181+
$token = explode('.', $token, 3);
159182
if (!$verify) {
160183
return (array) $this->urlSafeDecode($token[1]);
161184
}
@@ -196,13 +219,13 @@ public function setTestTimestamp(int $timestamp = null): self
196219
protected function sign(string $input): string
197220
{
198221
// HMAC SHA.
199-
if (\substr($this->algo, 0, 2) === 'HS') {
200-
return \hash_hmac($this->algos[$this->algo], $input, $this->key, true);
222+
if (strpos($this->algo, 'HS') === 0) {
223+
return hash_hmac($this->algos[$this->algo], $input, $this->key, true);
201224
}
202225

203226
$this->validateKey();
204227

205-
\openssl_sign($input, $signature, $this->key, $this->algos[$this->algo]);
228+
openssl_sign($input, $signature, $this->key, $this->algos[$this->algo]);
206229

207230
return $signature;
208231
}
@@ -222,15 +245,15 @@ protected function verify(string $input, string $signature): bool
222245
$algo = $this->algos[$this->algo];
223246

224247
// HMAC SHA.
225-
if (\substr($this->algo, 0, 2) === 'HS') {
226-
return \hash_equals($this->urlSafeEncode(\hash_hmac($algo, $input, $this->key, true)), $signature);
248+
if (strpos($this->algo, 'HS') === 0) {
249+
return hash_equals($this->urlSafeEncode(hash_hmac($algo, $input, $this->key, true)), $signature);
227250
}
228251

229252
$this->validateKey();
230253

231-
$pubKey = \openssl_pkey_get_details($this->key)['key'];
254+
$pubKey = openssl_pkey_get_details($this->key)['key'];
232255

233-
return \openssl_verify($input, $this->urlSafeDecode($signature, false), $pubKey, $algo) === 1;
256+
return openssl_verify($input, $this->urlSafeDecode($signature, false), $pubKey, $algo) === 1;
234257
}
235258

236259
/**
@@ -246,12 +269,12 @@ protected function verify(string $input, string $signature): bool
246269
*/
247270
protected function urlSafeEncode($data): string
248271
{
249-
if (\is_array($data)) {
250-
$data = \json_encode($data, \JSON_UNESCAPED_SLASHES);
272+
if (is_array($data)) {
273+
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
251274
$this->validateLastJson();
252275
}
253276

254-
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
277+
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
255278
}
256279

257280
/**
@@ -260,17 +283,17 @@ protected function urlSafeEncode($data): string
260283
* @param array|string $data
261284
* @param bool $asJson Whether to parse as JSON (defaults to true).
262285
*
263-
* @throws JWTException When JSON encode fails.
286+
* @return array|stdClass|string
287+
*@throws JWTException When JSON encode fails.
264288
*
265-
* @return array|\stdClass|string
266289
*/
267290
protected function urlSafeDecode($data, bool $asJson = true)
268291
{
269292
if (!$asJson) {
270-
return \base64_decode(\strtr($data, '-_', '+/'));
293+
return base64_decode(strtr($data, '-_', '+/'));
271294
}
272295

273-
$data = \json_decode(\base64_decode(\strtr($data, '-_', '+/')));
296+
$data = json_decode(base64_decode(strtr($data, '-_', '+/')), false);
274297
$this->validateLastJson();
275298

276299
return $data;

src/JWTException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Ahc\Jwt;
1313

14-
class JWTException extends \InvalidArgumentException
14+
use InvalidArgumentException;
15+
16+
class JWTException extends InvalidArgumentException
1517
{
1618
// ;)
1719
}

src/ValidatesJWT.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313

1414
namespace Ahc\Jwt;
1515

16+
use OpenSSLAsymmetricKey;
17+
use OpenSSLCertificate;
18+
use OpenSSLCertificateSigningRequest;
19+
use function is_resource;
20+
use function is_string;
21+
use function json_last_error;
22+
use function json_last_error_msg;
23+
use function openssl_get_privatekey;
24+
use function substr;
25+
use function time;
26+
use const JSON_ERROR_NONE;
27+
use const PHP_VERSION_ID;
28+
1629
/**
1730
* JSON Web Token (JWT) implementation in PHP7.
1831
*
@@ -82,7 +95,7 @@ protected function validateKid(array $header)
8295
*/
8396
protected function validateTimestamps(array $payload)
8497
{
85-
$timestamp = $this->timestamp ?: \time();
98+
$timestamp = $this->timestamp ?: time();
8699
$checks = [
87100
['exp', $this->leeway /* */ , static::ERROR_TOKEN_EXPIRED, 'Expired'],
88101
['iat', $this->maxAge - $this->leeway, static::ERROR_TOKEN_EXPIRED, 'Expired'],
@@ -106,22 +119,22 @@ protected function validateTimestamps(array $payload)
106119
*/
107120
protected function validateKey()
108121
{
109-
if (\is_string($key = $this->key)) {
110-
if (\substr($key, 0, 7) !== 'file://') {
122+
if (is_string($key = $this->key)) {
123+
if (strpos($key, 'file://') !== 0) {
111124
$key = 'file://' . $key;
112125
}
113126

114-
$this->key = \openssl_get_privatekey($key, $this->passphrase ?: '');
127+
$this->key = openssl_get_privatekey($key, $this->passphrase ?: '');
115128
}
116129

117-
if (\PHP_VERSION_ID < 80000 && !\is_resource($this->key)) {
130+
if (PHP_VERSION_ID < 80000 && !is_resource($this->key)) {
118131
throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID);
119132
}
120133

121-
if (\PHP_VERSION_ID > 80000 && !(
122-
$this->key instanceof \OpenSSLAsymmetricKey
123-
|| $this->key instanceof \OpenSSLCertificate
124-
|| $this->key instanceof \OpenSSLCertificateSigningRequest
134+
if (PHP_VERSION_ID > 80000 && !(
135+
$this->key instanceof OpenSSLAsymmetricKey
136+
|| $this->key instanceof OpenSSLCertificate
137+
|| $this->key instanceof OpenSSLCertificateSigningRequest
125138
)) {
126139
throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID);
127140
}
@@ -132,10 +145,10 @@ protected function validateKey()
132145
*/
133146
protected function validateLastJson()
134147
{
135-
if (\JSON_ERROR_NONE === \json_last_error()) {
148+
if (JSON_ERROR_NONE === json_last_error()) {
136149
return;
137150
}
138151

139-
throw new JWTException('JSON failed: ' . \json_last_error_msg(), static::ERROR_JSON_FAILED);
152+
throw new JWTException('JSON failed: ' . json_last_error_msg(), static::ERROR_JSON_FAILED);
140153
}
141154
}

0 commit comments

Comments
 (0)