From 98c25d83d722347ab84b162bb370f3745e429fa1 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 17 Mar 2022 00:57:29 +0200 Subject: [PATCH 1/3] Update AuthHttp code with php 8.1 features --- .../src/Exception/TransportException.php | 7 --- src/AuthHttp/src/HttpTransportInterface.php | 9 --- .../src/Middleware/AuthMiddleware.php | 38 ++----------- .../Middleware/Firewall/AbstractFirewall.php | 7 --- .../Middleware/Firewall/ExceptionFirewall.php | 18 +----- .../Middleware/Firewall/OverwriteFirewall.php | 24 ++------ .../src/Transport/CookieTransport.php | 56 ++----------------- .../src/Transport/HeaderTransport.php | 30 ++-------- src/AuthHttp/src/TransportRegistry.php | 20 ++----- 9 files changed, 26 insertions(+), 183 deletions(-) diff --git a/src/AuthHttp/src/Exception/TransportException.php b/src/AuthHttp/src/Exception/TransportException.php index 3884ba1c6..4c6da07b3 100644 --- a/src/AuthHttp/src/Exception/TransportException.php +++ b/src/AuthHttp/src/Exception/TransportException.php @@ -1,12 +1,5 @@ scope = $scope; - $this->actorProvider = $actorProvider; - $this->tokenStorage = $tokenStorage; - $this->transportRegistry = $transportRegistry; } /** - * * @throws \Throwable */ public function process(Request $request, RequestHandlerInterface $handler): Response @@ -65,9 +39,7 @@ public function process(Request $request, RequestHandlerInterface $handler): Res $response = $this->scope->runScope( [AuthContextInterface::class => $authContext], - static function () use ($request, $handler, $authContext) { - return $handler->handle($request->withAttribute(self::ATTRIBUTE, $authContext)); - } + static fn() => $handler->handle($request->withAttribute(self::ATTRIBUTE, $authContext)) ); return $this->closeContext($request, $response, $authContext); diff --git a/src/AuthHttp/src/Middleware/Firewall/AbstractFirewall.php b/src/AuthHttp/src/Middleware/Firewall/AbstractFirewall.php index b55a66eb8..96bdb658e 100644 --- a/src/AuthHttp/src/Middleware/Firewall/AbstractFirewall.php +++ b/src/AuthHttp/src/Middleware/Firewall/AbstractFirewall.php @@ -1,12 +1,5 @@ e = $e; + public function __construct( + private readonly \Throwable $e + ) { } /** - * @inheritDoc - * * @throws \Throwable */ protected function denyAccess(Request $request, RequestHandlerInterface $handler): Response diff --git a/src/AuthHttp/src/Middleware/Firewall/OverwriteFirewall.php b/src/AuthHttp/src/Middleware/Firewall/OverwriteFirewall.php index 1a03ea379..e47cb91f2 100644 --- a/src/AuthHttp/src/Middleware/Firewall/OverwriteFirewall.php +++ b/src/AuthHttp/src/Middleware/Firewall/OverwriteFirewall.php @@ -1,12 +1,5 @@ uri = $uri; - $this->status = $status; + public function __construct( + private readonly UriInterface $uri, + private readonly int $status = 401 + ) { } - /** - * @inheritDoc - */ protected function denyAccess(Request $request, RequestHandlerInterface $handler): Response { return $handler->handle($request->withUri($this->uri))->withStatus($this->status); diff --git a/src/AuthHttp/src/Transport/CookieTransport.php b/src/AuthHttp/src/Transport/CookieTransport.php index 83ee71621..cf7ee7f8c 100644 --- a/src/AuthHttp/src/Transport/CookieTransport.php +++ b/src/AuthHttp/src/Transport/CookieTransport.php @@ -1,13 +1,5 @@ cookie = $cookie; - $this->basePath = $basePath; - $this->domain = $domain; - $this->secure = $secure; - $this->httpOnly = $httpOnly; - $this->sameSite = $sameSite; } - /** - * @inheritDoc - */ public function fetchToken(Request $request): ?string { $cookies = $request->getCookieParams(); return $cookies[$this->cookie] ?? null; } - /** - * @inheritDoc - */ public function commitToken( Request $request, Response $response, @@ -111,18 +73,12 @@ public function commitToken( return $response; } - /** - * @inheritDoc - */ public function removeToken(Request $request, Response $response, string $tokenID): Response { // reset to null return $this->commitToken($request, $response, null, null); } - /** - * @param \DateTimeInterface|null $expiresAt - */ private function getLifetime(\DateTimeInterface $expiresAt = null): ?int { if ($expiresAt === null) { diff --git a/src/AuthHttp/src/Transport/HeaderTransport.php b/src/AuthHttp/src/Transport/HeaderTransport.php index 2f1ee2d65..218a78791 100644 --- a/src/AuthHttp/src/Transport/HeaderTransport.php +++ b/src/AuthHttp/src/Transport/HeaderTransport.php @@ -1,12 +1,5 @@ header = $header; - $this->valueFormat = $valueFormat; + public function __construct( + private readonly string $header = 'X-Auth-Token', + private readonly string $valueFormat = '%s' + ) { } - /** - * @inheritDoc - */ public function fetchToken(Request $request): ?string { if ($request->hasHeader($this->header)) { @@ -44,9 +28,6 @@ public function fetchToken(Request $request): ?string return null; } - /** - * @inheritDoc - */ public function commitToken( Request $request, Response $response, @@ -60,9 +41,6 @@ public function commitToken( return $response->withAddedHeader($this->header, sprintf($this->valueFormat, $tokenID)); } - /** - * @inheritDoc - */ public function removeToken(Request $request, Response $response, string $tokenID): Response { return $response; diff --git a/src/AuthHttp/src/TransportRegistry.php b/src/AuthHttp/src/TransportRegistry.php index f727b3e82..4239cf7c8 100644 --- a/src/AuthHttp/src/TransportRegistry.php +++ b/src/AuthHttp/src/TransportRegistry.php @@ -1,12 +1,5 @@ transports[$name] = $transport; } - /** - * @param string|null $name - */ public function getTransport(string $name = null): HttpTransportInterface { - $name = $name ?? $this->default; + $name ??= $this->default; if (!isset($this->transports[$name])) { - throw new TransportException("Undefined auth transport {$name}"); + throw new TransportException(sprintf('Undefined auth transport %s', $name)); } return $this->transports[$name]; From 8f8da0ecf03e039fc08c463eb0cc5d732beab836 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 24 Mar 2022 18:41:45 +0400 Subject: [PATCH 2/3] Add namespace --- src/AuthHttp/src/TransportRegistry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AuthHttp/src/TransportRegistry.php b/src/AuthHttp/src/TransportRegistry.php index 4239cf7c8..252e57529 100644 --- a/src/AuthHttp/src/TransportRegistry.php +++ b/src/AuthHttp/src/TransportRegistry.php @@ -30,7 +30,7 @@ public function getTransport(string $name = null): HttpTransportInterface $name ??= $this->default; if (!isset($this->transports[$name])) { - throw new TransportException(sprintf('Undefined auth transport %s', $name)); + throw new TransportException(\sprintf('Undefined auth transport %s', $name)); } return $this->transports[$name]; From 8e09549aaabe98c1298b0fd7c991e1e2630f518b Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 24 Mar 2022 18:43:53 +0400 Subject: [PATCH 3/3] Fix CS --- src/AuthHttp/src/Middleware/AuthMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AuthHttp/src/Middleware/AuthMiddleware.php b/src/AuthHttp/src/Middleware/AuthMiddleware.php index e86573477..28e56e4dc 100644 --- a/src/AuthHttp/src/Middleware/AuthMiddleware.php +++ b/src/AuthHttp/src/Middleware/AuthMiddleware.php @@ -39,7 +39,7 @@ public function process(Request $request, RequestHandlerInterface $handler): Res $response = $this->scope->runScope( [AuthContextInterface::class => $authContext], - static fn() => $handler->handle($request->withAttribute(self::ATTRIBUTE, $authContext)) + static fn () => $handler->handle($request->withAttribute(self::ATTRIBUTE, $authContext)) ); return $this->closeContext($request, $response, $authContext);