1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JwtController.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 jwts |
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 JwtController extends Controller |
33
|
|
|
{ |
34
|
|
|
public $clientId; |
35
|
|
|
public $subject; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string alias to public key file |
39
|
|
|
*/ |
40
|
|
|
public $publicKey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function options($actionID) |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'clientId', |
49
|
|
|
'subject', |
50
|
|
|
'publicKey', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create new Oauth Jwt |
56
|
|
|
* @param string $clientId Should be clientId |
57
|
|
|
* @param string $subject |
58
|
|
|
* @param string $publicKey |
59
|
|
|
* @return int |
60
|
|
|
* @throws \yii\base\InvalidConfigException |
61
|
|
|
* @throws \yii\base\UnknownClassException |
62
|
|
|
* @since 1.0.0 |
63
|
|
|
*/ |
64
|
|
|
public function actionCreate($clientId, $subject, $publicKey) |
65
|
|
|
{ |
66
|
|
|
$jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface'); |
67
|
|
|
/* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */ |
68
|
|
|
$jwt->clientId = $clientId; |
|
|
|
|
69
|
|
|
$jwt->subject = $subject; |
|
|
|
|
70
|
|
|
if ($this->publicKey !== null) { |
71
|
|
|
$this->publicKey = Yii::getAlias($this->publicKey); |
|
|
|
|
72
|
|
|
if (file_exists($this->publicKey) === true) { |
73
|
|
|
$jwt->publicKey = file_get_contents($this->publicKey); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$jwt->publicKey = $publicKey; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
if ($jwt->save() === true) { |
79
|
|
|
$this->stdout('Jwt created :' . "\n"); |
80
|
|
|
$this->stdout(' - id: ' . $jwt->id . "\n"); |
|
|
|
|
81
|
|
|
$this->stdout(' - clientId: ' . $jwt->clientId . "\n"); |
|
|
|
|
82
|
|
|
$this->stdout(' - subject: ' . $jwt->subject . "\n"); |
|
|
|
|
83
|
|
|
$this->stdout(' - publicKey: ' . $jwt->publicKey . "\n"); |
|
|
|
|
84
|
|
|
return ExitCode::OK; |
85
|
|
|
} else { |
86
|
|
|
$this->stdout('Jwt cannot be created.' . "\n"); |
87
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Update Oauth jwt |
93
|
|
|
* @param string $id Jwt id |
94
|
|
|
* @return int |
95
|
|
|
* @throws \yii\base\InvalidConfigException |
96
|
|
|
* @throws \yii\base\UnknownClassException |
97
|
|
|
*/ |
98
|
|
|
public function actionUpdate($id) |
99
|
|
|
{ |
100
|
|
|
$jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface'); |
101
|
|
|
$jwtClass = get_class($jwt); |
102
|
|
|
/* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */ |
103
|
|
|
$jwt = $jwtClass::findOne($id); |
104
|
|
|
if ($jwt !== null) { |
105
|
|
|
$jwt->clientId = $this->clientId; |
|
|
|
|
106
|
|
|
$jwt->subject = $this->subject; |
|
|
|
|
107
|
|
|
$jwt->publicKey = $this->publicKey; |
|
|
|
|
108
|
|
|
if ($jwt->save() === true) { |
109
|
|
|
$this->stdout('Jwt updated :' . "\n"); |
110
|
|
|
$this->stdout(' - id: ' . $jwt->id . "\n"); |
|
|
|
|
111
|
|
|
$this->stdout(' - clientId: ' . $jwt->clientId . "\n"); |
|
|
|
|
112
|
|
|
$this->stdout(' - subject: ' . $jwt->subject . "\n"); |
|
|
|
|
113
|
|
|
$this->stdout(' - publicKey: ' . $jwt->publicKey . "\n"); |
|
|
|
|
114
|
|
|
return ExitCode::OK; |
115
|
|
|
} else { |
116
|
|
|
$this->stdout('Jwt ' . $id . ' cannot be updated' . "\n"); |
117
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
|
|
$this->stdout('Jwt ' . $id . ' does not exist' . "\n"); |
121
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Delete Oauth jwt |
127
|
|
|
* @param string $id Jwt id |
128
|
|
|
* @return int |
129
|
|
|
* @throws \yii\base\InvalidConfigException |
130
|
|
|
* @throws \yii\base\UnknownClassException |
131
|
|
|
*/ |
132
|
|
|
public function actionDelete($id) |
133
|
|
|
{ |
134
|
|
|
$jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface'); |
135
|
|
|
$jwtClass = get_class($jwt); |
136
|
|
|
/* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */ |
137
|
|
|
$jwt = $jwtClass::findOne($id); |
138
|
|
|
if ($jwt !== null) { |
139
|
|
|
if ($jwt->delete() === true) { |
140
|
|
|
$this->stdout('Jwt ' . $id . ' deleted' . "\n"); |
141
|
|
|
return ExitCode::OK; |
142
|
|
|
} else { |
143
|
|
|
$this->stdout('Jwt ' . $id . ' cannot be deleted' . "\n"); |
144
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
$this->stdout('Jwt ' . $id . ' does not exist' . "\n"); |
148
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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: