-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7990826
commit 1b74ad2
Showing
10 changed files
with
183 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/bundle/Security/TwoFactor/Provider/TwoFactorProviderDecider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider; | ||
|
||
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, AuthenticationContextInterface $context): string|null | ||
{ | ||
$user = $context->getUser(); | ||
|
||
if ($user instanceof PreferredProviderInterface) { | ||
return $user->getPreferredTwoFactorProvider(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/bundle/Security/TwoFactor/Provider/TwoFactorProviderDeciderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider; | ||
|
||
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; | ||
|
||
interface TwoFactorProviderDeciderInterface | ||
{ | ||
/** | ||
* Return the alias of the preferred two-factor provider. | ||
* | ||
* @param string[] $activeProviders | ||
*/ | ||
public function getPreferredTwoFactorProvider(array $activeProviders, TwoFactorTokenInterface $token, AuthenticationContextInterface $context): string|null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/Security/TwoFactor/Provider/TwoFactorProviderDeciderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Tests\Security\TwoFactor\Provider; | ||
|
||
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 Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class TwoFactorProviderDeciderTest extends AbstractAuthenticationContextTestCase | ||
{ | ||
private MockObject|TwoFactorProviderDeciderInterface $twoFactorProviderDecider; | ||
private MockObject|TwoFactorTokenInterface $twoFactorToken; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->twoFactorToken = $this->createMock(TwoFactorTokenInterface::class); | ||
$this->twoFactorProviderDecider = new TwoFactorProviderDecider(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getPreferredTwoFactorProvider_implementsPreferredProvider_returnsPreferredProvider(): void | ||
{ | ||
$user = $this->createUserWithPreferredProvider('preferredProvider'); | ||
|
||
$this->assertEquals( | ||
'preferredProvider', | ||
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getPreferredTwoFactorProvider_implementsPreferredProvider_returnsNullPreferredProvider(): void | ||
{ | ||
$user = $this->createUserWithPreferredProvider(null); | ||
|
||
$this->assertNull( | ||
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getPreferredTwoFactorProvider_unexpectedUserObject_returnsNull(): void | ||
{ | ||
$user = $this->createMock(UserInterface::class); | ||
|
||
$this->assertNull( | ||
$this->twoFactorProviderDecider->getPreferredTwoFactorProvider([], $this->twoFactorToken, $this->createAuthContext($user)), | ||
); | ||
} | ||
|
||
private function createUserWithPreferredProvider(string|null $preferredProvider): MockObject|UserWithPreferredProviderInterface | ||
{ | ||
$user = $this->createMock(UserWithPreferredProviderInterface::class); | ||
$user | ||
->expects($this->any()) | ||
->method('getPreferredTwoFactorProvider') | ||
->willReturn($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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters