Skip to content

Commit

Permalink
Adding Laravel AccessTokenCommand
Browse files Browse the repository at this point in the history
Signed-off-by: RJ Garcia <[email protected]>
  • Loading branch information
ragboyjr committed Mar 17, 2018
1 parent df338db commit 0722d5b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@
},
"require": {
"league/oauth2-server": "^7.0"
},
"extra": {
"laravel": {
"providers": [
"Krak\\DoctrineOAuth2\\Provider\\Laravel\\OAuth2ServiceProvider"
]
}
}
}
1 change: 0 additions & 1 deletion src/Console/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SeedCommand extends Command
{
private $container;


public function __construct(ContainerInterface $container) {
parent::__construct();
$this->container = $container;
Expand Down
52 changes: 52 additions & 0 deletions src/Provider/Laravel/AccessTokenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Krak\DoctrineOAuth2\Provider\Laravel;

use Illuminate\Console\Command;
use Symfony\Component\Console\Logger\ConsoleLogger;
use ModernProducer\Api\Doctrine\LoadFixtures;

use Illuminate\Contracts\Http;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

final class AccessTokenCommand extends Command
{
protected $signature = 'oauth2:access-token {client-id} {client-secret} {--s|scope=*} {--g|grant-type=client_credentials} {json?}';
protected $description = 'Create an oauth2 access token';

public function handle() {
$app = $this->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));
}
}
3 changes: 2 additions & 1 deletion src/Provider/Laravel/OAuth2ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 0722d5b

Please sign in to comment.