Skip to content

Commit 5aaf057

Browse files
committed
Fix encrypter using wrong configuration to read old key
1 parent 099ab4f commit 5aaf057

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/Encryption/Encrypter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function decrypt($payload, $unserialize = true)
2323
} catch (\Throwable $e) {
2424
$currentKey = $this->key;
2525

26-
$this->key = Str::startsWith(config('identity.old_key'), 'base64:')
27-
? base64_decode(substr(config('identity.old_key'), 7))
28-
: config('identity.old_key');
26+
$this->key = Str::startsWith(config('identities.old_key'), 'base64:')
27+
? base64_decode(substr(config('identities.old_key'), 7))
28+
: config('identities.old_key');
2929

3030
return tap(parent::decrypt($payload, $unserialize), function () use ($currentKey) {
3131
$this->key = $currentKey;

tests/TestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use ReflectionFunction;
6+
use Illuminate\Support\Str;
67
use Illuminate\Events\Dispatcher;
78
use Laravel\Socialite\SocialiteServiceProvider;
89
use Orchestra\Testbench\TestCase as BaseTestCase;
@@ -44,6 +45,11 @@ protected function getEnvironmentSetUp($app)
4445
'redirect' => null,
4546
'instance_uri' => 'https://gitlab.com/'
4647
]);
48+
49+
$key = Str::random(32);
50+
$app['config']->set('app.key', $key);
51+
$app['config']->set('app.cipher', 'AES-256-CBC');
52+
$app['config']->set('identities.key', $key);
4753
}
4854

4955
/**

tests/Unit/EncrypterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Illuminate\Support\Str;
7+
use Oneofftech\Identities\Encryption\Encrypter;
8+
use Oneofftech\Identities\Facades\IdentityCrypt;
9+
10+
class EncrypterTest extends TestCase
11+
{
12+
protected function getEnvironmentSetUp($app)
13+
{
14+
parent::getEnvironmentSetUp($app);
15+
16+
$app['config']->set('identities.old_key', Str::random(32));
17+
}
18+
19+
public function test_value_encrypted_with_old_key_can_be_decrypted()
20+
{
21+
$encrypter = new Encrypter(config('identities.old_key'), 'AES-256-CBC');
22+
23+
$encryptedWithOldKey = $encrypter->encrypt('test');
24+
25+
$decrypted = IdentityCrypt::decrypt($encryptedWithOldKey);
26+
27+
$this->assertEquals('test', $decrypted);
28+
}
29+
30+
public function test_value_can_be_encrypted()
31+
{
32+
$encrypted = IdentityCrypt::encrypt('test');
33+
34+
$decrypted = IdentityCrypt::decrypt($encrypted);
35+
36+
$this->assertEquals('test', $decrypted);
37+
}
38+
}

tests/Unit/IdentityCryptTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Oneofftech\Identities\Encryption\Encrypter;
7+
use Oneofftech\Identities\Facades\IdentityCrypt;
8+
9+
class IdentityCryptTest extends TestCase
10+
{
11+
public function test_value_can_be_hashed()
12+
{
13+
$value = 'A value';
14+
$expected = 'af4ca8f543cdca304ca2345c4d80db84d004889804974328da71bf1c451a2c2d23a71e2c853909bf5596899e74bc82b2019d20fcc93850c4d6d86fd04a67bc5d';
15+
16+
$result = IdentityCrypt::hash($value);
17+
18+
$this->assertEquals($expected, $result);
19+
}
20+
21+
public function test_facade_return_encrypter_instance()
22+
{
23+
$this->assertInstanceOf(Encrypter::class, IdentityCrypt::getFacadeRoot());
24+
}
25+
}

0 commit comments

Comments
 (0)