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