generated from spatie/package-skeleton-laravel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting MaskSensitiveData into Collection makro (#3)
* Extracting MaskSensitiveData into Collection makro * Fix styling * Tweaking comment (code ocd) * Improve MaskSensitiveData and add PHPStan baseline
- Loading branch information
1 parent
7e3a5a1
commit deaf366
Showing
5 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
<?php | ||
|
||
// config for Spekulatius/LaravelPowertools | ||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Masked fields | ||
|-------------------------------------------------------------------------- | ||
| | ||
| These properties will be replaced with "[masked]", | ||
| when logged via toLog or the model tracker. | ||
| | ||
*/ | ||
|
||
'masked_fields' => [ | ||
'/password/i', | ||
'/passwd/i', | ||
'/hash/i', | ||
'/checksum/i', | ||
'/salt/i', | ||
'/token/i', | ||
'/key/i', | ||
'/secret/i', | ||
'/acl/i', | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Call to an undefined method Spekulatius\\\\LaravelPowertools\\\\LaravelPowertoolsServiceProvider\\:\\:map\\(\\)\\.$#" | ||
count: 1 | ||
path: src/LaravelPowertoolsServiceProvider.php | ||
|
||
- | ||
message: "#^Parameter \\#1 \\$pattern of function preg_match expects string, mixed given\\.$#" | ||
count: 1 | ||
path: src/LaravelPowertoolsServiceProvider.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Spekulatius\LaravelPowertools\Tests; | ||
|
||
class MaskSensitiveDataTest extends TestCase | ||
{ | ||
public function testMaskSensitiveData() | ||
{ | ||
$data = collect([ | ||
'username' => 'johndoe', | ||
'password' => 'secret', | ||
'email' => 'johndoe@example.com', | ||
'salt' => '12345', | ||
'phone' => '555-1234', | ||
'group' => 'admin', | ||
'role' => 'superuser', | ||
'token' => 'abc123', | ||
'acl' => 'user', | ||
'api key' => '12345', | ||
'secret key' => 'abcdef', | ||
'public key' => 'ghijkl', | ||
'checksum' => '123456', | ||
]); | ||
|
||
$maskedData = $data->maskSensitiveData(); | ||
|
||
$this->assertEquals('[masked]', $maskedData['password']); | ||
$this->assertEquals('[masked]', $maskedData['salt']); | ||
$this->assertEquals('[masked]', $maskedData['token']); | ||
$this->assertEquals('[masked]', $maskedData['acl']); | ||
$this->assertEquals('[masked]', $maskedData['api key']); | ||
$this->assertEquals('[masked]', $maskedData['secret key']); | ||
$this->assertEquals('[masked]', $maskedData['public key']); | ||
$this->assertEquals('[masked]', $maskedData['checksum']); | ||
$this->assertEquals('johndoe', $maskedData['username']); | ||
$this->assertEquals('johndoe@example.com', $maskedData['email']); | ||
$this->assertEquals('555-1234', $maskedData['phone']); | ||
} | ||
|
||
public function testMaskSensitiveDataWithCustomRegex() | ||
{ | ||
// Append our custom regex to the configuration. | ||
$config = config('powertools.masked_fields', []); | ||
$config[] = '/authorization/i'; | ||
\Illuminate\Support\Facades\Config::set('powertools.masked_fields', $config); | ||
|
||
$data = collect([ | ||
'username' => 'johndoe', | ||
'authorization' => 'secret', | ||
'email' => 'johndoe@example.com', | ||
]); | ||
|
||
$maskedData = $data->maskSensitiveData(); | ||
|
||
$this->assertEquals('[masked]', $maskedData['authorization']); | ||
} | ||
} |