diff --git a/composer.json b/composer.json index 1f027278..48a32855 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "symfony/http-kernel": "^6.4 || ^7.0", "symfony/property-access": "^6.4 || ^7.0", "symfony/security-bundle": "^6.4 || ^7.0", + "symfony/service-contracts": "^2.5|^3", "symfony/twig-bundle": "^6.4 || ^7.0" }, "require-dev": { diff --git a/src/bundle/composer.json b/src/bundle/composer.json index c13d0140..d228bd53 100644 --- a/src/bundle/composer.json +++ b/src/bundle/composer.json @@ -22,6 +22,7 @@ "symfony/http-kernel": "^6.4 || ^7.0", "symfony/property-access": "^6.4 || ^7.0", "symfony/security-bundle": "^6.4 || ^7.0", + "symfony/service-contracts": "^2.5|^3", "symfony/twig-bundle": "^6.4 || ^7.0" }, "autoload": { diff --git a/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php b/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php index 821db439..d7555f60 100644 --- a/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php +++ b/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php @@ -7,6 +7,7 @@ use RuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Contracts\Service\ResetInterface; use function array_map; use function explode; use function implode; @@ -14,7 +15,7 @@ /** * @final */ -class TrustedDeviceTokenStorage +class TrustedDeviceTokenStorage implements ResetInterface { private const TOKEN_DELIMITER = ';'; @@ -100,6 +101,12 @@ public function clearTrustedToken(string $username, string $firewall): void $this->updateCookie = true; } + public function reset(): void + { + $this->updateCookie = false; + $this->trustedTokenList = null; + } + /** * @return TrustedDeviceToken[] */ diff --git a/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php b/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php index 411099e3..6c8843b7 100644 --- a/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php +++ b/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php @@ -337,4 +337,32 @@ public function getCookieValue_hasTokenCalledWithInvalidToken_returnSerializedWi $returnValue = $this->tokenStorage->getCookieValue(); $this->assertEquals('validToken', $returnValue); } + + /** + * @test + */ + public function reset_cookiePreviouslyUpdated_resetUpdatedCookie(): void + { + $this->tokenStorage->addTrustedToken('username', 'firewallName', 1); + $this->assertTrue($this->tokenStorage->hasUpdatedCookie()); + + $this->tokenStorage->reset(); + $this->assertFalse($this->tokenStorage->hasUpdatedCookie()); + } + + /** + * @test + */ + public function reset_cookiePreviouslyUpdated_resetCookieList(): void + { + $this->stubCookieHasToken('serializedToken'); + $this->stubDecodeToken( + $this->createTokenWithProperties('serializedToken', true, true, false), + ); + $this->assertEquals('serializedToken', $this->tokenStorage->getCookieValue()); + + $this->request->cookies->remove('cookieName'); + $this->tokenStorage->reset(); + $this->assertEmpty($this->tokenStorage->getCookieValue()); + } }