1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\console\client; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface; |
6
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController; |
7
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\DiHelper; |
8
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface; |
9
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientScopeInterface; |
11
|
|
|
use rhertogh\Yii2Oauth2Server\Oauth2Module; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\base\Action; |
14
|
|
|
use yii\base\InvalidArgumentException; |
15
|
|
|
use yii\base\InvalidCallException; |
16
|
|
|
use yii\console\ExitCode; |
17
|
|
|
use yii\helpers\Console; |
18
|
|
|
use yii\helpers\StringHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @property Oauth2ClientController $controller |
22
|
|
|
*/ |
23
|
|
|
class Oauth2SetClientSecretAction extends Action |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @throws \yii\base\InvalidConfigException |
27
|
|
|
*/ |
28
|
|
|
public function run() |
29
|
|
|
{ |
30
|
|
|
$module = $this->controller->module; |
31
|
|
|
|
32
|
|
|
$identifier = $this->controller->identifier; |
33
|
|
|
$secret = $this->controller->secret; |
34
|
|
|
$oldSecretValidUntilInput = $this->controller->oldSecretValidUntil; |
35
|
|
|
|
36
|
|
|
if (empty($identifier)) { |
37
|
|
|
throw new InvalidCallException('The `identifier` option must be specified.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @var Oauth2ClientInterface $client */ |
41
|
|
|
$client = $module->getClientRepository()->findModelByIdentifier($identifier); |
42
|
|
|
if (empty($client)) { |
43
|
|
|
throw new InvalidCallException('No client with identifier "' . $identifier . '" found.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (empty($secret)) { |
47
|
|
|
$secret = $this->controller->prompt('Client Secret?', [ |
48
|
|
|
'required' => true, |
49
|
|
|
'validator' => [$client, 'validateNewSecret'], |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$oldSecretValidUntil = null; |
54
|
|
|
if ($oldSecretValidUntilInput) { |
55
|
|
|
if (StringHelper::startsWith($oldSecretValidUntilInput, 'P')) { |
56
|
|
|
try { |
57
|
|
|
$oldSecretValidUntil = new \DateInterval($oldSecretValidUntilInput); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$oldSecretValidUntil = false; |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
try { |
63
|
|
|
$oldSecretValidUntil = new \DateTimeImmutable($oldSecretValidUntilInput); |
64
|
|
|
} catch (\Exception $e) { |
65
|
|
|
$oldSecretValidUntil = false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
if (empty($oldSecretValidUntil)) { |
69
|
|
|
throw new InvalidArgumentException('Unable to parse "' . $oldSecretValidUntilInput |
70
|
|
|
. '" as a DateTime or DateInterval.'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$client->setSecret($secret, $module->getEncryptor(), $oldSecretValidUntil); |
|
|
|
|
75
|
|
|
$client->persist(); |
76
|
|
|
|
77
|
|
|
if ($oldSecretValidUntil) { |
78
|
|
|
$oldSecretValidUntilInfo = ' The previous secret will be valid until ' |
79
|
|
|
. Yii::$app->formatter->asDatetime($client->getOldSecretValidUntil(), 'long'); |
80
|
|
|
} else { |
81
|
|
|
$oldSecretValidUntilInfo = ' Any previous secret is cleared.'; |
82
|
|
|
} |
83
|
|
|
$this->controller->stdout('Successfully updated client secret.' . $oldSecretValidUntilInfo |
84
|
|
|
. PHP_EOL, Console::FG_GREEN); |
85
|
|
|
|
86
|
|
|
return ExitCode::OK; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|