-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backwards-compatible encryption to provider configuration data
- Loading branch information
Showing
4 changed files
with
175 additions
and
3 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
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Traits; | ||
|
||
use Illuminate\Contracts\Encryption\DecryptException; | ||
|
||
/** | ||
* Recursively encrypts and decrypts data. | ||
*/ | ||
trait Encryption | ||
{ | ||
/** | ||
* Recursively encrypts the given data. | ||
* | ||
* @param mixed $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function encrypt($data) | ||
{ | ||
if (is_iterable($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[$key] = $this->encrypt($value); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (!is_string($data) || is_numeric($data) || empty($data)) { | ||
return $data; | ||
} | ||
|
||
return encrypt($data); | ||
} | ||
|
||
/** | ||
* Recursively decrypts the given data. | ||
* | ||
* @param mixed $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function decrypt($data) | ||
{ | ||
if (is_iterable($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[$key] = $this->decrypt($value); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (!is_string($data) || is_numeric($data) || empty($data)) { | ||
return $data; | ||
} | ||
|
||
try { | ||
return decrypt($data); | ||
} catch (DecryptException $e) { | ||
// If the payload cannot be decrypted, assume it was already plaintext. | ||
return $data; | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
database/migrations/2023_02_09_102747_encrypt_provider_configuration_data.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class EncryptProviderConfigurationData extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
foreach (DB::table('provider_configurations')->get() as $providerConfiguration) { | ||
DB::table('provider_configurations') | ||
->where('id', $providerConfiguration->id) | ||
->update([ | ||
'data' => json_encode($this->encrypt(json_decode($providerConfiguration->data, true))), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
foreach (DB::table('provider_configurations')->get() as $providerConfiguration) { | ||
DB::table('provider_configurations') | ||
->where('id', $providerConfiguration->id) | ||
->update([ | ||
'data' => json_encode($this->decrypt(json_decode($providerConfiguration->data, true))), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Recursively encrypts the given data. | ||
* | ||
* @param mixed $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function encrypt($data) | ||
{ | ||
if (is_iterable($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[$key] = $this->encrypt($value); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (!is_string($data) || is_numeric($data) || empty($data)) { | ||
return $data; | ||
} | ||
|
||
return encrypt($data); | ||
} | ||
|
||
/** | ||
* Recursively decrypts the given data. | ||
* | ||
* @param mixed $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function decrypt($data) | ||
{ | ||
if (is_iterable($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[$key] = $this->decrypt($value); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (!is_string($data) || is_numeric($data) || empty($data)) { | ||
return $data; | ||
} | ||
|
||
try { | ||
return decrypt($data); | ||
} catch (DecryptException $e) { | ||
// If the payload cannot be decrypted, assume it was already plaintext. | ||
return $data; | ||
} | ||
} | ||
} |