-
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:
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]. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To override the Step 1: Create Your Custom Grant ClassFirst, create a new class that extends namespace App\OAuth;
use League\OAuth2\Server\Grant\AuthCodeGrant as BaseAuthCodeGrant;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
class MyCustomAuthCodeGrant extends BaseAuthCodeGrant
{
public function __construct(AuthCodeRepositoryInterface $authCodeRepository, RefreshTokenRepositoryInterface $refreshTokenRepository, \DateInterval $refreshTokenTTL)
{
parent::__construct($authCodeRepository, $refreshTokenRepository, $refreshTokenTTL);
}
protected function respondToAuthorizationRequest($request, $response, $grantType, array $options = [])
{
// Your custom logic here to handle tenant ID
// You can access request data and add tenant ID to the authorization code
// Example:
$tenantId = $request->get('tenant_id'); // assuming tenant_id is passed in request
// Process the request as usual
return parent::respondToAuthorizationRequest($request, $response, $grantType, $options);
}
// Add any additional methods as needed
} Step 2: Register Your Custom Grant in a Service ProviderNext, you need to register your custom grant in a service provider, similar to what you've done already. Make sure to remove any bindings that cause issues. namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
use League\OAuth2\Server\AuthorizationServer;
use App\OAuth\MyCustomAuthCodeGrant; // Import your custom grant class
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
class AuthServiceProvider extends ServiceProvider
{
public function register(): void
{
Passport::ignoreRoutes();
// Bind your repositories
$this->app->bind(AuthCodeRepositoryInterface::class, YourAuthCodeRepositoryImplementation::class);
$this->app->bind(RefreshTokenRepositoryInterface::class, YourRefreshTokenRepositoryImplementation::class);
parent::register();
// Extend the Authorization Server with your custom grant
$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;
});
}
} Step 3: Handle Tenant ID in Your ImplementationMake sure your Step 4: TestingAfter implementing the above steps, test your changes thoroughly to ensure that the tenant ID is correctly included in the authorization code and that you can later decrypt it as needed. TroubleshootingIf you encounter the "not instantiable" error again, check the following:
This should give you a good starting point to override the functionality you need. Let me know if you have further questions! |
Beta Was this translation helpful? Give feedback.
To override the
AuthCodeGrant
functionality in Laravel Passport and add custom information like a tenant ID, you can create your own custom grant class that extends the existingAuthCodeGrant
. Here’s a step-by-step guide to help you do this.Step 1: Create Your Custom Grant Class
First, create a new class that extends
AuthCodeGrant
. You’ll want to customize therespondToAuthorizationRequest
method (or any other relevant method) to include your tenant ID.