From c2429b721e77b5f70eca0ebd2bf8214ebc5aa4e9 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 15 Jul 2024 23:33:24 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Secrets/Secret.php | 1 - src/Signer/Hs/Hs256Signer.php | 2 +- src/Signer/Hs/Hs384Signer.php | 2 +- src/Signer/Hs/Hs512Signer.php | 2 +- src/Signer/Rs/Rs384Signer.php | 1 - src/Signer/Rs/Rs512Signer.php | 1 - src/Signer/Rs/RsSigner.php | 7 +++++-- src/Signer/Signer.php | 2 +- src/SignerFactory.php | 4 +++- src/Token.php | 23 +++++++++++++++++------ src/Validator/EqualityValidator.php | 2 -- src/Validator/ExpValidator.php | 5 ++--- src/Validator/NbfValidator.php | 7 +++---- src/Validator/Validator.php | 2 +- tests/Signer/Rs/RsSignerTest.php | 4 +--- tests/SignerFactoryTest.php | 2 +- tests/TokenTest.php | 2 +- tests/Validator/IssValidatorTest.php | 5 ++--- tests/Validator/SubValidatorTest.php | 5 ++--- 19 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/Secrets/Secret.php b/src/Secrets/Secret.php index cc60b7c..b975820 100644 --- a/src/Secrets/Secret.php +++ b/src/Secrets/Secret.php @@ -4,5 +4,4 @@ interface Secret { - } diff --git a/src/Signer/Hs/Hs256Signer.php b/src/Signer/Hs/Hs256Signer.php index db2f275..f9616ea 100644 --- a/src/Signer/Hs/Hs256Signer.php +++ b/src/Signer/Hs/Hs256Signer.php @@ -13,4 +13,4 @@ public function algorithm(): string { return 'HS256'; } -} \ No newline at end of file +} diff --git a/src/Signer/Hs/Hs384Signer.php b/src/Signer/Hs/Hs384Signer.php index 7c9167c..d6d44c8 100644 --- a/src/Signer/Hs/Hs384Signer.php +++ b/src/Signer/Hs/Hs384Signer.php @@ -13,4 +13,4 @@ public function algorithm(): string { return 'HS384'; } -} \ No newline at end of file +} diff --git a/src/Signer/Hs/Hs512Signer.php b/src/Signer/Hs/Hs512Signer.php index 7da82a1..c811d7d 100644 --- a/src/Signer/Hs/Hs512Signer.php +++ b/src/Signer/Hs/Hs512Signer.php @@ -13,4 +13,4 @@ public function algorithm(): string { return 'HS512'; } -} \ No newline at end of file +} diff --git a/src/Signer/Rs/Rs384Signer.php b/src/Signer/Rs/Rs384Signer.php index ed4f093..7e13759 100644 --- a/src/Signer/Rs/Rs384Signer.php +++ b/src/Signer/Rs/Rs384Signer.php @@ -4,7 +4,6 @@ class Rs384Signer extends RsSigner { - /** * @var int */ diff --git a/src/Signer/Rs/Rs512Signer.php b/src/Signer/Rs/Rs512Signer.php index f068999..1b433b5 100644 --- a/src/Signer/Rs/Rs512Signer.php +++ b/src/Signer/Rs/Rs512Signer.php @@ -4,7 +4,6 @@ class Rs512Signer extends RsSigner { - /** * @var int */ diff --git a/src/Signer/Rs/RsSigner.php b/src/Signer/Rs/RsSigner.php index 756e2c7..60ff14e 100644 --- a/src/Signer/Rs/RsSigner.php +++ b/src/Signer/Rs/RsSigner.php @@ -26,11 +26,14 @@ public function sign(string $header, string $payload): mixed $secret = $this->getSecret(); $signature = null; $signed = openssl_sign( - "$header.$payload", $signature, $secret->privateKey, $this->openSslAlgorithm + "$header.$payload", + $signature, + $secret->privateKey, + $this->openSslAlgorithm ); if (!$signed) { - throw new RuntimeException("Failed to create signature"); + throw new RuntimeException('Failed to create signature'); } return $signature; diff --git a/src/Signer/Signer.php b/src/Signer/Signer.php index 60a41ee..21ce257 100644 --- a/src/Signer/Signer.php +++ b/src/Signer/Signer.php @@ -33,7 +33,7 @@ public function getSecret(): Secret|null public function setSecret(Secret $secret): void { if (!$this->validateSecret($secret)) { - throw new InvalidArgumentException("Invalid secret provided"); + throw new InvalidArgumentException('Invalid secret provided'); } $this->secret = $secret; diff --git a/src/SignerFactory.php b/src/SignerFactory.php index 2742dd7..311e931 100644 --- a/src/SignerFactory.php +++ b/src/SignerFactory.php @@ -40,6 +40,7 @@ public static function assign(string $algorithm, string $className): void * @param string $algorithm * * @throws Exception + * * @return Signer */ public static function build(string $algorithm): Signer @@ -49,6 +50,7 @@ public static function build(string $algorithm): Signer } $className = self::$algorithmMap[$algorithm]; - return new $className; + + return new $className(); } } diff --git a/src/Token.php b/src/Token.php index 7fd0e67..fe3b1ed 100644 --- a/src/Token.php +++ b/src/Token.php @@ -12,9 +12,6 @@ class Token { - /** - * - */ public const DEFAULT_SIGNER = Hs256Signer::class; /** @@ -75,6 +72,7 @@ public function __construct() public function assignValidator(Validator $validator): Token { $this->validators[$validator->validates()] = $validator; + return $this; } @@ -85,11 +83,12 @@ private static function getDefaultSigner(): Signer { $signer = self::DEFAULT_SIGNER; - return new $signer; + return new $signer(); } /** * @throws Exception + * * @return Token */ public static function fromAuthorizationBearer(): Token @@ -121,7 +120,6 @@ public static function fromAuthorizationBearer(): Token throw new Exception('Authorization header is not set'); } - $matches = []; if (!preg_match('/Bearer\s((.*)\.(.*)\.(.*))/', $authorizationHeader, $matches)) { throw new Exception('Invalid "Authorization" header value'); @@ -134,6 +132,7 @@ public static function fromAuthorizationBearer(): Token * @param string $token * * @throws Exception + * * @return Token */ public static function fromString(string $token): Token @@ -177,6 +176,7 @@ private static function decodeSection(string $encoded): string public function withSigner(Signer $signer): Token { $this->signer = $signer; + return $this->withHeader('alg', $signer->algorithm()); } @@ -189,6 +189,7 @@ public function withSigner(Signer $signer): Token public function withHeader(string $index, mixed $value): Token { $this->headers[$index] = $value; + return $this; } @@ -200,6 +201,7 @@ public function withHeader(string $index, mixed $value): Token public function setSignature(string $signature): Token { $this->signature = $signature; + return $this; } @@ -215,6 +217,7 @@ public static function create(): Token * @param $param * * @throws Exception + * * @return Token */ public static function fromQueryString($param = 'token'): Token @@ -232,6 +235,7 @@ public static function fromQueryString($param = 'token'): Token public function assignHeaderValidator(Validator $validator): Token { $this->headerValidators[$validator->validates()] = $validator; + return $this; } @@ -257,6 +261,7 @@ public function getHeaders(string $index = null): mixed public function setHeaders(array $headers): Token { $this->headers = $headers; + return $this; } @@ -282,6 +287,7 @@ public function getPayload(string $index = null): mixed public function setPayload(array $payload): Token { $this->payload = $payload; + return $this; } @@ -301,6 +307,7 @@ public function getLastValidationIssue(): ?string public function remove(string $index): Token { unset($this->payload[$index]); + return $this; } @@ -312,6 +319,7 @@ public function remove(string $index): Token public function removeHeader(string $index): Token { unset($this->headers[$index]); + return $this; } @@ -350,6 +358,7 @@ public function validate(Secret $secret): bool if (isset($this->headers[$i]) && !$validator->validate($this->headers[$i])) { $this->lastValidationIssue = "Header '$i' value is invalid."; + return false; } } @@ -360,6 +369,7 @@ public function validate(Secret $secret): bool if (isset($this->payload[$i]) && !$validator->validate($this->payload[$i])) { $this->lastValidationIssue = "Payload field '$i' value is invalid."; + return false; } } @@ -374,7 +384,7 @@ public function validate(Secret $secret): bool $valid = $signer->verify($headers, $payload, $this->signature); if (!$valid) { - $this->lastValidationIssue = "Token signature is invalid."; + $this->lastValidationIssue = 'Token signature is invalid.'; } return $valid; @@ -406,6 +416,7 @@ public function sign(mixed $secret): Token public function with(string $index, mixed $value): Token { $this->payload[$index] = $value; + return $this; } } diff --git a/src/Validator/EqualityValidator.php b/src/Validator/EqualityValidator.php index 93bba0c..39edfa2 100644 --- a/src/Validator/EqualityValidator.php +++ b/src/Validator/EqualityValidator.php @@ -2,8 +2,6 @@ namespace Webdevcave\Jwt\Validator; -use Webdevcave\Jwt\Validator\Validator; - abstract class EqualityValidator extends Validator { /** diff --git a/src/Validator/ExpValidator.php b/src/Validator/ExpValidator.php index 41fd714..959329a 100644 --- a/src/Validator/ExpValidator.php +++ b/src/Validator/ExpValidator.php @@ -4,11 +4,10 @@ class ExpValidator extends Validator { - /** * @inheritDoc */ - function validates(): string + public function validates(): string { return 'exp'; } @@ -16,7 +15,7 @@ function validates(): string /** * @inheritDoc */ - function validate(mixed $value): bool + public function validate(mixed $value): bool { return $value > time(); } diff --git a/src/Validator/NbfValidator.php b/src/Validator/NbfValidator.php index 017c7cb..48fe496 100644 --- a/src/Validator/NbfValidator.php +++ b/src/Validator/NbfValidator.php @@ -4,11 +4,10 @@ class NbfValidator extends Validator { - /** * @inheritDoc */ - function validates(): string + public function validates(): string { return 'nbf'; } @@ -16,8 +15,8 @@ function validates(): string /** * @inheritDoc */ - function validate(mixed $value): bool + public function validate(mixed $value): bool { return $value <= time(); } -} \ No newline at end of file +} diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index 607bbf8..372716b 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -5,7 +5,7 @@ abstract class Validator { /** - * Must return the validated field name; + * Must return the validated field name;. * * @return string */ diff --git a/tests/Signer/Rs/RsSignerTest.php b/tests/Signer/Rs/RsSignerTest.php index a55a586..54a93af 100644 --- a/tests/Signer/Rs/RsSignerTest.php +++ b/tests/Signer/Rs/RsSignerTest.php @@ -3,14 +3,12 @@ namespace Webdevcave\Jwt\Tests\Signer\Rs; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use RuntimeException; use Webdevcave\Jwt\Secrets\RsSecret; use Webdevcave\Jwt\Signer\Rs\Rs256Signer; use Webdevcave\Jwt\Signer\Rs\RsSigner; -use PHPUnit\Framework\TestCase; use Webdevcave\Jwt\Signer\Signer; -use Webdevcave\Jwt\SignerFactory; -use Webdevcave\Jwt\Token; #[CoversClass(RsSecret::class)] #[CoversClass(RsSigner::class)] diff --git a/tests/SignerFactoryTest.php b/tests/SignerFactoryTest.php index ae52e09..90a0930 100644 --- a/tests/SignerFactoryTest.php +++ b/tests/SignerFactoryTest.php @@ -4,6 +4,7 @@ use Exception; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use Webdevcave\Jwt\Signer\Hs\Hs256Signer; use Webdevcave\Jwt\Signer\Hs\Hs384Signer; use Webdevcave\Jwt\Signer\Hs\Hs512Signer; @@ -11,7 +12,6 @@ use Webdevcave\Jwt\Signer\Rs\Rs384Signer; use Webdevcave\Jwt\Signer\Rs\Rs512Signer; use Webdevcave\Jwt\SignerFactory; -use PHPUnit\Framework\TestCase; use Webdevcave\Jwt\Tests\CustomSigner\MyCustomSigner; #[CoversClass(SignerFactory::class)] diff --git a/tests/TokenTest.php b/tests/TokenTest.php index 98fdda3..f01e570 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -4,6 +4,7 @@ use Exception; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use Webdevcave\Jwt\Secrets\HsSecret; use Webdevcave\Jwt\Secrets\RsSecret; use Webdevcave\Jwt\Signer\Hs\Hs256Signer; @@ -17,7 +18,6 @@ use Webdevcave\Jwt\Signer\Signer; use Webdevcave\Jwt\SignerFactory; use Webdevcave\Jwt\Token; -use PHPUnit\Framework\TestCase; use Webdevcave\Jwt\Validator\AudValidator; use Webdevcave\Jwt\Validator\EqualityValidator; use Webdevcave\Jwt\Validator\ExpValidator; diff --git a/tests/Validator/IssValidatorTest.php b/tests/Validator/IssValidatorTest.php index d2c3387..c06a405 100644 --- a/tests/Validator/IssValidatorTest.php +++ b/tests/Validator/IssValidatorTest.php @@ -4,8 +4,8 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Webdevcave\Jwt\Validator\IssValidator; use Webdevcave\Jwt\Validator\EqualityValidator; +use Webdevcave\Jwt\Validator\IssValidator; #[CoversClass(IssValidator::class)] #[CoversClass(EqualityValidator::class)] @@ -14,10 +14,9 @@ class IssValidatorTest extends TestCase public function testValidate() { $validator = new IssValidator('issuer'); - + $this->assertEquals('iss', $validator->validates()); $this->assertTrue($validator->validate('issuer')); $this->assertFalse($validator->validate('not-issuer')); } - } diff --git a/tests/Validator/SubValidatorTest.php b/tests/Validator/SubValidatorTest.php index 3222345..fae06c9 100644 --- a/tests/Validator/SubValidatorTest.php +++ b/tests/Validator/SubValidatorTest.php @@ -4,8 +4,8 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Webdevcave\Jwt\Validator\SubValidator; use Webdevcave\Jwt\Validator\EqualityValidator; +use Webdevcave\Jwt\Validator\SubValidator; #[CoversClass(SubValidator::class)] #[CoversClass(EqualityValidator::class)] @@ -14,10 +14,9 @@ class SubValidatorTest extends TestCase public function testValidate() { $validator = new SubValidator('sub'); - + $this->assertEquals('sub', $validator->validates()); $this->assertTrue($validator->validate('sub')); $this->assertFalse($validator->validate('not-sub')); } - }