diff --git a/Security/TwoFactor/Trusted/TrustedFilter.php b/Security/TwoFactor/Trusted/TrustedFilter.php index 0e45bccc..27cdb6cb 100644 --- a/Security/TwoFactor/Trusted/TrustedFilter.php +++ b/Security/TwoFactor/Trusted/TrustedFilter.php @@ -57,13 +57,13 @@ public function beginAuthentication(AuthenticationContextInterface $context) { $request = $context->getRequest(); $user = $context->getUser(); + $context->setUseTrustedOption($this->useTrustedOption); // Skip two-factor authentication on trusted computers - if ($this->useTrustedOption && $this->cookieManager->isTrustedComputer($request, $user)) { + if ($context->useTrustedOption() && $this->cookieManager->isTrustedComputer($request, $user)) { return; } - $context->setUseTrustedOption($this->useTrustedOption); // Set trusted flag $this->authHandler->beginAuthentication($context); } diff --git a/Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php b/Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php index f6b5b671..11983194 100644 --- a/Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php +++ b/Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php @@ -114,12 +114,47 @@ public function beginAuthentication_trustedOptionUsed_checkTrustedCookie() $user = $this->getUser(); $context = $this->getAuthenticationContext(); + $context + ->expects($this->once()) + ->method('useTrustedOption') + ->will($this->returnValue(true)); + //Mock the TrustedCookieManager $this->cookieManager ->expects($this->once()) ->method('isTrustedComputer') ->with($request, $user); + $this->authHandler + ->expects($this->once()) + ->method('beginAuthentication'); + + $this->trustedFilter->beginAuthentication($context); + } + + /** + * @test + */ + public function beginAuthentication_trustedOptionUsedOnlyIfContextAllows() + { + $request = $this->getRequest(); + $user = $this->getUser(); + $context = $this->getAuthenticationContext(); + + $context + ->expects($this->once()) + ->method('useTrustedOption') + ->will($this->returnValue(false)); + + $this->cookieManager + ->expects($this->never()) + ->method('isTrustedComputer') + ->with($request, $user); + + $this->authHandler + ->expects($this->once()) + ->method('beginAuthentication'); + $this->trustedFilter->beginAuthentication($context); } @@ -130,6 +165,11 @@ public function beginAuthentication_isTrustedComputer_notCallAuthenticationHandl { $context = $this->getAuthenticationContext(); + $context + ->expects($this->once()) + ->method('useTrustedOption') + ->will($this->returnValue(true)); + //Stub the TrustedCookieManager $this->cookieManager ->expects($this->any())