Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CSRF] add more parameter types #32285

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

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Csrf/CsrfTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function isTokenValid(CsrfToken $token)
return hash_equals($this->storage->getToken($namespacedId), $token->getValue());
}

private function getNamespace()
private function getNamespace(): string
{
return \is_callable($ns = $this->namespace) ? $ns() : $ns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function refreshToken(string $tokenId);
/**
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function testRemoveToken($namespace, $manager, $storage)
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();
Expand All @@ -169,6 +170,7 @@ public function testNamespaced()

$token = $manager->getToken('foo');
$this->assertSame('foo', $token->getId());
$this->assertSame('random', $token->getValue());
}

public function getManagerGeneratorAndStorage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(string $namespace = self::SESSION_NAMESPACE)
/**
* {@inheritdoc}
*/
public function getToken($tokenId)
public function getToken(string $tokenId)
{
if (!$this->sessionStarted) {
$this->startSession();
Expand All @@ -57,19 +57,19 @@ public function getToken($tokenId)
/**
* {@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();
Expand All @@ -81,7 +81,7 @@ public function hasToken($tokenId)
/**
* {@inheritdoc}
*/
public function removeToken($tokenId)
public function removeToken(string $tokenId)
{
if (!$this->sessionStarted) {
$this->startSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(SessionInterface $session, string $namespace = self:
/**
* {@inheritdoc}
*/
public function getToken($tokenId)
public function getToken(string $tokenId)
{
if (!$this->session->isStarted()) {
$this->session->start();
Expand All @@ -60,19 +60,19 @@ public function getToken($tokenId)
/**
* {@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();
Expand All @@ -84,7 +84,7 @@ public function hasToken($tokenId)
/**
* {@inheritdoc}
*/
public function removeToken($tokenId)
public function removeToken(string $tokenId)
{
if (!$this->session->isStarted()) {
$this->session->start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}