Skip to content

Commit

Permalink
Merge pull request #53 from tonysm/tm/default-encryption-to-using-string
Browse files Browse the repository at this point in the history
Default to `encryptString` and `decryptString`
  • Loading branch information
tonysm authored Mar 16, 2024
2 parents 15a0e37 + 1e6da76 commit 960e096
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ class Post extends Model
}
```

This uses [Laravel's Encryption](https://laravel.com/docs/encryption#introduction) feature. By default, it will encrypt using the default encryption handler in Laravel, which serializes the value before encrypting it. However you can opt-in skip the serialization step and only encrypt as string by calling the following method in the `AppServiceProvider`:
This uses [Laravel's Encryption](https://laravel.com/docs/encryption#introduction) feature. By default, we'll encrypt using Laravel's `Crypt::encryptString()` and decrypt with `Crypt::decryptString()`. If you're coming from version 2 of the Rich Text Laravel package, which would default to `Crypt::encrypt()` and `Crypt::decrypt()`, you must migrate your data manually (see instructions in the [2.2.0](https://github.com/tonysm/rich-text-laravel/releases/tag/2.2.0) release). This is the recommended way to upgrade to 3.x.

With that being said, you may configure how the package handles encryption however you want to by calling the `RichTextLaravel::encryptUsing()` method on your `AppServiceProvider::boot` method. This method takes an encryption and decryption handler. The handler will receive the value, the model and key (field) that is being encrypted, like so:

```php
namespace App\Providers;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\ServiceProvider;
use Tonysm\RichTextLaravel\RichTextLaravel;

Expand All @@ -205,12 +208,15 @@ class AppServiceProvider extends ServiceProvider
// ...
public function boot(): void
{
RichTextLaravel::encryptAsString();
RichTextLaravel::encryptUsing(
encryption: fn ($value, $model, $key) => Crypt::encrypt($value),
decryption: fn ($value, $model, $key) => Crypt::decrypt($value),
);
}
}
```

In the next major version of the package, this will be the default, so it's recommended that you do that.
Again, it's recommended that you migrate your existing encrypted data and use the default encryption handler (see instructions [here](https://github.com/tonysm/rich-text-laravel/releases/tag/2.2.0)).

#### Key Rotation

Expand Down
11 changes: 4 additions & 7 deletions src/RichTextLaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ public static function encryptUsing($encryption, $decryption): void
}

/**
* Configures the Rich Text Laravel package to store encrypted data as string,
* instead of using Laravel's default encryption mode, which serializes the
* content before encrypting it.
* This will be the default.
*/
public static function encryptAsString(): void
{
static::$encryptHandler = fn ($value) => Crypt::encryptString($value);
static::$decryptHandler = fn ($value) => ($value ? Crypt::decryptString($value) : $value);
static::encryptUsing(null, null);
}

public static function clearEncryptionHandlers(): void
Expand All @@ -47,14 +44,14 @@ public static function clearEncryptionHandlers(): void

public static function encrypt($value, $model, $key): string
{
$encrypt = static::$encryptHandler ??= fn ($value) => Crypt::encrypt($value);
$encrypt = static::$encryptHandler ??= fn ($value) => Crypt::encryptString($value);

return call_user_func($encrypt, $value, $model, $key);
}

public static function decrypt($value, $model, $key): ?string
{
$decrypt = static::$decryptHandler ??= fn ($value) => Crypt::decrypt($value);
$decrypt = static::$decryptHandler ??= fn ($value) => Crypt::decryptString($value);

return $value ? call_user_func($decrypt, $value, $model, $key) : $value;
}
Expand Down
9 changes: 0 additions & 9 deletions tests/EncryptedModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ public function encrypt_content_based_on_encrypted_option_at_declaration_time()
$this->assertNotEncryptedRichTextAttribute($clearMessage, 'content', 'Hello World');
}

/** @test */
public function encrypts_as_string()
{
RichTextLaravel::encryptAsString();

$encryptedMessage = EncryptedMessage::create(['content' => 'Hello World']);
$this->assertEncryptedRichTextAttribute($encryptedMessage, 'content', 'Hello World');
}

private function assertEncryptedRichTextAttribute($model, $field, $expectedValue)
{
$this->assertStringNotContainsString($expectedValue, $encrypted = DB::table('rich_texts')->where('record_id', $model->id)->value('body'));
Expand Down

0 comments on commit 960e096

Please sign in to comment.