[CSRF] add more parameter types
This commit is contained in:
parent
38b9b951a4
commit
d442028063
@ -114,7 +114,7 @@ class CsrfTokenManager implements CsrfTokenManagerInterface
|
||||
return hash_equals($this->storage->getToken($namespacedId), $token->getValue());
|
||||
}
|
||||
|
||||
private function getNamespace()
|
||||
private function getNamespace(): string
|
||||
{
|
||||
return \is_callable($ns = $this->namespace) ? $ns() : $ns;
|
||||
}
|
||||
|
@ -49,8 +49,6 @@ interface CsrfTokenManagerInterface
|
||||
/**
|
||||
* Invalidates the CSRF token with the given ID, if one exists.
|
||||
*
|
||||
* @param string $tokenId The token ID
|
||||
*
|
||||
* @return string|null Returns the removed token value if one existed, NULL
|
||||
* otherwise
|
||||
*/
|
||||
|
@ -160,6 +160,7 @@ class CsrfTokenManagerTest extends TestCase
|
||||
public function testNamespaced()
|
||||
{
|
||||
$generator = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock();
|
||||
$generator->expects($this->once())->method('generateToken')->willReturn('random');
|
||||
$storage = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock();
|
||||
|
||||
$requestStack = new RequestStack();
|
||||
@ -169,6 +170,7 @@ class CsrfTokenManagerTest extends TestCase
|
||||
|
||||
$token = $manager->getToken('foo');
|
||||
$this->assertSame('foo', $token->getId());
|
||||
$this->assertSame('random', $token->getValue());
|
||||
}
|
||||
|
||||
public function getManagerGeneratorAndStorage()
|
||||
|
@ -41,7 +41,7 @@ class NativeSessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getToken($tokenId)
|
||||
public function getToken(string $tokenId)
|
||||
{
|
||||
if (!$this->sessionStarted) {
|
||||
$this->startSession();
|
||||
@ -57,19 +57,19 @@ class NativeSessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setToken($tokenId, $token)
|
||||
public function setToken(string $tokenId, string $token)
|
||||
{
|
||||
if (!$this->sessionStarted) {
|
||||
$this->startSession();
|
||||
}
|
||||
|
||||
$_SESSION[$this->namespace][$tokenId] = (string) $token;
|
||||
$_SESSION[$this->namespace][$tokenId] = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasToken($tokenId)
|
||||
public function hasToken(string $tokenId)
|
||||
{
|
||||
if (!$this->sessionStarted) {
|
||||
$this->startSession();
|
||||
@ -81,7 +81,7 @@ class NativeSessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeToken($tokenId)
|
||||
public function removeToken(string $tokenId)
|
||||
{
|
||||
if (!$this->sessionStarted) {
|
||||
$this->startSession();
|
||||
|
@ -44,7 +44,7 @@ class SessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getToken($tokenId)
|
||||
public function getToken(string $tokenId)
|
||||
{
|
||||
if (!$this->session->isStarted()) {
|
||||
$this->session->start();
|
||||
@ -60,19 +60,19 @@ class SessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setToken($tokenId, $token)
|
||||
public function setToken(string $tokenId, string $token)
|
||||
{
|
||||
if (!$this->session->isStarted()) {
|
||||
$this->session->start();
|
||||
}
|
||||
|
||||
$this->session->set($this->namespace.'/'.$tokenId, (string) $token);
|
||||
$this->session->set($this->namespace.'/'.$tokenId, $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasToken($tokenId)
|
||||
public function hasToken(string $tokenId)
|
||||
{
|
||||
if (!$this->session->isStarted()) {
|
||||
$this->session->start();
|
||||
@ -84,7 +84,7 @@ class SessionTokenStorage implements ClearableTokenStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeToken($tokenId)
|
||||
public function removeToken(string $tokenId)
|
||||
{
|
||||
if (!$this->session->isStarted()) {
|
||||
$this->session->start();
|
||||
|
@ -21,38 +21,29 @@ interface TokenStorageInterface
|
||||
/**
|
||||
* Reads a stored CSRF token.
|
||||
*
|
||||
* @param string $tokenId The token ID
|
||||
*
|
||||
* @return string The stored token
|
||||
*
|
||||
* @throws \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException If the token ID does not exist
|
||||
*/
|
||||
public function getToken($tokenId);
|
||||
public function getToken(string $tokenId);
|
||||
|
||||
/**
|
||||
* Stores a CSRF token.
|
||||
*
|
||||
* @param string $tokenId The token ID
|
||||
* @param string $token The CSRF token
|
||||
*/
|
||||
public function setToken($tokenId, $token);
|
||||
public function setToken(string $tokenId, string $token);
|
||||
|
||||
/**
|
||||
* Removes a CSRF token.
|
||||
*
|
||||
* @param string $tokenId The token ID
|
||||
*
|
||||
* @return string|null Returns the removed token if one existed, NULL
|
||||
* otherwise
|
||||
*/
|
||||
public function removeToken($tokenId);
|
||||
public function removeToken(string $tokenId);
|
||||
|
||||
/**
|
||||
* Checks whether a token with the given token ID exists.
|
||||
*
|
||||
* @param string $tokenId The token ID
|
||||
*
|
||||
* @return bool Whether a token exists with the given ID
|
||||
*/
|
||||
public function hasToken($tokenId);
|
||||
public function hasToken(string $tokenId);
|
||||
}
|
||||
|
Reference in New Issue
Block a user