1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\console\PersonalAccessToken; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface; |
6
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2DebugController; |
7
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2PersonalAccessTokenController; |
8
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\DiHelper; |
9
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\Oauth2Module; |
11
|
|
|
use yii\base\Action; |
12
|
|
|
use yii\console\ExitCode; |
13
|
|
|
use yii\console\widgets\Table; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property Oauth2PersonalAccessTokenController $controller |
17
|
|
|
*/ |
18
|
|
|
class Oauth2GeneratePatAction extends Action |
19
|
|
|
{ |
20
|
|
|
public function run() |
21
|
|
|
{ |
22
|
|
|
$module = $this->controller->module; |
23
|
|
|
|
24
|
|
|
$clientIdentifier = $this->controller->client; |
25
|
|
|
$clientSecret = $this->controller->clientSecret; |
26
|
|
|
$userId = $this->controller->user; |
27
|
|
|
$scope = $this->controller->scope; |
28
|
|
|
|
29
|
|
|
if (empty($clientIdentifier)) { |
30
|
|
|
$clientIdentifier = $this->controller->prompt('Client Identifier?', [ |
31
|
|
|
'required' => true, |
32
|
|
|
'default' => $defaultClientIdentifier ?? null, |
|
|
|
|
33
|
|
|
'validator' => function ($input, &$error) { |
34
|
|
|
$client = $this->getClient($input); |
35
|
|
|
if ($client) { |
|
|
|
|
36
|
|
|
if (!$client->validateGrantType(Oauth2Module::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN)) { |
37
|
|
|
$error = 'The "Personal Access Token" grant type is not enabled for client "' . $input . '".'; |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
} else { |
41
|
|
|
$error = 'No client with identifier "' . $input . '" found.'; |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
return true; |
45
|
|
|
}, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$client = $this->getClient($clientIdentifier); |
50
|
|
|
|
51
|
|
|
if (empty($clientSecret) && $client->isConfidential()) { |
52
|
|
|
$clientSecret = $this->controller->prompt('Client Secret?', [ |
53
|
|
|
'required' => true, |
54
|
|
|
'default' => $defaultClientSecret ?? null, |
|
|
|
|
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($userId)) { |
59
|
|
|
$userId = $this->controller->prompt('User Identifier?', [ |
60
|
|
|
'required' => true, |
61
|
|
|
'default' => $defaultUserId ?? null, |
|
|
|
|
62
|
|
|
'validator' => function ($input, &$error) { |
63
|
|
|
$user = $this->getUser($input); |
64
|
|
|
if (!$user) { |
65
|
|
|
$error = 'No user with identifier "' . $input . '" found.'; |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
return true; |
69
|
|
|
}, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (empty($scope)) { |
74
|
|
|
$scope = $this->controller->prompt('Scope?', [ |
75
|
|
|
'required' => false, |
76
|
|
|
'default' => $defaultScope ?? null, |
|
|
|
|
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$result = $module->generatePersonalAccessToken( |
81
|
|
|
$clientIdentifier, |
82
|
|
|
$userId, |
83
|
|
|
$scope, |
84
|
|
|
$clientSecret, |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
foreach ($result as $key => $value) { |
88
|
|
|
$this->controller->stdout($key . ': ' . $value . PHP_EOL); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return ExitCode::OK; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getClient($identifier) |
95
|
|
|
{ |
96
|
|
|
return $this->controller->module->getClientRepository()->getClientEntity($identifier); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function getUser($identifier) |
100
|
|
|
{ |
101
|
|
|
return $this->controller->module->getUserRepository()->getUserEntityByIdentifier($identifier); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|