c.brahim
c.brahim

Reputation: 190

listen to request before json authentication symfony 4

I have tow way to login the users to my application: from a backoffice or from an api. for some reason i want to process a ldap check before the authentification proccess start.

listen to request before authentication or rewrite /login_check in symfony3

but i had this error when i try to login to the api:

Argument 3 passed to Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener::__construct() must be an instance of Symfony\Component\Security\Http\HttpUtils, instance of Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy given

this is my code

services.yaml

    security.authentication.listener.json:
    class: App\EventListener\UsernamePasswordJsonAuthenticationListener
    parent: security.authentication.listener.abstract
    abstract: true
    autowire: true
    autoconfigure: false
    calls: [ [initialize, ["@doctrine.orm.entity_manager"]] ]

//UsernamePasswordJsonAuthenticationListener.php

<?php


namespace App\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Service\LdapTools;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener as baseJsonAuthenticationListener;

class UsernamePasswordJsonAuthenticationListener extends baseJsonAuthenticationListener
{

/** @var EntityManagerInterface */
protected $entityManager;

/** @var LdapTools */
protected $ldapTools;

/** @var UserManagerInterface */
protected $userManager;

/**
 * setter is called through DI container
 *
 * @param EntityManagerInterface $entityManager
 */
public function initialize(EntityManagerInterface $entityManager,LdapTools $ldapTools,UserManagerInterface $userManager)
{

    $this->entityManager = $entityManager;
    $this->ldapTools = $ldapTools;
    $this->userManager = $userManager;
}

}

?>

thanx before .

Upvotes: 0

Views: 380

Answers (1)

c.brahim
c.brahim

Reputation: 190

I fixed that by change my declaration in services.yml

security.authentication.listener.json:
    class: App\EventListener\UsernamePasswordJsonAuthenticationListener
    abstract: true
    autowire: true
    autoconfigure: false
    arguments:
            - "@security.token_storage"
            - "@security.authentication.manager"
            - "@security.http_utils"
            - ~
            - Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
            - Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface
            - ~

    calls: [ [initialize, ["@doctrine.orm.entity_manager"]] ]

Upvotes: 1

Related Questions