Skip to content

Commit 960e096

Browse files
authored
Merge pull request #53 from tonysm/tm/default-encryption-to-using-string
Default to `encryptString` and `decryptString`
2 parents 15a0e37 + 1e6da76 commit 960e096

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ class Post extends Model
192192
}
193193
```
194194

195-
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`:
195+
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.
196+
197+
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:
196198

197199
```php
198200
namespace App\Providers;
199201

202+
use Illuminate\Support\Facades\Crypt;
200203
use Illuminate\Support\ServiceProvider;
201204
use Tonysm\RichTextLaravel\RichTextLaravel;
202205

@@ -205,12 +208,15 @@ class AppServiceProvider extends ServiceProvider
205208
// ...
206209
public function boot(): void
207210
{
208-
RichTextLaravel::encryptAsString();
211+
RichTextLaravel::encryptUsing(
212+
encryption: fn ($value, $model, $key) => Crypt::encrypt($value),
213+
decryption: fn ($value, $model, $key) => Crypt::decrypt($value),
214+
);
209215
}
210216
}
211217
```
212218

213-
In the next major version of the package, this will be the default, so it's recommended that you do that.
219+
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)).
214220

215221
#### Key Rotation
216222

src/RichTextLaravel.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ public static function encryptUsing($encryption, $decryption): void
3030
}
3131

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

4340
public static function clearEncryptionHandlers(): void
@@ -47,14 +44,14 @@ public static function clearEncryptionHandlers(): void
4744

4845
public static function encrypt($value, $model, $key): string
4946
{
50-
$encrypt = static::$encryptHandler ??= fn ($value) => Crypt::encrypt($value);
47+
$encrypt = static::$encryptHandler ??= fn ($value) => Crypt::encryptString($value);
5148

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

5552
public static function decrypt($value, $model, $key): ?string
5653
{
57-
$decrypt = static::$decryptHandler ??= fn ($value) => Crypt::decrypt($value);
54+
$decrypt = static::$decryptHandler ??= fn ($value) => Crypt::decryptString($value);
5855

5956
return $value ? call_user_func($decrypt, $value, $model, $key) : $value;
6057
}

tests/EncryptedModelTest.php

-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ public function encrypt_content_based_on_encrypted_option_at_declaration_time()
1919
$this->assertNotEncryptedRichTextAttribute($clearMessage, 'content', 'Hello World');
2020
}
2121

22-
/** @test */
23-
public function encrypts_as_string()
24-
{
25-
RichTextLaravel::encryptAsString();
26-
27-
$encryptedMessage = EncryptedMessage::create(['content' => 'Hello World']);
28-
$this->assertEncryptedRichTextAttribute($encryptedMessage, 'content', 'Hello World');
29-
}
30-
3122
private function assertEncryptedRichTextAttribute($model, $field, $expectedValue)
3223
{
3324
$this->assertStringNotContainsString($expectedValue, $encrypted = DB::table('rich_texts')->where('record_id', $model->id)->value('body'));

0 commit comments

Comments
 (0)