diff --git a/composer.json b/composer.json index 5cef95d..b1f2e54 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,12 @@ }, "require": { "league/oauth2-server": "^7.0" + }, + "extra": { + "laravel": { + "providers": [ + "Krak\\DoctrineOAuth2\\Provider\\Laravel\\OAuth2ServiceProvider" + ] + } } } diff --git a/src/Console/SeedCommand.php b/src/Console/SeedCommand.php index 7bc65e5..fdc3dfd 100644 --- a/src/Console/SeedCommand.php +++ b/src/Console/SeedCommand.php @@ -16,7 +16,6 @@ class SeedCommand extends Command { private $container; - public function __construct(ContainerInterface $container) { parent::__construct(); $this->container = $container; diff --git a/src/Provider/Laravel/AccessTokenCommand.php b/src/Provider/Laravel/AccessTokenCommand.php new file mode 100644 index 0000000..27d785c --- /dev/null +++ b/src/Provider/Laravel/AccessTokenCommand.php @@ -0,0 +1,52 @@ +getLaravel(); + + $grantType = $this->option('grant-type'); + $clientId = $this->argument('client-id'); + $clientSecret = $this->argument('client-secret'); + $scopes = $this->option('scope'); + $json = json_decode($this->argument('json') ?? '{}', true) ?? []; + + $app = $this->getLaravel(); + $uri = $app->make('config')->get('app.url') . '/oauth2/access-token'; + $kernel = $app->make(Http\Kernel::class); + + $body = array_merge([ + 'grant_type' => $grantType, + 'client_id' => $clientId, + 'client_secret' => $clientSecret, + ], array_filter([ + 'scope' => implode(' ', $scopes) + ]), $json); + + $symfonyRequest = SymfonyRequest::create($uri, 'POST', $body, [], [], [], ''); + + $response = $kernel->handle($request = Request::createFromBase($symfonyRequest)); + + $respContent = $response->getContent(); + $res = json_decode($respContent); + if (!$res) { + $this->error($respContent); + return; + } + + $this->info(json_encode($res, JSON_PRETTY_PRINT)); + } +} diff --git a/src/Provider/Laravel/OAuth2ServiceProvider.php b/src/Provider/Laravel/OAuth2ServiceProvider.php index 56581b2..29ae0c6 100644 --- a/src/Provider/Laravel/OAuth2ServiceProvider.php +++ b/src/Provider/Laravel/OAuth2ServiceProvider.php @@ -32,7 +32,8 @@ final class OAuth2ServiceProvider extends ServiceProvider public function register() { $this->commands([ DoctrineOAuth2\Console\GenerateKeysCommand::class, - DoctrineOAuth2\Console\SeedCommand::class + DoctrineOAuth2\Console\SeedCommand::class, + AccessTokenCommand::class, ]); $this->publishes([