-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable custom conditions for when to perform two-factor authent…
…ication #49
- Loading branch information
Showing
13 changed files
with
148 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Custom Conditions for Two-Factor Authentication | ||
=============================================== | ||
|
||
In your application, you may have extra requirements when to perform two-factor authentication, which goes beyond what | ||
the bundle is doing automatically. In such a case you need to implement | ||
`\Scheb\TwoFactorBundle\Security\TwoFactor\Condition\TwoFactorConditionInterface`: | ||
|
||
```php | ||
<?php | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\Condition\TwoFactorConditionInterface; | ||
|
||
class MyTwoFactorCondition implements TwoFactorConditionInterface | ||
{ | ||
public function shouldPerformTwoFactorAuthentication(AuthenticationContextInterface $context): bool | ||
{ | ||
// Your conditions here | ||
} | ||
} | ||
``` | ||
|
||
Register it as a service and configure the service name: | ||
|
||
```yaml | ||
# config/packages/scheb_two_factor.yaml | ||
scheb_two_factor: | ||
two_factor_condition: acme.custom_two_factor_condition | ||
``` |
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
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
15 changes: 15 additions & 0 deletions
15
src/bundle/Security/TwoFactor/Condition/DefaultTwoFactorCondition.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Condition; | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; | ||
|
||
class DefaultTwoFactorCondition implements TwoFactorConditionInterface | ||
{ | ||
public function shouldPerformTwoFactorAuthentication(AuthenticationContextInterface $context): bool | ||
{ | ||
return true; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/bundle/Security/TwoFactor/Condition/TwoFactorConditionInterface.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Condition; | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; | ||
|
||
interface TwoFactorConditionInterface | ||
{ | ||
/** | ||
* Check if two-factor authentication should be performed for the user under current conditions. | ||
*/ | ||
public function shouldPerformTwoFactorAuthentication(AuthenticationContextInterface $context): bool; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/bundle/Security/TwoFactor/Handler/ConditionAuthenticationHandler.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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Handler; | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\Condition\TwoFactorConditionInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
class ConditionAuthenticationHandler implements AuthenticationHandlerInterface | ||
{ | ||
/** | ||
* @var AuthenticationHandlerInterface | ||
*/ | ||
private $authenticationHandler; | ||
|
||
/** | ||
* @var TwoFactorConditionInterface | ||
*/ | ||
private $condition; | ||
|
||
public function __construct(AuthenticationHandlerInterface $authenticationHandler, TwoFactorConditionInterface $condition) | ||
{ | ||
$this->authenticationHandler = $authenticationHandler; | ||
$this->condition = $condition; | ||
} | ||
|
||
public function beginTwoFactorAuthentication(AuthenticationContextInterface $context): TokenInterface | ||
{ | ||
if (!$this->condition->shouldPerformTwoFactorAuthentication($context)) { | ||
return $context->getToken(); | ||
} | ||
|
||
return $this->authenticationHandler->beginTwoFactorAuthentication($context); | ||
} | ||
} |
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