|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Mvdstam\Oauth2ServerLaravel\Commands; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
use Mvdstam\Oauth2ServerLaravel\Repositories\ClientRepository; |
|
10
|
|
|
use Mvdstam\Oauth2ServerLaravel\Repositories\ScopeRepository; |
|
11
|
|
|
use Ramsey\Uuid\Uuid; |
|
12
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
13
|
|
|
|
|
14
|
|
|
class CreateClientCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $signature = 'oauth2-server:create-client {--id=} {--secret=} {--name=} {--redirect_uri=} {--scope=*}'; |
|
18
|
|
|
|
|
19
|
|
|
protected $description = 'Create new clients for your OAuth2 server'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ClientRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $clients; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ScopeRepository |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $scopes; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* CreateClientCommand constructor. |
|
33
|
|
|
* @param ClientRepository $clients |
|
34
|
|
|
* @param ScopeRepository $scopes |
|
35
|
|
|
*/ |
|
36
|
16 |
|
public function __construct(ClientRepository $clients, ScopeRepository $scopes) |
|
37
|
|
|
{ |
|
38
|
16 |
|
parent::__construct(); |
|
39
|
|
|
|
|
40
|
16 |
|
$this->clients = $clients; |
|
41
|
16 |
|
$this->scopes = $scopes; |
|
42
|
16 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function handle() |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
$this->clients->forceCreate([ |
|
48
|
|
|
'id' => (string) $this->getClientId(), |
|
49
|
|
|
'secret' => (string) $this->getClientSecret(), |
|
50
|
|
|
'name' => $this->option('name') ?: $this->ask('Please enter a name'), |
|
51
|
|
|
'redirect_uri' => $this->option('redirect_uri') |
|
52
|
|
|
]); |
|
53
|
|
|
} catch (Exception $e) { |
|
54
|
|
|
$this->error("An error occurred: {$e->getMessage()}"); |
|
55
|
|
|
return 1; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->info('Client created succesfully'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return UuidInterface |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
protected function getClientId() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
if (!($clientId = $this->option('id'))) { |
|
67
|
|
|
$this->info('Generating a random UUID for client id...'); |
|
68
|
|
|
$clientId = Uuid::uuid1(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$clientId = Uuid::fromString($clientId); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $clientId; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return UuidInterface |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
protected function getClientSecret() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
if (!($clientSecret = $this->option('secret'))) { |
|
82
|
|
|
$this->info('Generating a random UUID for client secret...'); |
|
83
|
|
|
$clientSecret = Uuid::uuid1(); |
|
84
|
|
|
} else { |
|
85
|
|
|
$clientSecret = Uuid::fromString($clientSecret); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $clientSecret; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.