Skip to content

Commit

Permalink
Add CSRF Manager support to return token query
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Nov 2, 2023
1 parent a0535d8 commit 9820b2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Security/Csrf/CsrfManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ public function getToken(?string $key = null): string
return $value;
}

/**
* Return the token query to be used in query string
* @param string|null $key
* @return array<string, string>
*/
public function getTokenQuery(?string $key = null): array
{
$token = $this->getToken($key);

if ($key === null) {
$key = $this->getConfigValue('key');
}

return [$key => $token];
}

/**
* Clear all CSRF data from storage
* @return void
Expand Down
19 changes: 19 additions & 0 deletions tests/Security/Csrf/CsrfManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,23 @@ public function testValidateIsUnique(): void
$o->clear();
$this->assertNull($storage->get('csrf_key'));
}

public function testGetTokenQuery(): void
{
$storage = new CsrfNullStorage();
$storage->set('csrf_key', 'bar', time() + 1000);
$config = $this->getMockInstance(Config::class, [
'get' => [
'key' => 'csrf_key',
'expire' => 600,
]
]);

$o = new CsrfManager($config, $storage);

$queries = $o->getTokenQuery();
$this->assertCount(1, $queries);
$this->assertArrayHasKey('csrf_key', $queries);
$this->assertEquals('bar', $queries['csrf_key']);
}
}

0 comments on commit 9820b2c

Please sign in to comment.