FriendsOfSymfony / FOSOAuthServerBundle

A server side OAuth2 Bundle for Symfony
1.09k stars 451 forks source link

Cannot override FOSOAuthServerBundle Controller tokenAction method #614

Closed ngoodwin14 closed 5 years ago

ngoodwin14 commented 5 years ago

I am using the following packages:

    "friendsofsymfony/oauth-server-bundle": "^1.6",
    "friendsofsymfony/rest-bundle": "^2.5",
    "friendsofsymfony/user-bundle": "^2.1",
    "symfony/framework-bundle": "4.2.*",
    "symfony/http-foundation": "4.2.*",
    "symfony/http-kernel": "4.2.*"

I am attempting to override the tokenAction method of the FOSOAuthServerBundle package, but I am getting stuck on the error:

"Cannot autowire service App\Controller\TokenController argument $server of method FOS\OAuthServerBundle\Controller\TokenController::__construct() references class OAuth2\OAuth2; but no such service exists. You should maybe alias this class to the existing fos_oauth_server.server service"

I have tried several different approaches (autowire, auto-injection), but I keep coming full circle back to the error described above. It seems that the "use OAuth2\OAuth2;" reference is properly namespaced in the bundle's TokenController, but when I try to override it cannot parse the OAuth2 class locations properly, and I am not sure what pattern to use either in the class or in the services.yaml to point it to the right location.

Here is my services.yaml

services:
    App\Controller\TokenController\:
        resource: '../src/Controller/TokenController.php'
        arguments: ['@fos_oauth_server.server']

And my custom TokenController class

?php

namespace App\Controller;

use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TokenController extends BaseController
{

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function tokenAction(Request $request)
    {
        $token = parent::tokenAction($request);

        // my custom code here 

        return $token;
    }

}

And if I try to do the obvious and add the line

use OAuth2\OAuth2;

to my custom TokenController I get the same error.

AntoniusGolly commented 4 years ago

As stated elsewhere, the solution is to decorate the the original controller. In service.yaml do

App\Infrastructure\Controller\Account\TokenController:
        decorates: FOS\OAuthServerBundle\Controller\TokenController
        arguments: ['@fos_oauth_server.server']
        tags: ['controller.service_arguments']

(assuming your custom controller is in App\Infrastructure\Controller\Account\TokenController)

trangvt commented 4 years ago

@AntoniusGolly I did it, but got this error on v3.4

Type error: Too few arguments to function MccpAuthBundle\\Controller\\OauthController::__construct(), 0 passed in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php on line 200 and exactly 1 expected

er1z commented 4 years ago

Could you show a repo with app?