Skip to content

[Security] Cannot autowire custom LoginFormAuthenticator in a controller action #59091

Closed
@ebitkov

Description

@ebitkov

Symfony version(s) affected

7.2

Description

This may be related to #59071.

I have a custom authenticator extending the builtin AbstractLoginFormAuthenticator. I autowire it into the register action of my SecurityController, so I can authenticate newly registered users, so they can continue the process they are currently in without the need to verify their data at this point:

class SecurityController extends AbstractController {
    # ...
  
    public function register(
        App\Security\LoginFormAuthenticator $customAuthenticator,
        # ...
    ) {
        # ...
    }

After upgrading to 7.2, this throws an exception:

App\Controller\SecurityController::register(): Argument #3 ($customAuthenticator) must be of type App\Security\LoginFormAuthenticator, Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticator given

A quick debug of the service shows, that the dev environment seems to wrap it automatically into the TracableAuthenticator class:

> symfony console debug:container App\Security\LoginFormAuthenticator


// This service is a private alias for the service debug.App\Security\LoginFormAuthenticator                           

Information for Service "debug.App\Security\LoginFormAuthenticator"
===================================================================

 Collects info about an authenticator for debugging purposes.

 ---------------- ----------------------------------------------------------------------------------------------------------------------- 
  Option           Value                                                                                                                  
 ---------------- ----------------------------------------------------------------------------------------------------------------------- 
  Service ID       debug.App\Security\LoginFormAuthenticator                                                                              
  Class            Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticator                                             
  Tags             container.decorator (id: App\Security\LoginFormAuthenticator, inner: debug.App\Security\LoginFormAuthenticator.inner)  
  Public           no                                                                                                                     
  Synthetic        no                                                                                                                     
  Lazy             no                                                                                                                     
  Shared           yes                                                                                                                    
  Abstract         no                                                                                                                     
  Autowired        no                                                                                                                     
  Autoconfigured   no                                                                                                                     
  Usages           App\Security\LoginFormAuthenticator                                                                                    
                   security.command.debug_firewall                                                                                        
                   security.authenticator.manager.main                                                                                    
                   security.exception_listener.main                                                                                       
                   .service_locator.vgziCoP                                                                                               
                   .service_locator.j9y68NC                                                                                               
 ---------------- ----------------------------------------------------------------------------------------------------------------------- 

 ! [NOTE] The "App\Security\LoginFormAuthenticator" service or alias has been removed or inlined when the container was 
 !        compiled.          

Switching to the prod environment seems to "fix" the problem.

How to reproduce

Create a new class App\Security\LoginFormAuthenticator extending Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator. Define the abstract methods and configure it in your configurations:

# config/packages/security.yaml

security:
    firewalls:
        main:
            custom_authenticators:
                - App\Security\LoginFormAuthenticator

Now, create a new class App\Controller\SecurityController with an method register and try to autowire the newly created authenticator via a method parameter:

class SecurityController extends AbstractController
{
    #[Route(path: '/register')]
    public function register(
        App\Security\LoginFormAuthenticator $customAuthenticator,
    ): Response {
        # ...
    }
}

Accessing the route (https://127.0.0.1:8000/register) throws the described exception.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions