Skip to content

Commit

Permalink
Pass complete auth context to TwoFactorProviderDeciderInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
danielburger1337 committed Jan 15, 2024
1 parent 45a43fb commit c858536
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

use Scheb\TwoFactorBundle\Model\PreferredProviderInterface;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;

class TwoFactorProviderDecider implements TwoFactorProviderDeciderInterface
{
/**
* @param string[] $activeProviders
*/
public function getPreferredTwoFactorProvider(array $activeProviders, TwoFactorTokenInterface $token, object $user): string|null
public function getPreferredTwoFactorProvider(array $activeProviders, TwoFactorTokenInterface $token, AuthenticationContextInterface $context): string|null
{
$user = $context->getUser();

if ($user instanceof PreferredProviderInterface) {
return $user->getPreferredTwoFactorProvider();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider;

use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;

interface TwoFactorProviderDeciderInterface
{
Expand All @@ -13,5 +14,5 @@ interface TwoFactorProviderDeciderInterface
*
* @param string[] $activeProviders
*/
public function getPreferredTwoFactorProvider(array $activeProviders, TwoFactorTokenInterface $token, object $user): string|null;
public function getPreferredTwoFactorProvider(array $activeProviders, TwoFactorTokenInterface $token, AuthenticationContextInterface $context): string|null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function beginTwoFactorAuthentication(AuthenticationContextInterface $con
if ($activeTwoFactorProviders) {
$twoFactorToken = $this->twoFactorTokenFactory->create($authenticatedToken, $context->getFirewallName(), $activeTwoFactorProviders);

$preferredProvider = $this->twoFactorProviderDecider->getPreferredTwoFactorProvider($activeTwoFactorProviders, $twoFactorToken, $context->getUser());
$preferredProvider = $this->twoFactorProviderDecider->getPreferredTwoFactorProvider($activeTwoFactorProviders, $twoFactorToken, $context);

if (null !== $preferredProvider) {
try {
Expand Down
22 changes: 18 additions & 4 deletions tests/Security/TwoFactor/Provider/TwoFactorProviderDeciderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use PHPUnit\Framework\MockObject\MockObject;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderDecider;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderDeciderInterface;
use Scheb\TwoFactorBundle\Tests\Security\TwoFactor\Condition\AbstractAuthenticationContextTestCase;
use stdClass;
use Symfony\Component\Security\Core\User\UserInterface;

class TwoFactorProviderDeciderTest extends AbstractAuthenticationContextTestCase
{
Expand All @@ -31,7 +32,7 @@ public function getPreferredTwoFactorProvider_implementsPreferredProvider_return

$this->assertEquals(
'preferredProvider',
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $user),
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)),
);
}

Expand All @@ -43,7 +44,7 @@ public function getPreferredTwoFactorProvider_implementsPreferredProvider_return
$user = $this->createUserWithPreferredProvider(null);

$this->assertNull(
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $user),
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)),
);
}

Expand All @@ -52,8 +53,10 @@ public function getPreferredTwoFactorProvider_implementsPreferredProvider_return
*/
public function getPreferredTwoFactorProvider_unexpectedUserObject_returnsNull(): void
{
$user = $this->createMock(UserInterface::class);

$this->assertNull(
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, new stdClass()),
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)),
);
}

Expand All @@ -67,4 +70,15 @@ private function createUserWithPreferredProvider(string|null $preferredProvider)

return $user;
}

private function createAuthContext(object $user): MockObject|AuthenticationContextInterface
{
$authContext = $this->createMock(AuthenticationContextInterface::class);
$authContext
->expects($this->any())
->method('getUser')
->willReturn($user);

return $authContext;
}
}

0 comments on commit c858536

Please sign in to comment.