1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/Traits/HasEncryptedAttributes.php. |
4
|
|
|
* |
5
|
|
|
* @author Austin Heap <[email protected]> |
6
|
|
|
* @version v0.1.0 |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace AustinHeap\Database\Encryption\Traits; |
11
|
|
|
|
12
|
|
|
use Log; |
13
|
|
|
use Crypt; |
14
|
|
|
use DatabaseEncryption; |
|
|
|
|
15
|
|
|
use Illuminate\Contracts\Encryption\DecryptException; |
16
|
|
|
use Illuminate\Contracts\Encryption\EncryptException; |
17
|
|
|
use Illuminate\Support\Arr; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* HasEncryptedAttributes. |
21
|
|
|
* |
22
|
|
|
* Automatically encrypt and decrypt Laravel 5.5+ Eloquent values |
23
|
|
|
* |
24
|
|
|
* ### Example |
25
|
|
|
* |
26
|
|
|
* <code> |
27
|
|
|
* use AustinHeap\Database\Encryption\Traits\HasEncryptedAttributes; |
28
|
|
|
* |
29
|
|
|
* class User extends Eloquent { |
30
|
|
|
* |
31
|
|
|
* use HasEncryptedAttributes; |
32
|
|
|
* |
33
|
|
|
* protected $encrypted = [ |
34
|
|
|
* 'address_line_1', 'first_name', 'last_name', 'postcode' |
35
|
|
|
* ]; |
36
|
|
|
* } |
37
|
|
|
* </code> |
38
|
|
|
* |
39
|
|
|
* ### Summary of Methods in Illuminate\Database\Eloquent\Model |
40
|
|
|
* |
41
|
|
|
* This surveys the major methods in the Laravel Model class as of |
42
|
|
|
* Laravel v5.5 and checks to see how those models set attributes |
43
|
|
|
* and hence how they are affected by this trait. |
44
|
|
|
* |
45
|
|
|
* - __construct -- calls fill() |
46
|
|
|
* - fill() -- calls setAttribute() which has been overridden. |
47
|
|
|
* - hydrate() -- TBD |
48
|
|
|
* - create() -- calls constructor and hence fill() |
49
|
|
|
* - firstOrCreate -- calls constructor |
50
|
|
|
* - firstOrNew -- calls constructor |
51
|
|
|
* - updateOrCreate -- calls fill() |
52
|
|
|
* - update() -- calls fill() |
53
|
|
|
* - toArray() -- calls attributesToArray() |
54
|
|
|
* - jsonSerialize() -- calls toArray() |
55
|
|
|
* - toJson() -- calls toArray() |
56
|
|
|
* - attributesToArray() -- has been over-ridden here. |
57
|
|
|
* - getAttribute -- calls getAttributeValue() |
58
|
|
|
* - getAttributeValue -- calls getAttributeFromArray() |
59
|
|
|
* - getAttributeFromArray -- calls getArrayableAttributes |
60
|
|
|
* - getArrayableAttributes -- has been over-ridden here. |
61
|
|
|
* - setAttribute -- has been over-ridden here. |
62
|
|
|
* - getAttributes -- has been over-ridden here. |
63
|
|
|
* |
64
|
|
|
* @see Illuminate\Support\Facades\Crypt |
65
|
|
|
* @see Illuminate\Contracts\Encryption\Encrypter |
66
|
|
|
* @see Illuminate\Encryption\Encrypter |
67
|
|
|
* @link http://laravel.com/docs/5.5/eloquent |
68
|
|
|
* @link https://github.com/austinheap/laravel-database-encryption |
69
|
|
|
* @link https://packagist.org/packages/austinheap/laravel-database-encryption |
70
|
|
|
* @link https://austinheap.github.io/laravel-database-encryption/classes/AustinHeap.Database.Encryption.EncryptionServiceProvider.html |
71
|
|
|
*/ |
72
|
|
|
trait HasEncryptedAttributes |
73
|
|
|
{ |
74
|
|
|
/** |
75
|
|
|
* Private copy of last Encryption exception to occur. |
76
|
|
|
* |
77
|
|
|
* @var null|EncryptException|DecryptException |
78
|
|
|
*/ |
79
|
|
|
private $lastEncryptionException = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the last encryption-related exception to occur, if any. |
83
|
|
|
* |
84
|
|
|
* @return null|EncryptException|DecryptException |
85
|
|
|
*/ |
86
|
2 |
|
public function getLastEncryptionException() |
87
|
|
|
{ |
88
|
2 |
|
return $this->lastEncryptionException; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the last encryption-related exception to occur, if any. |
93
|
|
|
* |
94
|
|
|
* @param null|EncryptException|DecryptException $exception |
95
|
|
|
* @param null|string $function |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
2 |
|
protected function setLastEncryptionException($exception, ?string $function = null): self |
100
|
|
|
{ |
101
|
2 |
|
Log::debug('Ignored exception "'.get_class($exception).'" in function "'.(is_null($function) ? '(unknown)' : $function).'": '.$exception->getMessage()); |
|
|
|
|
102
|
|
|
|
103
|
2 |
|
$this->lastEncryptionException = $exception; |
104
|
|
|
|
105
|
2 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the configuration setting for the prefix used to determine if a string is encrypted. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
11 |
|
protected function getEncryptionPrefix(): string |
114
|
|
|
{ |
115
|
11 |
|
return DatabaseEncryption::getHeaderPrefix(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determine whether an attribute should be encrypted. |
120
|
|
|
* |
121
|
|
|
* @param string $key |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
11 |
|
protected function shouldEncrypt($key): bool |
126
|
|
|
{ |
127
|
11 |
|
$encrypt = isset($this->encrypted) ? $this->encrypted : []; |
|
|
|
|
128
|
|
|
|
129
|
11 |
|
return in_array($key, $encrypt); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Determine whether a string has already been encrypted. |
134
|
|
|
* |
135
|
|
|
* @param mixed $value |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
11 |
|
protected function isEncrypted($value): bool |
140
|
|
|
{ |
141
|
11 |
|
return strpos((string) $value, $this->getEncryptionPrefix()) === 0; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the encrypted value of an attribute's value. |
146
|
|
|
* |
147
|
|
|
* This has been exposed as a public method because it is of some |
148
|
|
|
* use when searching. |
149
|
|
|
* |
150
|
|
|
* @param string $value |
151
|
|
|
* |
152
|
|
|
* @return null|string |
153
|
|
|
*/ |
154
|
9 |
|
public function encryptedAttribute($value): ?string |
155
|
|
|
{ |
156
|
9 |
|
return DatabaseEncryption::buildHeader($value).Crypt::encrypt($value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the decrypted value of an attribute's encrypted value. |
161
|
|
|
* |
162
|
|
|
* This has been exposed as a public method because it is of some |
163
|
|
|
* use when searching. |
164
|
|
|
* |
165
|
|
|
* @param string $value |
166
|
|
|
* |
167
|
|
|
* @return null|string |
168
|
|
|
*/ |
169
|
7 |
|
public function decryptedAttribute($value): ?string |
170
|
|
|
{ |
171
|
7 |
|
$characters = DatabaseEncryption::getControlCharacters('header'); |
172
|
7 |
|
$value = substr($value, strpos($value, $characters['stop']['string'])); |
173
|
|
|
|
174
|
7 |
|
return Crypt::decrypt($value); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Encrypt a stored attribute. |
179
|
|
|
* |
180
|
|
|
* @param string $key |
181
|
|
|
* |
182
|
|
|
* @return self |
183
|
|
|
*/ |
184
|
12 |
|
protected function doEncryptAttribute($key): self |
185
|
|
|
{ |
186
|
12 |
|
if ($this->shouldEncrypt($key) && ! $this->isEncrypted($this->attributes[$key])) { |
187
|
|
|
try { |
188
|
12 |
|
$this->attributes[$key] = $this->encryptedAttribute($this->attributes[$key]); |
|
|
|
|
189
|
1 |
|
} catch (EncryptException $exception) { |
190
|
1 |
|
$this->setLastEncryptionException($exception, __FUNCTION__); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
12 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Decrypt an attribute if required. |
199
|
|
|
* |
200
|
|
|
* @param string $key |
201
|
|
|
* @param mixed $value |
202
|
|
|
* |
203
|
|
|
* @return mixed |
204
|
|
|
*/ |
205
|
9 |
|
protected function doDecryptAttribute($key, $value) |
206
|
|
|
{ |
207
|
9 |
|
if ($this->shouldEncrypt($key) && $this->isEncrypted($value)) { |
208
|
|
|
try { |
209
|
8 |
|
return $this->decryptedAttribute($value); |
210
|
1 |
|
} catch (DecryptException $exception) { |
211
|
1 |
|
$this->setLastEncryptionException($exception, __FUNCTION__); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
9 |
|
return $value; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Decrypt each attribute in the array as required. |
220
|
|
|
* |
221
|
|
|
* @param array $attributes |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
3 |
|
public function doDecryptAttributes($attributes) |
226
|
|
|
{ |
227
|
3 |
|
foreach ($attributes as $key => $value) { |
228
|
3 |
|
$attributes[$key] = $this->doDecryptAttribute($key, $value); |
229
|
|
|
} |
230
|
|
|
|
231
|
3 |
|
return $attributes; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// |
235
|
|
|
// Methods below here override methods within the base Laravel/Illuminate/Eloquent |
236
|
|
|
// model class and may need adjusting for later releases of Laravel. |
237
|
|
|
// |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Decrypt encrypted data before it is processed by cast attribute. |
241
|
|
|
* |
242
|
|
|
* @param $key |
243
|
|
|
* @param $value |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
5 |
|
protected function castAttribute($key, $value) |
248
|
|
|
{ |
249
|
5 |
|
return parent::castAttribute($key, $this->doDecryptAttribute($key, $value)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the model's original attribute values. |
254
|
|
|
* |
255
|
|
|
* @param string|null $key |
256
|
|
|
* @param mixed $default |
257
|
|
|
* @return mixed|array |
258
|
|
|
*/ |
259
|
6 |
|
public function getOriginal($key = null, $default = null) |
260
|
|
|
{ |
261
|
6 |
|
return Arr::get($this->original, $key, $default); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Determine if the given attribute is a date or date castable. |
266
|
|
|
* |
267
|
|
|
* @param string $key |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
9 |
|
protected function isDateAttribute($key) |
271
|
|
|
{ |
272
|
9 |
|
return in_array($key, $this->getDates()) || |
|
|
|
|
273
|
9 |
|
$this->isDateCastable($key); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Convert a DateTime to a storable string. |
278
|
|
|
* |
279
|
|
|
* @param \DateTime|int $value |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
9 |
|
public function fromDateTime($value) |
283
|
|
|
{ |
284
|
9 |
|
return empty($value) ? $value : $this->asDateTime($value)->format( |
|
|
|
|
285
|
9 |
|
$this->getDateFormat() |
|
|
|
|
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Determine whether an attribute should be cast to a native type. |
291
|
|
|
* |
292
|
|
|
* @param string $key |
293
|
|
|
* @param array|string|null $types |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
9 |
|
public function hasCast($key, $types = null) |
297
|
|
|
{ |
298
|
9 |
|
if (array_key_exists($key, $this->getCasts())) { |
|
|
|
|
299
|
9 |
|
return $types ? in_array($this->getCastType($key), (array) $types, true) : true; |
|
|
|
|
300
|
|
|
} |
301
|
9 |
|
return false; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the attributes that have been changed since last sync. |
306
|
|
|
* |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
9 |
|
public function getDirty() |
310
|
|
|
{ |
311
|
9 |
|
$dirty = []; |
312
|
|
|
|
313
|
9 |
|
foreach ($this->attributes as $key => $value) { |
314
|
9 |
|
if (! $this->originalIsEquivalent($key, $value)) { |
315
|
9 |
|
$dirty[$key] = $value; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
9 |
|
return $dirty; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Determine if the new and old values for a given key are equivalent. |
324
|
|
|
* |
325
|
|
|
* @param string $key |
326
|
|
|
* @param mixed $current |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
9 |
|
protected function originalIsEquivalent($key, $current) |
330
|
|
|
{ |
331
|
9 |
|
if (! array_key_exists($key, $this->original)) { |
|
|
|
|
332
|
9 |
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
6 |
|
$original = $this->getOriginal($key); |
336
|
|
|
|
337
|
6 |
|
if ($this->shouldEncrypt($key)) { |
338
|
6 |
|
$current = $this->decryptedAttribute($current); |
339
|
6 |
|
$original = $this->decryptedAttribute($this->getOriginal($key)); |
340
|
|
|
} |
341
|
|
|
|
342
|
6 |
|
if ($current === $original) { |
343
|
6 |
|
return true; |
344
|
4 |
|
} elseif (is_null($current)) { |
345
|
|
|
return false; |
346
|
4 |
|
} elseif ($this->isDateAttribute($key)) { |
347
|
|
|
return $this->fromDateTime($current) === |
|
|
|
|
348
|
|
|
$this->fromDateTime($original); |
349
|
4 |
|
} elseif ($this->hasCast($key)) { |
350
|
|
|
return $this->castAttribute($key, $current) === |
351
|
|
|
$this->castAttribute($key, $original); |
352
|
|
|
} |
353
|
4 |
|
return is_numeric($current) && is_numeric($original) |
354
|
4 |
|
&& strcmp((string) $current, (string) $original) === 0; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Set a given attribute on the model. |
359
|
|
|
* |
360
|
|
|
* @param string $key |
361
|
|
|
* @param mixed $value |
362
|
|
|
* |
363
|
|
|
* @return void |
364
|
|
|
*/ |
365
|
11 |
|
public function setAttribute($key, $value) |
366
|
|
|
{ |
367
|
11 |
|
parent::setAttribute($key, $value); |
368
|
|
|
|
369
|
11 |
|
$this->doEncryptAttribute($key); |
370
|
11 |
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Get an attribute from the $attributes array. |
374
|
|
|
* |
375
|
|
|
* @param string $key |
376
|
|
|
* |
377
|
|
|
* @return mixed |
378
|
|
|
*/ |
379
|
5 |
|
protected function getAttributeFromArray($key) |
380
|
|
|
{ |
381
|
5 |
|
return $this->doDecryptAttribute($key, parent::getAttributeFromArray($key)); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Get an attribute array of all arrayable attributes. |
386
|
|
|
* |
387
|
|
|
* @return array |
388
|
|
|
*/ |
389
|
1 |
|
protected function getArrayableAttributes() |
390
|
|
|
{ |
391
|
1 |
|
return $this->doDecryptAttributes(parent::getArrayableAttributes()); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get all of the current attributes on the model. |
396
|
|
|
* |
397
|
|
|
* @return array |
398
|
|
|
*/ |
399
|
2 |
|
public function getAttributes() |
400
|
|
|
{ |
401
|
2 |
|
return $this->doDecryptAttributes(parent::getAttributes()); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths