Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - How to get username and IP address in authentication failure listener?

Tags:

php

symfony

I need to get:

  • username
  • ip address

for each users that tries to log in to my website. There is no problem to get that information if user logged successfully but I don't know how to get IP address for failure attempts.

Her's my code:

app.listener.interactive_login_listener:
    class: AppBundle\EventListener\LogonListener
    arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
    tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onAuthenticationSuccess }

# authentication failure event listener
app.listener.authentication_failure_event_listener:
    class: AppBundle\EventListener\LogonListener
    arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
    tags:
        - { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }

and the listener:

class LogonListener implements EventSubscriberInterface
{

    private $_tokenStorage;
    private $_em;
    private $_authorizationChecker;
    private $_router;
    private $_settings;

    public function __construct(
        TokenStorage $tokenStorage,
        EntityManager $em,
        AuthorizationCheckerInterface $authorizationChecker,
        Router $router,
        $settings)
    {
        $this->_tokenStorage = $tokenStorage;
        $this->_em = $em;
        $this->_authorizationChecker = $authorizationChecker;
        $this->_router = $router;
        $this->_settings = $settings;
    }

    /**
     * getSubscribedEvents
     *
     * @return    array
     */
    public static function getSubscribedEvents()
    {
        return array(
            AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
        );
    }

    /**
     * onAuthenticationSuccess
     *
     * @param    InteractiveLoginEvent $event
     */
    public function onAuthenticationSuccess(InteractiveLoginEvent $event)
    {
        var_dump( $event->getAuthenticationToken()->getUser() );
        var_dump( $event->getRequest()->getClientIp() );
        die();
    }

    public function onAuthenticationFailure(AuthenticationFailureEvent $event)
    {
        $userName = $event->getAuthenticationToken()->getUsername();
        $user = $this->_em->getRepository('AppBundle:User')->findOneByUsername($userName);

        var_dump( $user );
        // how to get IP?
        die();
    }
like image 575
breq Avatar asked Aug 01 '16 07:08

breq


2 Answers

Inject RequestStack into listener and get Request from it. You can get IP from getClientIp()

/**
 * @var RequestStack
 */
protected $requestStack;

/**
 * @return string
 */
protected function resolveClientIp()
{
    return $this->requestStack->getMasterRequest()->getClientIp();
}
like image 74
Konrad Podgórski Avatar answered Sep 18 '22 08:09

Konrad Podgórski


Have you tried to inject RequestStack to your listener ?

//in your service

# authentication failure event listener
app.listener.authentication_failure_event_listener:
    class: AppBundle\EventListener\LogonListener
    arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings', '@request_stack']
    tags:
        - { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }


//in your listener

class LogonListener implements EventSubscriberInterface
{

    private $_tokenStorage;
    private $_em;
    private $_authorizationChecker;
    private $_router;
    private $_settings;
    private $_request

    public function __construct(TokenStorage $tokenStorage, EntityManager $em, AuthorizationCheckerInterface $authorizationChecker, Router $router, $settings, $request)
    {
        $this->_tokenStorage = $tokenStorage;
        $this->_em = $em;
        $this->_authorizationChecker = $authorizationChecker;
        $this->_router = $router;
        $this->_settings = $settings;
        $this->_request = $request
    }

    public function onAuthenticationFailure(AuthenticationFailureEvent $event)
    {
        $userName = $event->getAuthenticationToken()->getUsername();
        $user = $this->_em->getRepository('AppBundle:User')->findOneByUsername($userName);

        var_dump( $this->request->getCurrentRequest()->getClientIp() );
        die();
    }
}
like image 32
PASTAGA Avatar answered Sep 17 '22 08:09

PASTAGA