generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 65-csrf-header
- Loading branch information
Showing
10 changed files
with
455 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Csrf; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Yiisoft\Http\Method; | ||
use Yiisoft\Http\Status; | ||
|
||
use function in_array; | ||
use function is_string; | ||
|
||
/** | ||
* PSR-15 middleware that takes care of token validation. | ||
* | ||
* @link https://www.php-fig.org/psr/psr-15/ | ||
*/ | ||
final class CsrfTokenMiddleware implements MiddlewareInterface | ||
{ | ||
public const PARAMETER_NAME = '_csrf'; | ||
public const HEADER_NAME = 'X-CSRF-Token'; | ||
|
||
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 process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if ($this->validateCsrfToken($request)) { | ||
return $handler->handle($request); | ||
} | ||
|
||
if ($this->failureHandler !== null) { | ||
return $this->failureHandler->handle($request); | ||
} | ||
|
||
$response = $this->responseFactory->createResponse(Status::UNPROCESSABLE_ENTITY); | ||
$response | ||
->getBody() | ||
->write(Status::TEXTS[Status::UNPROCESSABLE_ENTITY]); | ||
return $response; | ||
} | ||
|
||
public function withParameterName(string $name): self | ||
{ | ||
$new = clone $this; | ||
$new->parameterName = $name; | ||
return $new; | ||
} | ||
|
||
public function withHeaderName(string $name): self | ||
{ | ||
$new = clone $this; | ||
$new->headerName = $name; | ||
return $new; | ||
} | ||
|
||
public function getParameterName(): string | ||
{ | ||
return $this->parameterName; | ||
} | ||
|
||
public function getHeaderName(): string | ||
{ | ||
return $this->headerName; | ||
} | ||
|
||
private function validateCsrfToken(ServerRequestInterface $request): bool | ||
{ | ||
if (in_array($request->getMethod(), [Method::GET, Method::HEAD, Method::OPTIONS], true)) { | ||
return true; | ||
} | ||
|
||
$token = $this->getTokenFromRequest($request); | ||
|
||
return !empty($token) && $this->token->validate($token); | ||
} | ||
|
||
private function getTokenFromRequest(ServerRequestInterface $request): ?string | ||
{ | ||
$parsedBody = $request->getParsedBody(); | ||
|
||
$token = $parsedBody[$this->parameterName] ?? null; | ||
if (empty($token)) { | ||
$headers = $request->getHeader($this->headerName); | ||
$token = reset($headers); | ||
} | ||
|
||
return is_string($token) ? $token : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Csrf\Tests; | ||
|
||
use Nyholm\Psr7\Factory\Psr17Factory; | ||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Csrf\CsrfMiddleware; | ||
use Yiisoft\Csrf\Synchronizer\Generator\RandomCsrfTokenGenerator; | ||
use Yiisoft\Csrf\Synchronizer\SynchronizerCsrfToken; | ||
use Yiisoft\Csrf\Tests\Synchronizer\Storage\MockCsrfTokenStorage; | ||
|
||
final class DeprecatedCsrfMiddlewareTest extends TestCase | ||
{ | ||
public function testDefaultParameterName(): void | ||
{ | ||
$middleware = $this->createMiddleware(); | ||
$this->assertSame(CsrfMiddleware::PARAMETER_NAME, $middleware->getParameterName()); | ||
} | ||
|
||
public function testGetParameterName(): void | ||
{ | ||
$middleware = $this | ||
->createMiddleware() | ||
->withParameterName('my-csrf'); | ||
$this->assertSame('my-csrf', $middleware->getParameterName()); | ||
} | ||
|
||
public function testDefaultHeaderName(): void | ||
{ | ||
$middleware = $this->createMiddleware(); | ||
$this->assertSame(CsrfMiddleware::HEADER_NAME, $middleware->getHeaderName()); | ||
} | ||
|
||
public function testGetHeaderName(): void | ||
{ | ||
$middleware = $this | ||
->createMiddleware() | ||
->withHeaderName('MY-CSRF'); | ||
$this->assertSame('MY-CSRF', $middleware->getHeaderName()); | ||
} | ||
|
||
public function testImmutability(): void | ||
{ | ||
$original = $this->createMiddleware(); | ||
$this->assertNotSame($original, $original->withHeaderName('csrf')); | ||
$this->assertNotSame($original, $original->withParameterName('csrf')); | ||
} | ||
|
||
private function createMiddleware(): CsrfMiddleware | ||
{ | ||
return new CsrfMiddleware( | ||
new Psr17Factory(), | ||
new SynchronizerCsrfToken( | ||
new RandomCsrfTokenGenerator(), | ||
new MockCsrfTokenStorage() | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.