Search code examples
phpsymfonyfosoauthserverbundle

FOSAuthBundle Register Event onPreAuthorizationProcess never called


I would like to set up event listener on the OAuth in order to log connection failures, and connections.

I registered an event listener in services.yml, defined the EventListener/OAuthEventListener class with a method to trigger when the event is called.

In my API, I just call /oauth/v2/token to login/refresh_token. These calls are handled by fosrestbundle.

My methodes registered are never called. Does this url never trigger fos_oauth_server.pre_authorization_process event ?


Solution

  • The route configuration:

    <route id="fos_oauth_server_token" path="/oauth/v2/token" methods="GET POST">
        <default key="_controller">fos_oauth_server.controller.token:tokenAction</default>
    </route>
    

    The controller service:

        <service id="fos_oauth_server.controller.token" class="FOS\OAuthServerBundle\Controller\TokenController">
            <argument type="service" id="fos_oauth_server.server" />
        </service>
    

    The server service:

    <parameters>
        <parameter key="fos_oauth_server.server.class">OAuth2\OAuth2</parameter>
    </parameters>
    <service id="fos_oauth_server.server" class="%fos_oauth_server.server.class%">
        <argument type="service" id="fos_oauth_server.storage" />
        <argument>%fos_oauth_server.server.options%</argument>
    </service>
    

    The controller method:

    public function tokenAction(Request $request)
    {
        try {
            return $this->server->grantAccessToken($request);
        } catch (OAuth2ServerException $e) {
            return $e->getHttpResponse();
        }
    }
    

    Assuming you are using the defaults, the controller service for that route is just delegating to the server library. The pre_authorization event is called during authorization handled by /oauth/v2/auth.