1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ClientController.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 sweelix\oauth2\server\models\Client; |
18
|
|
|
use yii\console\Controller; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\console\ExitCode; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Manage oauth clients |
24
|
|
|
* |
25
|
|
|
* @author Philippe Gaultier <[email protected]> |
26
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
27
|
|
|
* @license http://www.sweelix.net/license license |
28
|
|
|
* @version 1.2.0 |
29
|
|
|
* @link http://www.sweelix.net |
30
|
|
|
* @package sweelix\oauth2\server\commands |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
*/ |
33
|
|
|
class ClientController extends Controller |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
public $redirectUri; |
37
|
|
|
public $grantTypes; |
38
|
|
|
public $scopes; |
39
|
|
|
public $userId; |
40
|
|
|
public $name; |
41
|
|
|
public $isPublic; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function options($actionID) |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
// Generated 'id', |
50
|
|
|
// Generated 'secret', |
51
|
|
|
'redirectUri', |
52
|
|
|
'grantTypes', |
53
|
|
|
'scopes', |
54
|
|
|
'userId', |
55
|
|
|
'name', |
56
|
|
|
'isPublic' |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create new Oauth client |
62
|
|
|
* @return int |
63
|
|
|
* @throws \yii\base\InvalidConfigException |
64
|
|
|
* @throws \yii\base\UnknownClassException |
65
|
|
|
* @since 1.0.0 |
66
|
|
|
*/ |
67
|
|
|
public function actionCreate() |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
$client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface'); |
71
|
|
|
/* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */ |
72
|
|
|
$client->id = $this->getRandomString(); |
|
|
|
|
73
|
|
|
$client->secret = $this->getRandomString(); |
|
|
|
|
74
|
|
|
$client->name = $this->name; |
|
|
|
|
75
|
|
|
$redirectUri = empty($this->redirectUri) ? null : explode(',', $this->redirectUri); |
76
|
|
|
$client->redirectUri = $redirectUri; |
|
|
|
|
77
|
|
|
$client->userId = $this->userId; |
|
|
|
|
78
|
|
|
$client->isPublic = (bool)$this->isPublic; |
|
|
|
|
79
|
|
|
$client->scopes = empty($this->scopes) ? null : explode(',', $this->scopes); |
|
|
|
|
80
|
|
|
$client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes); |
|
|
|
|
81
|
|
|
if ($client->save() === true) { |
82
|
|
|
$this->stdout('Client created :' . "\n"); |
83
|
|
|
$this->stdout(' - id: ' . $client->id . "\n"); |
|
|
|
|
84
|
|
|
$this->stdout(' - secret: ' . $client->secret . "\n"); |
|
|
|
|
85
|
|
|
$this->stdout(' - name: ' . $client->name . "\n"); |
|
|
|
|
86
|
|
|
$this->stdout(' - redirectUri: ' . implode(',', $client->redirectUri) . "\n"); |
|
|
|
|
87
|
|
|
return ExitCode::OK; |
88
|
|
|
} else { |
89
|
|
|
$this->stdout('Client cannot be created.' . "\n"); |
90
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generate random string |
96
|
|
|
* @param int $length |
97
|
|
|
* @return string |
98
|
|
|
* @since 1.0.0 |
99
|
|
|
*/ |
100
|
|
|
protected function getRandomString($length = 40) |
101
|
|
|
{ |
102
|
|
|
$bytes = (int)$length / 2; |
103
|
|
|
return bin2hex(openssl_random_pseudo_bytes($bytes)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update Oauth client |
108
|
|
|
* @param $id |
109
|
|
|
* @return int |
110
|
|
|
* @throws \yii\base\UnknownClassException |
111
|
|
|
* @throws \yii\base\InvalidConfigException |
112
|
|
|
*/ |
113
|
|
|
public function actionUpdate($id) |
114
|
|
|
{ |
115
|
|
|
$client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface'); |
116
|
|
|
$clientClass = get_class($client); |
117
|
|
|
/* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */ |
118
|
|
|
$client = $clientClass::findOne($id); |
119
|
|
|
if ($client !== null) { |
120
|
|
|
$client->redirectUri = $this->redirectUri; |
|
|
|
|
121
|
|
|
$client->name = $this->name; |
|
|
|
|
122
|
|
|
$client->userId = $this->userId; |
|
|
|
|
123
|
|
|
$client->isPublic = (bool)$this->isPublic; |
|
|
|
|
124
|
|
|
$client->scopes = empty($this->scopes) ? null : explode(',', $this->scopes); |
|
|
|
|
125
|
|
|
$client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes); |
|
|
|
|
126
|
|
|
if ($client->save() === true) { |
127
|
|
|
$this->stdout('Client updated :' . "\n"); |
128
|
|
|
$this->stdout(' - id: ' . $client->id . "\n"); |
|
|
|
|
129
|
|
|
$this->stdout(' - secret: ' . $client->secret . "\n"); |
|
|
|
|
130
|
|
|
$this->stdout(' - name: ' . $client->name . "\n"); |
|
|
|
|
131
|
|
|
$this->stdout(' - redirectUri: ' . implode(',', $client->redirectUri) . "\n"); |
|
|
|
|
132
|
|
|
return ExitCode::OK; |
133
|
|
|
} else { |
134
|
|
|
$this->stdout('Client ' . $id . ' cannot be updated.' . "\n"); |
135
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$this->stdout('Client ' . $id . ' does not exist' . "\n"); |
139
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Delete Oauth client |
145
|
|
|
* @param $id |
146
|
|
|
* @return int |
147
|
|
|
* @throws \yii\base\InvalidConfigException |
148
|
|
|
* @throws \yii\base\UnknownClassException |
149
|
|
|
*/ |
150
|
|
|
public function actionDelete($id) |
151
|
|
|
{ |
152
|
|
|
$client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface'); |
153
|
|
|
$clientClass = get_class($client); |
154
|
|
|
/* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */ |
155
|
|
|
$client = $clientClass::findOne($id); |
156
|
|
|
if ($client !== null) { |
157
|
|
|
if ($client->delete() === true) { |
158
|
|
|
$this->stdout('Client ' . $id . ' deleted' . "\n"); |
159
|
|
|
return ExitCode::OK; |
160
|
|
|
} else { |
161
|
|
|
$this->stdout('Client ' . $id . ' cannot be deleted.' . "\n"); |
162
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
|
|
$this->stdout('Client ' . $id . ' does not exist' . "\n"); |
166
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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: