Skip to content

Commit

Permalink
Update CSRF template tag to support query string
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Dec 6, 2023
1 parent 4521e5f commit 483e3b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Auth/Entity/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function mapEntity(EntityMapperInterface $mapper): void
'created_at' => 'date',
'updated_at' => '?date',
]);
$mapper->relation('roles')->shareMany(Role::class);

$mapper->relation('roles')->shareMany(Role::class);
}
}
16 changes: 16 additions & 0 deletions src/Template/Tag/CsrfTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Platine\Framework\Security\Csrf\CsrfManager;
use Platine\Template\Parser\AbstractTag;
use Platine\Template\Parser\Context;
use Platine\Template\Parser\Parser;

/**
* @class CsrfTag
Expand All @@ -59,6 +60,15 @@
*/
class CsrfTag extends AbstractTag
{
/**
* {@inheritdoc}
*/
public function __construct(string $markup, &$tokens, Parser $parser)
{
parent::__construct($markup, $tokens, $parser);
$this->extractAttributes($markup);
}

/**
* {@inheritdoc}
*/
Expand All @@ -70,6 +80,12 @@ public function render(Context $context): string
/** @var CsrfManager<T> $csrfManager */
$csrfManager = app(CsrfManager::class);

$query = array_key_exists('query', $this->attributes);

if ($query) {
return http_build_query($csrfManager->getTokenQuery());
}

$key = $config->get('security.csrf.key', '');
$token = $csrfManager->getToken();

Expand Down
23 changes: 23 additions & 0 deletions tests/Template/Tag/CsrfTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,27 @@ public function testRender(): void

$this->assertEquals('<input type = "hidden" name = "csrf" value = "foo" />', $o->render($context));
}

public function testRenderUsingQueryString(): void
{
global $mock_app_to_instance,
$mock_app_config_items,
$mock_sha1_foo;

$mock_sha1_foo = true;
$mock_app_to_instance = true;

$mock_app_config_items = [
'security.csrf' => ['expire' => 400, 'key' => 'csrf'],
'security.csrf.key' => 'csrf',
];

$parser = $this->getMockInstance(Parser::class);
$context = $this->getMockInstance(Context::class);

$tokens = ['tnh', '{% endcapture %}'];
$o = new CsrfTag('myname query:1', $tokens, $parser);

$this->assertEquals('csrf=foo', $o->render($context));
}
}
3 changes: 1 addition & 2 deletions tests/Template/Tag/RouteUrlTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class RouteUrlTagTest extends PlatineTestCase
public function testConstructWrongSynthax(): void
{
$parser = $this->getMockInstance(Parser::class);
$context = $this->getMockInstance(Context::class);

$tokens = [];
$this->expectException(ParseException::class);
$o = new RouteUrlTag('', $tokens, $parser);
(new RouteUrlTag('', $tokens, $parser));
}


Expand Down

0 comments on commit 483e3b4

Please sign in to comment.