mercredi 28 septembre 2016

Overriding class in a composer package

Currently I have a Laravel installation using Laravel Passport (which uses league/oauth2-server for the server implementation). I would like to return the user id when a oauth2 token is granted, so I can use it to identify the authenticated user in my EmberJS app.

The suggested method to do this is:

Create my own class:

use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;

class UserIdBearerTokenResponse extends BearerTokenResponse
{
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
    {
        return [
            'user_id' => $this->accessToken->getUserIdentifier()
        ];
    }
}

Modifying AuthorizationServer.getResponseType() in vendor/league/oauth2-server/src

    protected function getResponseType()
    {
        if ($this->responseType instanceof ResponseTypeInterface === false) {
            // Return my own class instead of provided one
            $this->responseType = new UserIdBearerTokenResponse();
        }

        $this->responseType->setPrivateKey($this->privateKey);

        return $this->responseType;
    }

But this approach requires me to add the vendor/league/oauth2-server/src/AuthorizationServer.php file to my git repo.

This seems very messy and unreliable to me. Is there a better/cleaner way to achieve this?




Aucun commentaire:

Enregistrer un commentaire