|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ClientController.php |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6+ |
|
6
|
|
|
* |
|
7
|
|
|
* @author pgaultier |
|
8
|
|
|
* @copyright 2010-2016 Ibitux |
|
9
|
|
|
* @license http://www.ibitux.com/license license |
|
10
|
|
|
* @version XXX |
|
11
|
|
|
* @link http://www.ibitux.com |
|
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
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manage oauth clients |
|
23
|
|
|
* |
|
24
|
|
|
* @author pgaultier |
|
25
|
|
|
* @copyright 2010-2016 Ibitux |
|
26
|
|
|
* @license http://www.ibitux.com/license license |
|
27
|
|
|
* @version XXX |
|
28
|
|
|
* @link http://www.ibitux.com |
|
29
|
|
|
* @package sweelix\oauth2\server\commands |
|
30
|
|
|
* @since XXX |
|
31
|
|
|
*/ |
|
32
|
|
|
class ClientController extends Controller |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
public $redirectUri; |
|
36
|
|
|
public $grantTypes; |
|
37
|
|
|
public $scopes; |
|
38
|
|
|
public $userId; |
|
39
|
|
|
public $name; |
|
40
|
|
|
public $isPublic; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function options($actionID) |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
// Generated 'id', |
|
49
|
|
|
// Generated 'secret', |
|
50
|
|
|
'redirectUri', |
|
51
|
|
|
'grantTypes', |
|
52
|
|
|
'scopes', |
|
53
|
|
|
'userId', |
|
54
|
|
|
'name', |
|
55
|
|
|
'isPublic' |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
/** |
|
59
|
|
|
* Create new Oauth client |
|
60
|
|
|
* @return int |
|
61
|
|
|
* @since XXX |
|
62
|
|
|
*/ |
|
63
|
|
|
public function actionCreate() |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
$client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface'); |
|
67
|
|
|
/* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */ |
|
68
|
|
|
$client->id = $this->getRandomString(); |
|
|
|
|
|
|
69
|
|
|
$client->secret = $this->getRandomString(); |
|
|
|
|
|
|
70
|
|
|
$client->name = $this->name; |
|
|
|
|
|
|
71
|
|
|
$client->redirectUri = $this->redirectUri; |
|
|
|
|
|
|
72
|
|
|
$client->userId = $this->userId; |
|
|
|
|
|
|
73
|
|
|
$client->isPublic = (bool)$this->isPublic; |
|
|
|
|
|
|
74
|
|
|
$client->scopes = empty($this->scope) ? null : explode(',', $this->scopes); |
|
|
|
|
|
|
75
|
|
|
$client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes); |
|
|
|
|
|
|
76
|
|
|
if ($client->save() === true) { |
|
77
|
|
|
$this->stdout('Client created :'."\n"); |
|
78
|
|
|
$this->stdout(' - id: ' . $client->id . "\n"); |
|
|
|
|
|
|
79
|
|
|
$this->stdout(' - secret: ' . $client->secret . "\n"); |
|
|
|
|
|
|
80
|
|
|
$this->stdout(' - name: ' . $client->name . "\n"); |
|
|
|
|
|
|
81
|
|
|
$this->stdout(' - redirectUri: ' . $client->redirectUri . "\n"); |
|
|
|
|
|
|
82
|
|
|
return Controller::EXIT_CODE_NORMAL; |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->stdout('Client cannot be created.'."\n"); |
|
85
|
|
|
return Controller::EXIT_CODE_ERROR; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function actionUpdate($id) |
|
90
|
|
|
{ |
|
91
|
|
|
$client = Client::findOne($id); |
|
92
|
|
|
if ($client !== null) { |
|
93
|
|
|
$client->redirectUri = $this->redirectUri; |
|
|
|
|
|
|
94
|
|
|
$client->name = $this->name; |
|
|
|
|
|
|
95
|
|
|
$client->userId = $this->userId; |
|
|
|
|
|
|
96
|
|
|
$client->isPublic = (bool)$this->isPublic; |
|
|
|
|
|
|
97
|
|
|
$client->scopes = empty($this->scope) ? null : explode(',', $this->scopes); |
|
|
|
|
|
|
98
|
|
|
$client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes); |
|
|
|
|
|
|
99
|
|
|
if ($client->save() === true) { |
|
100
|
|
|
$this->stdout('Client updated :' . "\n"); |
|
101
|
|
|
$this->stdout(' - id: ' . $client->id . "\n"); |
|
|
|
|
|
|
102
|
|
|
$this->stdout(' - secret: ' . $client->secret . "\n"); |
|
|
|
|
|
|
103
|
|
|
$this->stdout(' - name: ' . $client->name . "\n"); |
|
|
|
|
|
|
104
|
|
|
$this->stdout(' - redirectUri: ' . $client->redirectUri . "\n"); |
|
|
|
|
|
|
105
|
|
|
return Controller::EXIT_CODE_NORMAL; |
|
106
|
|
|
} else { |
|
107
|
|
|
$this->stdout('Client cannot be updated.'."\n"); |
|
108
|
|
|
return Controller::EXIT_CODE_ERROR; |
|
109
|
|
|
} |
|
110
|
|
|
} else { |
|
111
|
|
|
$this->stdout('Client '.$id.' does not exist'."\n"); |
|
112
|
|
|
return Controller::EXIT_CODE_ERROR; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Generate random string |
|
118
|
|
|
* @param int $length |
|
119
|
|
|
* @return string |
|
120
|
|
|
* @since XXX |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getRandomString($length = 40) |
|
123
|
|
|
{ |
|
124
|
|
|
$bytes = (int) $length/2; |
|
125
|
|
|
return bin2hex(openssl_random_pseudo_bytes($bytes)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
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: