Skip to content

Commit

Permalink
Updating AuthHttp code with PHP 8.1 features (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Mar 24, 2022
1 parent 21b6b3c commit 06a5b93
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 183 deletions.
7 changes: 0 additions & 7 deletions src/AuthHttp/src/Exception/TransportException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Exception;
Expand Down
9 changes: 0 additions & 9 deletions src/AuthHttp/src/HttpTransportInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth;
Expand All @@ -26,8 +19,6 @@ public function fetchToken(Request $request): ?string;

/**
* Commit (write) token to the outgoing response.
*
* @param \DateTimeInterface|null $expiresAt
*/
public function commitToken(
Request $request,
Expand Down
38 changes: 5 additions & 33 deletions src/AuthHttp/src/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -31,32 +22,15 @@ final class AuthMiddleware implements MiddlewareInterface
{
public const ATTRIBUTE = 'authContext';

/** @var ScopeInterface */
private $scope;

/** @var ActorProviderInterface */
private $actorProvider;

/** @var TokenStorageInterface */
private $tokenStorage;

/** @var TransportRegistry */
private $transportRegistry;

public function __construct(
ScopeInterface $scope,
ActorProviderInterface $actorProvider,
TokenStorageInterface $tokenStorage,
TransportRegistry $transportRegistry
private readonly ScopeInterface $scope,
private readonly ActorProviderInterface $actorProvider,
private readonly TokenStorageInterface $tokenStorage,
private readonly TransportRegistry $transportRegistry
) {
$this->scope = $scope;
$this->actorProvider = $actorProvider;
$this->tokenStorage = $tokenStorage;
$this->transportRegistry = $transportRegistry;
}

/**
*
* @throws \Throwable
*/
public function process(Request $request, RequestHandlerInterface $handler): Response
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/AuthHttp/src/Middleware/Firewall/AbstractFirewall.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Middleware\Firewall;
Expand Down
18 changes: 3 additions & 15 deletions src/AuthHttp/src/Middleware/Firewall/ExceptionFirewall.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Middleware\Firewall;
Expand All @@ -20,17 +13,12 @@
*/
final class ExceptionFirewall extends AbstractFirewall
{
/** @var \Throwable */
private $e;

public function __construct(\Throwable $e)
{
$this->e = $e;
public function __construct(
private readonly \Throwable $e
) {
}

/**
* @inheritDoc
*
* @throws \Throwable
*/
protected function denyAccess(Request $request, RequestHandlerInterface $handler): Response
Expand Down
24 changes: 4 additions & 20 deletions src/AuthHttp/src/Middleware/Firewall/OverwriteFirewall.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Middleware\Firewall;
Expand All @@ -21,21 +14,12 @@
*/
final class OverwriteFirewall extends AbstractFirewall
{
/** @var UriInterface */
private $uri;

/** @var int */
private $status;

public function __construct(UriInterface $uri, int $status = 401)
{
$this->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);
Expand Down
56 changes: 6 additions & 50 deletions src/AuthHttp/src/Transport/CookieTransport.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Auth\Transport;
Expand All @@ -23,52 +15,22 @@
*/
final class CookieTransport implements HttpTransportInterface
{
/** @var string */
private $cookie;

/** @var string */
private $basePath;

/** @var string|null */
private $domain;

/** @var bool */
private $secure;

/** @var bool */
private $httpOnly;

/** @var string|null */
private $sameSite;

public function __construct(
string $cookie,
string $basePath = '/',
?string $domain = null,
bool $secure = false,
bool $httpOnly = true,
?string $sameSite = null
private string $cookie,
private readonly string $basePath = '/',
private readonly ?string $domain = null,
private readonly bool $secure = false,
private readonly bool $httpOnly = true,
private readonly ?string $sameSite = null
) {
$this->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,
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 4 additions & 26 deletions src/AuthHttp/src/Transport/HeaderTransport.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth\Transport;
Expand All @@ -20,21 +13,12 @@
*/
final class HeaderTransport implements HttpTransportInterface
{
/** @var string */
private $header;

/** @var string */
private $valueFormat;

public function __construct(string $header = 'X-Auth-Token', string $valueFormat = '%s')
{
$this->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)) {
Expand All @@ -44,9 +28,6 @@ public function fetchToken(Request $request): ?string
return null;
}

/**
* @inheritDoc
*/
public function commitToken(
Request $request,
Response $response,
Expand All @@ -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;
Expand Down
20 changes: 4 additions & 16 deletions src/AuthHttp/src/TransportRegistry.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Auth;
Expand All @@ -19,10 +12,8 @@
final class TransportRegistry
{
/** @var HttpTransportInterface[] */
private $transports = [];

/** @var string */
private $default;
private array $transports = [];
private ?string $default = null;

public function setDefaultTransport(string $name): void
{
Expand All @@ -34,15 +25,12 @@ public function setTransport(string $name, HttpTransportInterface $transport): v
$this->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];
Expand Down

0 comments on commit 06a5b93

Please sign in to comment.