Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Additional check if the context allows trusted computers
Browse files Browse the repository at this point in the history
It is also required to check if the context allows to bypass the 2FA check if the device is trusted.
It is to be used in scenarios when the context resolves the ability to "trust" the device in runtime (say - depending on the client's network). In those cases even if the user has the trusted device - it may be not trusted under this very circumstances.
  • Loading branch information
zerkms committed Aug 14, 2016
1 parent 17fffbd commit baaf0ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Security/TwoFactor/Trusted/TrustedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
40 changes: 40 additions & 0 deletions Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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())
Expand Down

0 comments on commit baaf0ba

Please sign in to comment.