1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KeyController.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version 1.2.0 |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server\commands |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\commands; |
16
|
|
|
|
17
|
|
|
use yii\console\Controller; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\console\ExitCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Manage oauth keys |
23
|
|
|
* |
24
|
|
|
* @author Philippe Gaultier <[email protected]> |
25
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
26
|
|
|
* @license http://www.sweelix.net/license license |
27
|
|
|
* @version 1.2.0 |
28
|
|
|
* @link http://www.sweelix.net |
29
|
|
|
* @package sweelix\oauth2\server\commands |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
*/ |
32
|
|
|
class KeyController extends Controller |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string alias to public key file |
37
|
|
|
*/ |
38
|
|
|
public $publicKey; |
39
|
|
|
/** |
40
|
|
|
* @var string alias to private key file |
41
|
|
|
*/ |
42
|
|
|
public $privateKey; |
43
|
|
|
/** |
44
|
|
|
* @var string encryption algorithm |
45
|
|
|
*/ |
46
|
|
|
public $encryptionAlgorithm; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function options($actionID) |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'publicKey', |
55
|
|
|
'privateKey', |
56
|
|
|
'encryptionAlgorithm', |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create new Oauth CypherKey |
62
|
|
|
* @param string $id Should be client-id or default for common key |
63
|
|
|
* @return int |
64
|
|
|
* @throws \yii\base\InvalidConfigException |
65
|
|
|
* @throws \yii\base\UnknownClassException |
66
|
|
|
* @since 1.0.0 |
67
|
|
|
*/ |
68
|
|
|
public function actionCreate($id) |
69
|
|
|
{ |
70
|
|
|
$cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface'); |
71
|
|
|
/* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */ |
72
|
|
|
$cypherKey->id = $id; |
|
|
|
|
73
|
|
|
$hasKey = false; |
74
|
|
|
if (($this->privateKey !== null) && ($this->publicKey !== null)) { |
75
|
|
|
$this->privateKey = Yii::getAlias($this->privateKey); |
|
|
|
|
76
|
|
|
$this->publicKey = Yii::getAlias($this->publicKey); |
|
|
|
|
77
|
|
|
if ((file_exists($this->privateKey) === true) && (file_exists($this->publicKey) === true)) { |
78
|
|
|
$cypherKey->privateKey = file_get_contents($this->privateKey); |
|
|
|
|
79
|
|
|
$cypherKey->publicKey = file_get_contents($this->publicKey); |
|
|
|
|
80
|
|
|
$hasKey = true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if ($hasKey === false) { |
84
|
|
|
$cypherKey->generateKeys(); |
85
|
|
|
} |
86
|
|
|
if ($cypherKey->save() === true) { |
87
|
|
|
$this->stdout('Key created :' . "\n"); |
88
|
|
|
$this->stdout(' - id: ' . $cypherKey->id . "\n"); |
|
|
|
|
89
|
|
|
$this->stdout(' - Algorithm: ' . $cypherKey->encryptionAlgorithm . "\n"); |
|
|
|
|
90
|
|
|
return ExitCode::OK; |
91
|
|
|
} else { |
92
|
|
|
$this->stdout('Key cannot be created.' . "\n"); |
93
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update existing Oauth CypherKey |
99
|
|
|
* @param $id |
100
|
|
|
* @return int |
101
|
|
|
* @throws \yii\base\InvalidConfigException |
102
|
|
|
* @throws \yii\base\UnknownClassException |
103
|
|
|
*/ |
104
|
|
|
public function actionUpdate($id) |
105
|
|
|
{ |
106
|
|
|
$cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface'); |
107
|
|
|
$cypherKeyClass = get_class($cypherKey); |
108
|
|
|
/* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */ |
109
|
|
|
$cypherKey = $cypherKeyClass::findOne($id); |
110
|
|
|
if ($cypherKey !== null) { |
111
|
|
|
$hasKey = false; |
112
|
|
|
if (($this->privateKey !== null) && ($this->publicKey !== null)) { |
113
|
|
|
$this->privateKey = Yii::getAlias($this->privateKey); |
|
|
|
|
114
|
|
|
$this->publicKey = Yii::getAlias($this->publicKey); |
|
|
|
|
115
|
|
|
if ((file_exists($this->privateKey) === true) && (file_exists($this->publicKey) === true)) { |
116
|
|
|
$cypherKey->privateKey = file_get_contents($this->privateKey); |
|
|
|
|
117
|
|
|
$cypherKey->publicKey = file_get_contents($this->publicKey); |
|
|
|
|
118
|
|
|
$hasKey = true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if ($hasKey === false) { |
122
|
|
|
$cypherKey->generateKeys(); |
123
|
|
|
} |
124
|
|
|
if ($cypherKey->save() === true) { |
125
|
|
|
$this->stdout('Key updated :' . "\n"); |
126
|
|
|
$this->stdout(' - id: ' . $cypherKey->id . "\n"); |
|
|
|
|
127
|
|
|
$this->stdout(' - Algorithm: ' . $cypherKey->encryptionAlgorithm . "\n"); |
|
|
|
|
128
|
|
|
return ExitCode::OK; |
129
|
|
|
} else { |
130
|
|
|
$this->stdout('Key ' . $id . ' cannot be updated' . "\n"); |
131
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
$this->stdout('Key ' . $id . ' does not exist' . "\n"); |
135
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Delete Oauth CypherKey |
141
|
|
|
* @param $id |
142
|
|
|
* @return int |
143
|
|
|
* @throws \yii\base\InvalidConfigException |
144
|
|
|
* @throws \yii\base\UnknownClassException |
145
|
|
|
*/ |
146
|
|
|
public function actionDelete($id) |
147
|
|
|
{ |
148
|
|
|
$cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface'); |
149
|
|
|
$cypherKeyClass = get_class($cypherKey); |
150
|
|
|
/* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */ |
151
|
|
|
$cypherKey = $cypherKeyClass::findOne($id); |
152
|
|
|
if ($cypherKey !== null) { |
153
|
|
|
if ($cypherKey->delete() === true) { |
154
|
|
|
$this->stdout('Key ' . $id . ' deleted' . "\n"); |
155
|
|
|
return ExitCode::OK; |
156
|
|
|
} else { |
157
|
|
|
$this->stdout('Key ' . $id . ' cannot be deleted' . "\n"); |
158
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
159
|
|
|
} |
160
|
|
|
} else { |
161
|
|
|
$this->stdout('Key ' . $id . ' does not exist' . "\n"); |
162
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: