Skip to content

Commit

Permalink
Update CsrfMiddleware.php
Browse files Browse the repository at this point in the history
use csrf trait and rename parameterName  to  formParameterName 
for the view injection also use parameterName
  • Loading branch information
TiaNex-Com authored Oct 6, 2024
1 parent 119bc63 commit 0f9fda1
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/CsrfMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Http\Method;
use Yiisoft\Http\Status;
use Yiisoft\Csrf\CsrfTrait;

use function in_array;
use function is_string;
Expand All @@ -21,12 +22,8 @@
* @link https://www.php-fig.org/psr/psr-15/
*/
final class CsrfMiddleware 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;
{
use CsrfTrait;

Check failure on line 26 in src/CsrfMiddleware.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedTrait

src/CsrfMiddleware.php:26:9: UndefinedTrait: Trait Yiisoft\Csrf\CsrfTrait does not exist (see https://psalm.dev/023)

Check failure on line 26 in src/CsrfMiddleware.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedTrait

src/CsrfMiddleware.php:26:9: UndefinedTrait: Trait Yiisoft\Csrf\CsrfTrait does not exist (see https://psalm.dev/023)

Check failure on line 26 in src/CsrfMiddleware.php

View workflow job for this annotation

GitHub Actions / psalm83 / PHP 8.3-ubuntu-latest

UndefinedTrait

src/CsrfMiddleware.php:26:9: UndefinedTrait: Trait Yiisoft\Csrf\CsrfTrait does not exist (see https://psalm.dev/023)

Check failure on line 26 in src/CsrfMiddleware.php

View workflow job for this annotation

GitHub Actions / psalm74-80 / PHP 7.4-ubuntu-latest

UndefinedTrait

src/CsrfMiddleware.php:26:9: UndefinedTrait: Trait Yiisoft\Csrf\CsrfTrait does not exist (see https://psalm.dev/023)

Check failure on line 26 in src/CsrfMiddleware.php

View workflow job for this annotation

GitHub Actions / psalm74-80 / PHP 8.0-ubuntu-latest

UndefinedTrait

src/CsrfMiddleware.php:26:9: UndefinedTrait: Trait Yiisoft\Csrf\CsrfTrait does not exist (see https://psalm.dev/023)

private ResponseFactoryInterface $responseFactory;
private CsrfTokenInterface $token;
Expand Down Expand Up @@ -59,29 +56,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
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
{
Expand All @@ -98,7 +73,7 @@ private function getTokenFromRequest(ServerRequestInterface $request): ?string
{
$parsedBody = $request->getParsedBody();

$token = $parsedBody[$this->parameterName] ?? null;
$token = $parsedBody[$this->formParameterName] ?? null;
if (empty($token)) {
$headers = $request->getHeader($this->headerName);
$token = reset($headers);
Expand Down

0 comments on commit 0f9fda1

Please sign in to comment.