From 9aa087db5995e02ccf157716943232fca47a9ab2 Mon Sep 17 00:00:00 2001 From: Spomky Date: Fri, 29 May 2020 22:21:59 +0200 Subject: [PATCH] Bugs fixed Fix #257 Fix #258 Global imports --- AbstractBuilder.php | 5 +++-- AbstractLoader.php | 27 ++++++++++++++++----------- AlgorithmProvider.php | 5 +++-- Build.php | 2 +- CallableChecker.php | 2 +- ContentEncryptionAlgorithmChecker.php | 8 +++++--- Decrypt.php | 10 ++++++---- JWEBuilder.php | 7 ++++--- JWSBuilder.php | 2 +- JWT.php | 2 +- Load.php | 2 +- ParameterBag.php | 17 ++++++++++------- Tests/AlgorithmProviderTest.php | 18 ++++++++++-------- Tests/EncryptionTest.php | 2 +- Tests/ParameterBagTest.php | 2 +- Tests/SignatureTest.php | 6 +++--- Validate.php | 5 +++-- 17 files changed, 70 insertions(+), 52 deletions(-) diff --git a/AbstractBuilder.php b/AbstractBuilder.php index 3db5ef2..b3d2e44 100644 --- a/AbstractBuilder.php +++ b/AbstractBuilder.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -14,6 +14,7 @@ namespace Jose\Easy; use InvalidArgumentException; +use function is_string; use Jose\Component\Core\Algorithm as JoseAlgorithm; use Jose\Component\Signature\Algorithm; @@ -101,7 +102,7 @@ public function alg($alg): self $clone->jwt->header->set('alg', $alg->name()); break; - case \is_string($alg): + case is_string($alg): $clone->jwt->header->set('alg', $alg); break; diff --git a/AbstractLoader.php b/AbstractLoader.php index 1fd0f7b..7bcf01b 100644 --- a/AbstractLoader.php +++ b/AbstractLoader.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,7 +13,12 @@ namespace Jose\Easy; +use function in_array; use InvalidArgumentException; +use function is_array; +use function is_callable; +use function is_int; +use function is_string; use Jose\Component\Checker; use Jose\Component\Core\Algorithm; use Jose\Component\Core\JWK; @@ -113,12 +118,12 @@ public function claim(string $key, $checker, bool $inHeader = false): self switch (true) { case $checker instanceof Checker\ClaimChecker: break; - case \is_callable($checker): + case is_callable($checker): $checker = new CallableChecker($key, $checker); break; - case \is_array($checker): - $checker = new CallableChecker($key, static function ($value) use ($checker) {return \in_array($value, $checker, true); }); + case is_array($checker): + $checker = new CallableChecker($key, static function ($value) use ($checker) {return in_array($value, $checker, true); }); break; default: @@ -146,7 +151,7 @@ public function exp($leeway = 0, bool $inHeader = false): self return $clone; } - if (!\is_int($leeway) or $leeway < 0) { + if (!is_int($leeway) or $leeway < 0) { throw new InvalidArgumentException('First parameter for "exp" claim is invalid. Set false to disable or a positive integer.'); } @@ -166,7 +171,7 @@ public function nbf($leeway = 0, bool $inHeader = false): self return $clone; } - if (!\is_int($leeway) or $leeway < 0) { + if (!is_int($leeway) or $leeway < 0) { throw new InvalidArgumentException('First parameter for "nbf" claim is invalid. Set false to disable or a positive integer.'); } @@ -186,7 +191,7 @@ public function iat($leeway = 0, bool $inHeader = false): self return $clone; } - if (!\is_int($leeway) or $leeway < 0) { + if (!is_int($leeway) or $leeway < 0) { throw new InvalidArgumentException('First parameter for "iat" claim is invalid. Set false to disable or a positive integer.'); } @@ -202,7 +207,7 @@ public function alg($alg): self { $clone = clone $this; switch (true) { - case \is_string($alg): + case is_string($alg): $clone->allowedAlgorithms[] = $alg; return $clone; @@ -244,12 +249,12 @@ public function header(string $key, $checker): self switch (true) { case $checker instanceof Checker\HeaderChecker: break; - case \is_callable($checker): + case is_callable($checker): $checker = new CallableChecker($key, $checker); break; - case \is_array($checker): - $checker = new CallableChecker($key, static function ($value) use ($checker) {return \in_array($value, $checker, true); }); + case is_array($checker): + $checker = new CallableChecker($key, static function ($value) use ($checker) {return in_array($value, $checker, true); }); break; default: diff --git a/AlgorithmProvider.php b/AlgorithmProvider.php index 89b4df2..3509a7e 100644 --- a/AlgorithmProvider.php +++ b/AlgorithmProvider.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -14,6 +14,7 @@ namespace Jose\Easy; use Jose\Component\Core\Algorithm; +use Throwable; final class AlgorithmProvider { @@ -50,7 +51,7 @@ private function addClass(string $algorithmClass): void if (class_exists($algorithmClass)) { try { $this->algorithms[] = new $algorithmClass(); - } catch (\Throwable $throwable) { + } catch (Throwable $throwable) { //does nothing } } diff --git a/Build.php b/Build.php index f1b580f..c5971d7 100644 --- a/Build.php +++ b/Build.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/CallableChecker.php b/CallableChecker.php index fc2456a..739f66a 100644 --- a/CallableChecker.php +++ b/CallableChecker.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/ContentEncryptionAlgorithmChecker.php b/ContentEncryptionAlgorithmChecker.php index be72c95..c7606cf 100644 --- a/ContentEncryptionAlgorithmChecker.php +++ b/ContentEncryptionAlgorithmChecker.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,6 +13,8 @@ namespace Jose\Easy; +use function in_array; +use function is_string; use Jose\Component\Checker\HeaderChecker; use Jose\Component\Checker\InvalidHeaderException; @@ -50,10 +52,10 @@ public function __construct(array $supportedAlgorithms, bool $protectedHeader = */ public function checkHeader($value): void { - if (!\is_string($value)) { + if (!is_string($value)) { throw new InvalidHeaderException('"enc" must be a string.', self::HEADER_NAME, $value); } - if (!\in_array($value, $this->supportedAlgorithms, true)) { + if (!in_array($value, $this->supportedAlgorithms, true)) { throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value); } } diff --git a/Decrypt.php b/Decrypt.php index b23a3d7..2873d81 100644 --- a/Decrypt.php +++ b/Decrypt.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,7 +13,9 @@ namespace Jose\Easy; +use function count; use InvalidArgumentException; +use function is_string; use Jose\Component\Checker; use Jose\Component\Core\Algorithm; use Jose\Component\Core\AlgorithmManager; @@ -60,7 +62,7 @@ public function enc($enc): self { $clone = clone $this; switch (true) { - case \is_string($enc): + case is_string($enc): $clone->allowedContentEncryptionAlgorithms[] = $enc; return $clone; @@ -89,10 +91,10 @@ public function encs($encs): self public function run(): JWT { - if (0 !== \count($this->allowedAlgorithms)) { + if (0 !== count($this->allowedAlgorithms)) { $this->headerCheckers[] = new Checker\AlgorithmChecker($this->allowedAlgorithms, true); } - if (0 !== \count($this->allowedContentEncryptionAlgorithms)) { + if (0 !== count($this->allowedContentEncryptionAlgorithms)) { $this->headerCheckers[] = new ContentEncryptionAlgorithmChecker($this->allowedContentEncryptionAlgorithms, true); } $jwe = (new CompactSerializer())->unserialize($this->token); diff --git a/JWEBuilder.php b/JWEBuilder.php index a82e770..c176b6e 100644 --- a/JWEBuilder.php +++ b/JWEBuilder.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -14,6 +14,7 @@ namespace Jose\Easy; use InvalidArgumentException; +use function is_string; use Jose\Component\Core\Algorithm; use Jose\Component\Core\AlgorithmManager; use Jose\Component\Core\JWK; @@ -55,7 +56,7 @@ public function enc($enc): self $clone->jwt->header->set('enc', $enc->name()); break; - case \is_string($enc): + case is_string($enc): $clone->jwt->header->set('enc', $enc); break; @@ -80,7 +81,7 @@ public function zip($zip): self $clone->jwt->header->set('zip', $zip->name()); break; - case \is_string($zip): + case is_string($zip): $clone->jwt->header->set('zip', $zip); break; diff --git a/JWSBuilder.php b/JWSBuilder.php index 1b1d0da..4f5c2f1 100644 --- a/JWSBuilder.php +++ b/JWSBuilder.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/JWT.php b/JWT.php index 49f2853..c5f9235 100644 --- a/JWT.php +++ b/JWT.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/Load.php b/Load.php index a059abe..5245ef6 100644 --- a/Load.php +++ b/Load.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/ParameterBag.php b/ParameterBag.php index 2f5f146..1cf5aee 100644 --- a/ParameterBag.php +++ b/ParameterBag.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,7 +13,10 @@ namespace Jose\Easy; +use function array_key_exists; use ArrayIterator; +use function call_user_func_array; +use function count; use Countable; use InvalidArgumentException; use IteratorAggregate; @@ -31,15 +34,15 @@ class ParameterBag implements IteratorAggregate, Countable public function __call(string $name, array $arguments) { if (method_exists($this, $name)) { - return \call_user_func_array([$this, $name], $arguments); + return call_user_func_array([$this, $name], $arguments); } - if (0 === \count($arguments)) { + if (0 === count($arguments)) { return $this->get($name); } array_unshift($arguments, $name); - return \call_user_func_array([$this, 'set'], $arguments); + return call_user_func_array([$this, 'set'], $arguments); } public function all(): array @@ -76,7 +79,7 @@ public function add(array $parameters): void */ public function get(string $key) { - if (!\array_key_exists($key, $this->parameters)) { + if (!array_key_exists($key, $this->parameters)) { throw new InvalidArgumentException(sprintf('Parameter "%s" is missing', $key)); } @@ -93,7 +96,7 @@ public function set(string $key, $value): void public function has(string $key): bool { - return \array_key_exists($key, $this->parameters); + return array_key_exists($key, $this->parameters); } public function remove(string $key): void @@ -108,6 +111,6 @@ public function getIterator(): ArrayIterator public function count(): int { - return \count($this->parameters); + return count($this->parameters); } } diff --git a/Tests/AlgorithmProviderTest.php b/Tests/AlgorithmProviderTest.php index c4863ee..c53937b 100644 --- a/Tests/AlgorithmProviderTest.php +++ b/Tests/AlgorithmProviderTest.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,6 +13,8 @@ namespace Jose\Easy\Tests; +use BadFunctionCallException; +use function get_class; use Jose\Component\Core\JWK; use Jose\Component\Encryption\Algorithm\ContentEncryption; use Jose\Component\Encryption\Algorithm\KeyEncryption; @@ -90,7 +92,7 @@ public function itReturnsTheAvailableAlgorithms(): void { $algorithmProvider = new AlgorithmProvider(self::ALL_ALGORITHMS); foreach ($algorithmProvider->getAvailableAlgorithms() as $algorithm) { - static::assertContains(\get_class($algorithm), self::ALL_ALGORITHMS); + static::assertContains(get_class($algorithm), self::ALL_ALGORITHMS); } } @@ -139,30 +141,30 @@ public function __construct() return; } - throw new \BadFunctionCallException('should not be called'); + throw new BadFunctionCallException('should not be called'); } public function name(): string { - throw new \BadFunctionCallException('should not be called'); + throw new BadFunctionCallException('should not be called'); } public function allowedKeyTypes(): array { - throw new \BadFunctionCallException('should not be called'); + throw new BadFunctionCallException('should not be called'); } public function sign(JWK $key, string $input): string { - throw new \BadFunctionCallException('should not be called'); + throw new BadFunctionCallException('should not be called'); } public function verify(JWK $key, string $input, string $signature): bool { - throw new \BadFunctionCallException('should not be called'); + throw new BadFunctionCallException('should not be called'); } }; - return \get_class($mockClass); + return get_class($mockClass); } } diff --git a/Tests/EncryptionTest.php b/Tests/EncryptionTest.php index 89d11c6..5af575f 100644 --- a/Tests/EncryptionTest.php +++ b/Tests/EncryptionTest.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/Tests/ParameterBagTest.php b/Tests/ParameterBagTest.php index fb10f85..afb47d3 100644 --- a/Tests/ParameterBagTest.php +++ b/Tests/ParameterBagTest.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. diff --git a/Tests/SignatureTest.php b/Tests/SignatureTest.php index e628730..b92d0e8 100644 --- a/Tests/SignatureTest.php +++ b/Tests/SignatureTest.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -89,9 +89,9 @@ public function jwsCanBeCreated(): void */ public function missingMandatoryClaimInPayload(): void { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('The following claims are mandatory: test.'); - + $jwk = new JWK([ 'kty' => 'RSA', 'kid' => 'bilbo.baggins@hobbiton.example', diff --git a/Validate.php b/Validate.php index 362d737..0b3736a 100644 --- a/Validate.php +++ b/Validate.php @@ -5,7 +5,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2019 Spomky-Labs + * Copyright (c) 2014-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. @@ -13,6 +13,7 @@ namespace Jose\Easy; +use function count; use Exception; use Jose\Component\Checker; use Jose\Component\Core\AlgorithmManager; @@ -31,7 +32,7 @@ public static function token(string $token): self public function run(): JWT { - if (0 !== \count($this->allowedAlgorithms)) { + if (0 !== count($this->allowedAlgorithms)) { $this->headerCheckers[] = new Checker\AlgorithmChecker($this->allowedAlgorithms, true); } $jws = (new CompactSerializer())->unserialize($this->token);