Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to modify AuthCodeGrant? #1030

Open
gssj85 opened this issue Oct 1, 2024 · 1 comment
Open

How to modify AuthCodeGrant? #1030

gssj85 opened this issue Oct 1, 2024 · 1 comment

Comments

@gssj85
Copy link

gssj85 commented Oct 1, 2024

I need to override functionality of: vendor/league/oauth2-server/src/Grant/AuthCodeGrant.php to add another information on the authorization code (the tenant id), will also have to decrypt it later but for now I would be very happy to know how to override this one first.

So far I can say I have no idea how to do this, used some GPT help on a service provider but no success:

public function register(): void
    {
        Passport::ignoreRoutes();

        // Bind the AuthCodeRepositoryInterface to an implementation
        $this->app->bind(AuthCodeRepositoryInterface::class, AuthCodeRepository::class);

        // Optionally bind the RefreshTokenRepositoryInterface if you haven't done so
        $this->app->bind(RefreshTokenRepositoryInterface::class, RefreshTokenRepositoryInterface::class);

        parent::register();

        $this->app->extend(AuthorizationServer::class, function (AuthorizationServer $server) {
            $grant = new MyCustomAuthCodeGrant(
                $this->app->make(AuthCodeRepositoryInterface::class),
                $this->app->make(RefreshTokenRepositoryInterface::class),
                new \DateInterval('PT10M')
            );

            $server->enableGrantType($grant, new \DateInterval('PT1H'));

            return $server;
        });
    }

Those bind didnt help at all, got this error message:

Target [League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface] is not instantiable while building [Laravel\Passport\Http\Controllers\AuthorizationController].

@gssj85
Copy link
Author

gssj85 commented Oct 2, 2024

To anyone with the same question, this solved:

App::extend(AuthorizationServer::class, function (AuthorizationServer $server) {
    App::singleton(AuthCodeRepositoryInterface::class, AuthCodeRepository::class);
    App::singleton(RefreshTokenRepositoryInterface::class, RefreshTokenRepository::class);
    
    $grant = new AuthCodeGrant( // this is my custom AuthCodeGrant, I used the same name
        resolve(AuthCodeRepositoryInterface::class),
        resolve(RefreshTokenRepositoryInterface::class),
        new \DateInterval('PT10M')
    );
    
    $server->enableGrantType($grant, new \DateInterval('PT1H'));
    
    return $server;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant