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

Commit

Permalink
Implemented ip whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Feb 14, 2016
1 parent 23d7ba4 commit 2640fb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions Resources/config/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" />
<argument type="service" id="scheb_two_factor.trusted_filter" />
<argument>%scheb_two_factor.security_tokens%</argument>
<argument>%scheb_two_factor.ip_whitelist%</argument>
</service>
<service id="scheb_two_factor.security.request_listener" class="%scheb_two_factor.security.request_listener.class%">
<tag name="kernel.event_listener" event="kernel.request" method="onCoreRequest" priority="-1" />
Expand Down
14 changes: 13 additions & 1 deletion Security/TwoFactor/EventListener/InteractiveLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ class InteractiveLoginListener
*/
private $supportedTokens;

/**
* @var array
*/
private $ipWhitelist;

/**
* Construct a listener for login events.
*
* @param AuthenticationHandlerInterface $authHandler
* @param array $supportedTokens
* @param array $ipWhitelist
*/
public function __construct(AuthenticationHandlerInterface $authHandler, array $supportedTokens)
public function __construct(AuthenticationHandlerInterface $authHandler, array $supportedTokens, array $ipWhitelist)
{
$this->authHandler = $authHandler;
$this->supportedTokens = $supportedTokens;
$this->ipWhitelist = $ipWhitelist;
}

/**
Expand All @@ -39,6 +46,11 @@ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$request = $event->getRequest();

// Skip two-factor authentication for whitelisted IPs
if (in_array($request->getClientIp(), $this->ipWhitelist)) {
return;
}

// Check if security token is supported
$token = $event->getAuthenticationToken();
if (!$this->isTokenSupported($token)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class InteractiveLoginListenerTest extends \PHPUnit_Framework_TestCase
{
const WHITELISTED_IP = '1.2.3.4';
const NON_WHITELISTED_IP = '1.1.1.1';

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -28,15 +31,20 @@ public function setUp()
$this->authHandler = $this->getMock("Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationHandlerInterface");

$supportedTokens = array("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken");
$this->listener = new InteractiveLoginListener($this->authHandler, $supportedTokens);
$this->listener = new InteractiveLoginListener($this->authHandler, $supportedTokens, array(self::WHITELISTED_IP));
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function createEvent($token)
private function createEvent($token, $clientIp)
{
$this->request = $this->getMock("Symfony\Component\HttpFoundation\Request");
$this->request
->expects($this->any())
->method('getClientIp')
->will($this->returnValue($clientIp));

$event = $this->getMockBuilder("Symfony\Component\Security\Http\Event\InteractiveLoginEvent")
->disableOriginalConstructor()
->getMock();
Expand All @@ -58,7 +66,7 @@ private function createEvent($token)
public function onSecurityInteractiveLogin_tokenClassSupported_beginAuthentication()
{
$token = new UsernamePasswordToken('user', array(), 'key');
$event = $this->createEvent($token);
$event = $this->createEvent($token, self::NON_WHITELISTED_IP);

//Expect TwoFactorProvider to be called
$expectedContext = new AuthenticationContext($this->request, $token);
Expand All @@ -76,7 +84,23 @@ public function onSecurityInteractiveLogin_tokenClassSupported_beginAuthenticati
public function onSecurityInteractiveLogin_tokenClassNotSupported_doNothing()
{
$token = $this->getMock("Symfony\Component\Security\Core\Authentication\Token\TokenInterface");
$event = $this->createEvent($token);
$event = $this->createEvent($token, self::NON_WHITELISTED_IP);

//Expect TwoFactorProvider not to be called
$this->authHandler
->expects($this->never())
->method('beginAuthentication');

$this->listener->onSecurityInteractiveLogin($event);
}

/**
* @test
*/
public function onSecurityInteractiveLogin_ipWhitelisted_doNothing()
{
$token = $this->getMock("Symfony\Component\Security\Core\Authentication\Token\TokenInterface");
$event = $this->createEvent($token, self::WHITELISTED_IP);

//Expect TwoFactorProvider not to be called
$this->authHandler
Expand Down

0 comments on commit 2640fb7

Please sign in to comment.