Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a custom failure handler feature #30

Merged
merged 9 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/CsrfMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Csrf;

use Closure;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -30,24 +31,37 @@ final class CsrfMiddleware implements MiddlewareInterface

private ResponseFactoryInterface $responseFactory;
private CsrfTokenInterface $token;
private ?Closure $failureHandler;
samdark marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(
ResponseFactoryInterface $responseFactory,
CsrfTokenInterface $token
CsrfTokenInterface $token,
?Closure $failureHandler = null
) {
$this->responseFactory = $responseFactory;
$this->token = $token;
$this->failureHandler = $failureHandler;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->validateCsrfToken($request)) {
return $this->handleFailure();
}

return $handler->handle($request);
}

private function handleFailure(): ResponseInterface
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_null($handler = $this->failureHandler)) {
$response = $this->responseFactory->createResponse(Status::UNPROCESSABLE_ENTITY);
$response->getBody()->write(Status::TEXTS[Status::UNPROCESSABLE_ENTITY]);
return $response;
}

return $handler->handle($request);
/** @var Closure():ResponseInterface $handler */
return $handler();
}

public function withParameterName(string $name): self
Expand Down
29 changes: 26 additions & 3 deletions tests/TokenCsrfMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Csrf\Tests;

use Closure;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
Expand Down Expand Up @@ -98,6 +99,25 @@ public function testInvalidTokenResultIn422(): void
$this->assertEquals(Status::UNPROCESSABLE_ENTITY, $response->getStatusCode());
}

public function testInvalidTokenResultWithCustomFailureHandler(): void
{
$failureHandler = function () {
$response = new Response(Status::BAD_REQUEST);
$response->getBody()->write(Status::TEXTS[Status::BAD_REQUEST]);
return $response;
};

$middleware = $this->createCsrfMiddleware(null, $failureHandler);

$response = $middleware->process(
$this->createPostServerRequestWithBodyToken(Random::string()),
$this->createRequestHandler()
);

$this->assertEquals(Status::TEXTS[Status::BAD_REQUEST], $response->getBody());
$this->assertEquals(Status::BAD_REQUEST, $response->getStatusCode());
}

public function testEmptyTokenInRequestResultIn422(): void
{
$middleware = $this->createCsrfMiddleware();
Expand Down Expand Up @@ -155,12 +175,15 @@ private function getBodyRequestParamsByToken(string $token): array
];
}

protected function createCsrfMiddleware(?CsrfTokenInterface $csrfToken = null): CsrfMiddleware
{
protected function createCsrfMiddleware(
?CsrfTokenInterface $csrfToken = null,
?Closure $failureHandler = null
): CsrfMiddleware {

$csrfToken = new MaskedCsrfToken($csrfToken ?? $this->createCsrfToken());
$this->token = $csrfToken->getValue();

$middleware = new CsrfMiddleware(new Psr17Factory(), $csrfToken);
$middleware = new CsrfMiddleware(new Psr17Factory(), $csrfToken, $failureHandler);

return $middleware->withParameterName(self::PARAM_NAME);
}
Expand Down