|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* JwtService.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\services\mySql |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\services\mySql; |
|
16
|
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateIndexException; |
|
18
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateKeyException; |
|
19
|
|
|
use sweelix\oauth2\server\interfaces\JwtModelInterface; |
|
20
|
|
|
use sweelix\oauth2\server\interfaces\JwtServiceInterface; |
|
21
|
|
|
use yii\db\Exception as DatabaseException; |
|
22
|
|
|
use Yii; |
|
23
|
|
|
use yii\db\Query; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is the jwt service for mySql |
|
27
|
|
|
* database structure |
|
28
|
|
|
* * oauth2:jwt:<jid> : hash (Jwt) |
|
29
|
|
|
* |
|
30
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
31
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
|
32
|
|
|
* @license http://www.sweelix.net/license license |
|
33
|
|
|
* @version 1.2.0 |
|
34
|
|
|
* @link http://www.sweelix.net |
|
35
|
|
|
* @package sweelix\oauth2\server\services\mySql |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
class JwtService extends BaseService implements JwtServiceInterface |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string sql jwts table |
|
42
|
|
|
*/ |
|
43
|
|
|
public $jwtsTable = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Save Jwt |
|
47
|
|
|
* @param JwtModelInterface $jwt |
|
48
|
|
|
* @param null|array $attributes attributes to save |
|
49
|
|
|
* @return bool |
|
50
|
|
|
* @throws DatabaseException |
|
51
|
|
|
* @throws DuplicateIndexException |
|
52
|
|
|
* @throws DuplicateKeyException |
|
53
|
|
|
* @since 1.0.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function insert(JwtModelInterface $jwt, $attributes) |
|
56
|
|
|
{ |
|
57
|
|
|
$result = false; |
|
58
|
|
|
if (!$jwt->beforeSave(true)) { |
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
$entity = (new Query()) |
|
62
|
|
|
->select('*') |
|
63
|
|
|
->from($this->jwtsTable) |
|
64
|
|
|
->where('id = :id', [':id' => $jwt->id]) |
|
|
|
|
|
|
65
|
|
|
->one($this->db); |
|
66
|
|
|
if ($entity !== false) { |
|
67
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $jwt->id . '"'); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
$values = $jwt->getDirtyAttributes($attributes); |
|
|
|
|
|
|
70
|
|
|
$jwtParameters = []; |
|
71
|
|
|
$this->setAttributesDefinitions($jwt->attributesDefinition()); |
|
72
|
|
|
foreach ($values as $key => $value) { |
|
73
|
|
|
if ($value !== null) { |
|
74
|
|
|
$jwtParameters[$key] = $this->convertToDatabase($key, $value); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
$jwtParameters['dateCreated'] = date('Y-m-d H:i:s'); |
|
78
|
|
|
$jwtParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
|
79
|
|
|
try { |
|
80
|
|
|
$this->db->createCommand() |
|
81
|
|
|
->insert($this->jwtsTable, $jwtParameters) |
|
82
|
|
|
->execute(); |
|
83
|
|
|
} catch (DatabaseException $e) { |
|
84
|
|
|
// @codeCoverageIgnoreStart |
|
85
|
|
|
// we have a MYSQL exception, we should not discard |
|
86
|
|
|
Yii::debug('Error while inserting entity', __METHOD__); |
|
87
|
|
|
throw $e; |
|
88
|
|
|
// @codeCoverageIgnoreEnd |
|
89
|
|
|
} |
|
90
|
|
|
$changedAttributes = array_fill_keys(array_keys($values), null); |
|
91
|
|
|
$jwt->setOldAttributes($values); |
|
92
|
|
|
$jwt->afterSave(true, $changedAttributes); |
|
93
|
|
|
$result = true; |
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Update Jwt |
|
99
|
|
|
* @param JwtModelInterface $jwt |
|
100
|
|
|
* @param null|array $attributes attributes to save |
|
101
|
|
|
* @return bool |
|
102
|
|
|
* @throws DatabaseException |
|
103
|
|
|
* @throws DuplicateIndexException |
|
104
|
|
|
* @throws DuplicateKeyException |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function update(JwtModelInterface $jwt, $attributes) |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$jwt->beforeSave(false)) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$values = $jwt->getDirtyAttributes($attributes); |
|
|
|
|
|
|
113
|
|
|
$modelKey = $jwt->key(); |
|
114
|
|
|
if (isset($values[$modelKey]) === true) { |
|
115
|
|
|
$entity = (new Query()) |
|
116
|
|
|
->select('*') |
|
117
|
|
|
->from($this->jwtsTable) |
|
118
|
|
|
->where('id = :id', [':id' => $values[$modelKey]]) |
|
119
|
|
|
->one($this->db); |
|
120
|
|
|
if ($entity !== false) { |
|
121
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"'); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
$jwtKey = isset($values[$modelKey]) ? $values[$modelKey] : $jwt->getKey(); |
|
125
|
|
|
|
|
126
|
|
|
$jwtParameters = []; |
|
127
|
|
|
$this->setAttributesDefinitions($jwt->attributesDefinition()); |
|
128
|
|
|
foreach ($values as $key => $value) { |
|
129
|
|
|
$jwtParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null; |
|
130
|
|
|
} |
|
131
|
|
|
$jwtParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
|
132
|
|
|
try { |
|
133
|
|
|
if (array_key_exists($modelKey, $values) === true) { |
|
134
|
|
|
$oldJwtKey = $jwt->getOldKey(); |
|
135
|
|
|
$this->db->createCommand() |
|
136
|
|
|
->update($this->jwtsTable, $jwtParameters, 'id = :id', [':id' => $oldJwtKey]) |
|
137
|
|
|
->execute(); |
|
138
|
|
|
} else { |
|
139
|
|
|
$this->db->createCommand() |
|
140
|
|
|
->update($this->jwtsTable, $jwtParameters, 'id = :id', [':id' => $jwtKey]) |
|
141
|
|
|
->execute(); |
|
142
|
|
|
} |
|
143
|
|
|
} catch (DatabaseException $e) { |
|
144
|
|
|
// @codeCoverageIgnoreStart |
|
145
|
|
|
// we have a MYSQL exception, we should not discard |
|
146
|
|
|
Yii::debug('Error while updating entity', __METHOD__); |
|
147
|
|
|
throw $e; |
|
148
|
|
|
// @codeCoverageIgnoreEnd |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$changedAttributes = []; |
|
152
|
|
|
foreach ($values as $name => $value) { |
|
153
|
|
|
$oldAttributes = $jwt->getOldAttributes(); |
|
154
|
|
|
$changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
|
155
|
|
|
$jwt->setOldAttribute($name, $value); |
|
156
|
|
|
} |
|
157
|
|
|
$jwt->afterSave(false, $changedAttributes); |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @inheritdoc |
|
163
|
|
|
*/ |
|
164
|
|
|
public function save(JwtModelInterface $jwt, $attributes) |
|
165
|
|
|
{ |
|
166
|
|
|
if ($jwt->getIsNewRecord()) { |
|
167
|
|
|
$result = $this->insert($jwt, $attributes); |
|
168
|
|
|
} else { |
|
169
|
|
|
$result = $this->update($jwt, $attributes); |
|
170
|
|
|
} |
|
171
|
|
|
return $result; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @inheritdoc |
|
176
|
|
|
*/ |
|
177
|
|
|
public function findOne($key) |
|
178
|
|
|
{ |
|
179
|
|
|
$record = null; |
|
180
|
|
|
$jwtData = (new Query()) |
|
181
|
|
|
->select('*') |
|
182
|
|
|
->from($this->jwtsTable) |
|
183
|
|
|
->where('id = :id', [':id' => $key]) |
|
184
|
|
|
->one($this->db); |
|
185
|
|
|
if ($jwtData !== false) { |
|
186
|
|
|
$record = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface'); |
|
187
|
|
|
/** @var JwtModelInterface $record */ |
|
188
|
|
|
$properties = $record->attributesDefinition(); |
|
189
|
|
|
$this->setAttributesDefinitions($properties); |
|
190
|
|
|
$attributes = []; |
|
191
|
|
|
foreach ($jwtData as $key => $value) { |
|
192
|
|
|
if (isset($properties[$key]) === true) { |
|
193
|
|
|
$jwtData[$key] = $this->convertToModel($key, $value); |
|
194
|
|
|
$record->setAttribute($key, $jwtData[$key]); |
|
195
|
|
|
$attributes[$key] = $jwtData[$key]; |
|
196
|
|
|
// @codeCoverageIgnoreStart |
|
197
|
|
|
} elseif ($record->canSetProperty($key)) { |
|
198
|
|
|
// TODO: find a way to test attribute population |
|
199
|
|
|
$record->{$key} = $value; |
|
200
|
|
|
} |
|
201
|
|
|
// @codeCoverageIgnoreEnd |
|
202
|
|
|
} |
|
203
|
|
|
if (empty($attributes) === false) { |
|
204
|
|
|
$record->setOldAttributes($attributes); |
|
205
|
|
|
} |
|
206
|
|
|
$record->afterFind(); |
|
207
|
|
|
} |
|
208
|
|
|
return $record; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @inheritdoc |
|
213
|
|
|
*/ |
|
214
|
|
|
public function delete(JwtModelInterface $jwt) |
|
215
|
|
|
{ |
|
216
|
|
|
$result = false; |
|
217
|
|
|
if ($jwt->beforeDelete()) { |
|
218
|
|
|
//TODO: check results to return correct information |
|
219
|
|
|
$this->db->createCommand() |
|
220
|
|
|
->delete($this->jwtsTable, 'id = :id', [':id' => $jwt->getKey()]) |
|
221
|
|
|
->execute(); |
|
222
|
|
|
$jwt->setIsNewRecord(true); |
|
223
|
|
|
$jwt->afterDelete(); |
|
224
|
|
|
$result = true; |
|
225
|
|
|
} |
|
226
|
|
|
return $result; |
|
227
|
|
|
} |
|
228
|
|
|
} |
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: