diff --git a/src/CsrfMiddleware.php b/src/CsrfMiddleware.php index 467712c..6d5c35f 100644 --- a/src/CsrfMiddleware.php +++ b/src/CsrfMiddleware.php @@ -28,18 +28,8 @@ final class CsrfMiddleware implements MiddlewareInterface private string $parameterName = self::PARAMETER_NAME; private string $headerName = self::HEADER_NAME; - private ResponseFactoryInterface $responseFactory; - private CsrfTokenInterface $token; - private ?RequestHandlerInterface $failureHandler; - - public function __construct( - ResponseFactoryInterface $responseFactory, - CsrfTokenInterface $token, - RequestHandlerInterface $failureHandler = null - ) { - $this->responseFactory = $responseFactory; - $this->token = $token; - $this->failureHandler = $failureHandler; + public function __construct(private ResponseFactoryInterface $responseFactory, private CsrfTokenInterface $token, private ?RequestHandlerInterface $failureHandler = null) + { } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface diff --git a/src/Hmac/HmacCsrfToken.php b/src/Hmac/HmacCsrfToken.php index b7aeabc..b695767 100644 --- a/src/Hmac/HmacCsrfToken.php +++ b/src/Hmac/HmacCsrfToken.php @@ -24,29 +24,21 @@ */ final class HmacCsrfToken implements CsrfTokenInterface { - private CsrfTokenIdentityGeneratorInterface $identityGenerator; private Mac $mac; - /** - * @var string Shared secret key used to generate the hash. - */ - private string $secretKey; - - /** - * @var int|null Number of seconds that the token is valid for. - */ - private ?int $lifetime; - public function __construct( - CsrfTokenIdentityGeneratorInterface $identityGenerator, - string $secretKey, + private CsrfTokenIdentityGeneratorInterface $identityGenerator, + /** + * @var string Shared secret key used to generate the hash. + */ + private string $secretKey, string $algorithm = 'sha256', - ?int $lifetime = null + /** + * @var int|null Number of seconds that the token is valid for. + */ + private ?int $lifetime = null ) { - $this->identityGenerator = $identityGenerator; $this->mac = new Mac($algorithm); - $this->secretKey = $secretKey; - $this->lifetime = $lifetime; } public function getValue(): string @@ -91,7 +83,7 @@ private function extractData(string $token): ?array $this->secretKey, true ); - } catch (DataIsTamperedException $e) { + } catch (DataIsTamperedException) { return null; } diff --git a/src/Hmac/IdentityGenerator/SessionCsrfTokenIdentityGenerator.php b/src/Hmac/IdentityGenerator/SessionCsrfTokenIdentityGenerator.php index cfd240f..4223f31 100644 --- a/src/Hmac/IdentityGenerator/SessionCsrfTokenIdentityGenerator.php +++ b/src/Hmac/IdentityGenerator/SessionCsrfTokenIdentityGenerator.php @@ -11,11 +11,8 @@ */ final class SessionCsrfTokenIdentityGenerator implements CsrfTokenIdentityGeneratorInterface { - private SessionInterface $session; - - public function __construct(SessionInterface $session) + public function __construct(private SessionInterface $session) { - $this->session = $session; } public function generate(): string diff --git a/src/MaskedCsrfToken.php b/src/MaskedCsrfToken.php index 2498a8e..6008b06 100644 --- a/src/MaskedCsrfToken.php +++ b/src/MaskedCsrfToken.php @@ -14,11 +14,8 @@ */ final class MaskedCsrfToken implements CsrfTokenInterface { - private CsrfTokenInterface $token; - - public function __construct(CsrfTokenInterface $token) + public function __construct(private CsrfTokenInterface $token) { - $this->token = $token; } public function getValue(): string diff --git a/src/Synchronizer/Generator/RandomCsrfTokenGenerator.php b/src/Synchronizer/Generator/RandomCsrfTokenGenerator.php index 1faac8c..3cfe839 100644 --- a/src/Synchronizer/Generator/RandomCsrfTokenGenerator.php +++ b/src/Synchronizer/Generator/RandomCsrfTokenGenerator.php @@ -11,11 +11,8 @@ */ final class RandomCsrfTokenGenerator implements CsrfTokenGeneratorInterface { - private int $length; - - public function __construct(int $length = 32) + public function __construct(private int $length = 32) { - $this->length = $length; } public function generate(): string diff --git a/src/Synchronizer/Storage/CsrfTokenStorageInterface.php b/src/Synchronizer/Storage/CsrfTokenStorageInterface.php index 40e4064..8b1dd26 100644 --- a/src/Synchronizer/Storage/CsrfTokenStorageInterface.php +++ b/src/Synchronizer/Storage/CsrfTokenStorageInterface.php @@ -16,8 +16,6 @@ public function get(): ?string; /** * Write CSRF token into a storage. - * - * @param string $token */ public function set(string $token): void; diff --git a/src/Synchronizer/Storage/SessionCsrfTokenStorage.php b/src/Synchronizer/Storage/SessionCsrfTokenStorage.php index db47458..22ee800 100644 --- a/src/Synchronizer/Storage/SessionCsrfTokenStorage.php +++ b/src/Synchronizer/Storage/SessionCsrfTokenStorage.php @@ -15,20 +15,11 @@ class SessionCsrfTokenStorage implements CsrfTokenStorageInterface { public const KEY = '_csrf'; - /** - * @var string Session key used to store data. - */ - private string $key; - - private SessionInterface $session; - /** * @param string $key Session key used to store data. Default is "_csrf". */ - public function __construct(SessionInterface $session, string $key = self::KEY) + public function __construct(private SessionInterface $session, private string $key = self::KEY) { - $this->key = $key; - $this->session = $session; } public function get(): ?string diff --git a/src/Synchronizer/SynchronizerCsrfToken.php b/src/Synchronizer/SynchronizerCsrfToken.php index 36f8ff9..ca99d1d 100644 --- a/src/Synchronizer/SynchronizerCsrfToken.php +++ b/src/Synchronizer/SynchronizerCsrfToken.php @@ -21,15 +21,8 @@ */ final class SynchronizerCsrfToken implements CsrfTokenInterface { - private CsrfTokenGeneratorInterface $generator; - private CsrfTokenStorageInterface $storage; - - public function __construct( - CsrfTokenGeneratorInterface $generator, - CsrfTokenStorageInterface $storage - ) { - $this->generator = $generator; - $this->storage = $storage; + public function __construct(private CsrfTokenGeneratorInterface $generator, private CsrfTokenStorageInterface $storage) + { } public function getValue(): string diff --git a/tests/Hmac/IdentityGenerator/MockCsrfTokenIdentityGenerator.php b/tests/Hmac/IdentityGenerator/MockCsrfTokenIdentityGenerator.php index 11c3514..0f4f7ed 100644 --- a/tests/Hmac/IdentityGenerator/MockCsrfTokenIdentityGenerator.php +++ b/tests/Hmac/IdentityGenerator/MockCsrfTokenIdentityGenerator.php @@ -8,11 +8,8 @@ final class MockCsrfTokenIdentityGenerator implements CsrfTokenIdentityGeneratorInterface { - private string $identity; - - public function __construct(string $identity) + public function __construct(private string $identity) { - $this->identity = $identity; } public function generate(): string