Skip to content

Commit

Permalink
minor #32285 [CSRF] add more parameter types (Tobion)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.0-dev branch.

Discussion
----------

[CSRF] add more parameter types

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | /no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32179
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Those have been missing in #32208

Commits
-------

d442028 [CSRF] add more parameter types
  • Loading branch information
Tobion committed Jul 2, 2019
2 parents f800d01 + d442028 commit 393f9ae
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Csrf/CsrfTokenManager.php
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
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
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
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
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
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);
}

0 comments on commit 393f9ae

Please sign in to comment.