Skip to content

Commit

Permalink
Merge pull request laravel#42 from eurides-eu/feature/oauth-setup
Browse files Browse the repository at this point in the history
Add console command to create oauth clients for organizations
  • Loading branch information
jaureguivictoria authored May 9, 2018
2 parents faa5633 + c1e2558 commit e576122
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 1 deletion.
48 changes: 48 additions & 0 deletions app/Console/Commands/oAuthClients/CreateOAuthClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands\oAuthClients;

use App\OAuthClients\Commands\CreateOAuthClient as CreateOAuthClientCommand;
use Illuminate\Console\Command;
use Madewithlove\Tactician\Traits\DispatchesJobs;

class CreateOAuthClient extends Command
{
use DispatchesJobs;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'eurides:oauth-client:create {organizationId}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create an oAuth Client for an organization';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$organizationId = $this->argument('organizationId');

$client = $this->dispatch(new CreateOAuthClientCommand($organizationId));

$this->info("OAuth Client created successfully\n ID: $client->id\n Secret: $client->secret");
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\oAuthClients\CreateOAuthClient;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -13,6 +14,7 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
CreateOAuthClient::class,
];

/**
Expand Down
42 changes: 42 additions & 0 deletions app/OAuthClients/CommandHandlers/CreateOAuthClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\OAuthClients\CommandHandlers;

use App\Models\Organization;
use App\OAuthClients\Commands\CreateOAuthClient as Command;
use Laravel\Passport\ClientRepository;

class CreateOAuthClient
{
protected $clientRepository;

/**
* CreateOAuthClient constructor.
*
* @param $clientFactory
*/
public function __construct(ClientRepository $clientFactory)
{
$this->clientRepository = $clientFactory;
}

/**
* @param Command $command
*
* @return \Laravel\Passport\Client
*/
public function handle(Command $command)
{
$organization = Organization::findOrFail($command->getOrganizationId());

$clientName = "$organization->name Client Credentials Grant Client";

$client = $this->clientRepository->create(null, $clientName, url('/'));

$client->organization_id = $command->getOrganizationId();

$client->save();

return $client;
}
}
29 changes: 29 additions & 0 deletions app/OAuthClients/Commands/CreateOAuthClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\OAuthClients\Commands;

class CreateOAuthClient
{
/**
* @var string
*/
protected $organizationId;

/**
* CreateOAuthClient constructor.
*
* @param string $organizationId
*/
public function __construct(string $organizationId)
{
$this->organizationId = $organizationId;
}

/**
* @return string
*/
public function getOrganizationId(): string
{
return $this->organizationId;
}
}
6 changes: 5 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function boot()
Passport::tokensExpireIn(now()->addDays(7));
Passport::refreshTokensExpireIn(now()->addDays(30));

Passport::routes();
Passport::routes(
function ($router) {
$router->forPersonalAccessTokens();
$router->forClients();
}, ['prefix' => 'api/oauth']);
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Madewithlove\Tactician\ServiceProvider::class,
],

/*
Expand Down
41 changes: 41 additions & 0 deletions config/tactician.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Plugins\LockingMiddleware;
use Madewithlove\Tactician\Middlewares\TransactionMiddleware;

return [
/*
|--------------------------------------------------------------------------
| Middlewares
|--------------------------------------------------------------------------
|
| The middlewares to inject into your command bus, you can simply pass the class name
| and the middleware will be resolved from the IoC Container.
|
*/

'middlewares' => [
LockingMiddleware::class,
TransactionMiddleware::class,
CommandHandlerMiddleware::class,
],

/*
|--------------------------------------------------------------------------
| Replacements
|--------------------------------------------------------------------------
|
| The ContainerLocator provided by this package will match your commands
| to your handlers by replacing part of the command namespace.
|
| Use this config to customize what to look in the Command namespace and
| what to replace it with.
|
*/

'replacements' => [
'origin' => 'Commands',
'target' => 'CommandHandlers',
],
];

0 comments on commit e576122

Please sign in to comment.