Skip to content

Commit d58050d

Browse files
committed
feature #41175 [Security] [RememberMe] Add support for parallel requests doing remember-me re-authentication (Seldaek)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Security] [RememberMe] Add support for parallel requests doing remember-me re-authentication | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | yes ish <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40971, Fix #28314, Fix #18384 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This is a possible implementation to gather feedback mostly.. `TokenVerifierInterface` naming is kinda bad perhaps.. But my goal would be to merge it in TokenProviderInterface for 6.0 so it's not so important. Not sure if/how to best indicate this in terms of deprecation notices. Anyway wondering if this would be an acceptable implementation (ideally in an application I would probably override the new methods from DoctrineTokenProvider to something like this which is less of a hack and does expiration properly: ```php public function verifyToken(PersistentTokenInterface $token, string $tokenValue) { if (hash_equals($token->getTokenValue(), $tokenValue)) { return true; } if (!$this->cache->hasItem('rememberme-' . $token->getSeries())) { return false; } /** `@var` CacheItem $item */ $item = $this->cache->getItem('rememberme-' . $token->getSeries()); $oldToken = $item->get(); return hash_equals($oldToken, $tokenValue); } public function updateExistingToken(PersistentTokenInterface $token, string $tokenValue, \DateTimeInterface $lastUsed): void { $this->updateToken($token->getSeries(), $tokenValue, $lastUsed); /** `@var` CacheItem $item */ $item = $this->cache->getItem('rememberme-'.$token->getSeries()); $item->set($token->getTokenValue()); $item->expiresAfter(60); $this->cache->save($item); } ``` If you think it'd be fine to require optionally the cache inside DoctrineTokenProvider to enable this feature instead of the hackish way I did it, that'd be ok for me too. The current `DoctrineTokenProvider` implementation of `TokenVerifierInterface` relies on the lucky fact that series are generated using `base64_encode(random_bytes(64))` which always ends in the `==` padding of base64, so that allowed me to store an alternative token value temporarily by replacing `==` with `_`. Alternative implementation options: 1. Inject cache in `DoctrineTokenProvider` and do a proper implementation (as shown above) that way 2. Do not implement at all in `DoctrineTokenProvider` and let users who care implement this themselves. 3. Implement as a new `token_verifier` option that could be configured on the `firewall->remember_me` key so you can pass an implementation if needed, and possibly ship a default one using cache that could be autoconfigured 4. Add events that allow modifying the token to be verified, and allow receiving the newly updated token incl series, instead of TokenVerifierInterface, but then we need to inject a dispatcher in RememberMeAuthenticator. `@chalasr` `@wouterj` sorry for the long description but in the hope of getting this included in 5.3.0, if you can provide guidance I will happily work on this further tomorrow to try and wrap it up ASAP. Commits ------- 1992337d87 [Security] [RememberMe] Add support for parallel requests doing remember-me re-authentication
2 parents b7471d5 + cac8ca7 commit d58050d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

RememberMe/PersistentRememberMeHandler.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
1717
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface;
18+
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenVerifierInterface;
1819
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1920
use Symfony\Component\Security\Core\Exception\CookieTheftException;
2021
use Symfony\Component\Security\Core\User\UserInterface;
@@ -32,13 +33,18 @@
3233
final class PersistentRememberMeHandler extends AbstractRememberMeHandler
3334
{
3435
private $tokenProvider;
36+
private $tokenVerifier;
3537
private $secret;
3638

37-
public function __construct(TokenProviderInterface $tokenProvider, string $secret, UserProviderInterface $userProvider, RequestStack $requestStack, array $options, ?LoggerInterface $logger = null)
39+
public function __construct(TokenProviderInterface $tokenProvider, string $secret, UserProviderInterface $userProvider, RequestStack $requestStack, array $options, ?LoggerInterface $logger = null, ?TokenVerifierInterface $tokenVerifier = null)
3840
{
3941
parent::__construct($userProvider, $requestStack, $options, $logger);
4042

43+
if (!$tokenVerifier && $tokenProvider instanceof TokenVerifierInterface) {
44+
$tokenVerifier = $tokenProvider;
45+
}
4146
$this->tokenProvider = $tokenProvider;
47+
$this->tokenVerifier = $tokenVerifier;
4248
$this->secret = $secret;
4349
}
4450

@@ -66,7 +72,13 @@ public function processRememberMe(RememberMeDetails $rememberMeDetails, UserInte
6672

6773
[$series, $tokenValue] = explode(':', $rememberMeDetails->getValue());
6874
$persistentToken = $this->tokenProvider->loadTokenBySeries($series);
69-
if (!hash_equals($persistentToken->getTokenValue(), $tokenValue)) {
75+
76+
if ($this->tokenVerifier) {
77+
$isTokenValid = $this->tokenVerifier->verifyToken($persistentToken, $tokenValue);
78+
} else {
79+
$isTokenValid = hash_equals($persistentToken->getTokenValue(), $tokenValue);
80+
}
81+
if (!$isTokenValid) {
7082
throw new CookieTheftException('This token was already used. The account is possibly compromised.');
7183
}
7284

@@ -78,7 +90,12 @@ public function processRememberMe(RememberMeDetails $rememberMeDetails, UserInte
7890
// if multiple concurrent requests reauthenticate a user we do not want to update the token several times
7991
if ($persistentToken->getLastUsed()->getTimestamp() + 60 < time()) {
8092
$tokenValue = base64_encode(random_bytes(64));
81-
$this->tokenProvider->updateToken($series, $this->generateHash($tokenValue), new \DateTime());
93+
$tokenValueHash = $this->generateHash($tokenValue);
94+
$tokenLastUsed = new \DateTime();
95+
if ($this->tokenVerifier) {
96+
$this->tokenVerifier->updateExistingToken($persistentToken, $tokenValueHash, $tokenLastUsed);
97+
}
98+
$this->tokenProvider->updateToken($series, $tokenValueHash, $tokenLastUsed);
8299
}
83100

84101
$this->createCookie($rememberMeDetails->withValue($tokenValue));

0 commit comments

Comments
 (0)