diff --git a/src/DelegateAuthServiceProvider.php b/src/DelegateAuthServiceProvider.php index 2bf1591..3b25a9b 100644 --- a/src/DelegateAuthServiceProvider.php +++ b/src/DelegateAuthServiceProvider.php @@ -9,26 +9,26 @@ class DelegateAuthServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom( - __DIR__.'/../config/delegate-auth.php', 'delegate-auth' + __DIR__ . "/../config/delegate-auth.php", + "delegate-auth", ); } public function boot(): void { - $this->publishes([ - __DIR__.'/../config/delegate-auth.php' => config_path('delegate-auth.php'), - ], 'delegate-auth-config'); + $this->publishes( + [ + __DIR__ . "/../config/delegate-auth.php" => config_path( + "delegate-auth.php", + ), + ], + "delegate-auth-config", + ); - $this->publishes([ - __DIR__.'/../resources/views/errors/401.blade.php' => resource_path('views/errors/401.blade.php'), - ], 'delegate-auth-views'); - - $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); + $this->loadRoutesFrom(__DIR__ . "/../routes/web.php"); if ($this->app->runningInConsole()) { - $this->commands([ - Console\InstallCommand::class, - ]); + $this->commands([Console\InstallCommand::class]); } } } diff --git a/src/Http/Controllers/DelegateAuthController.php b/src/Http/Controllers/DelegateAuthController.php index 64ca8b5..d26345b 100644 --- a/src/Http/Controllers/DelegateAuthController.php +++ b/src/Http/Controllers/DelegateAuthController.php @@ -49,16 +49,22 @@ class DelegateAuthController extends Controller private function decryptToken(string $token): ?array { - $data = base64_decode(urldecode($token)); - $payload = openssl_decrypt( - substr($data, 16), - "AES-256-CBC", - config("delegate.encrypt_key"), - 0, - substr($data, 0, 16), - ); - $data = json_decode($payload, associative: true); + $data = base64_decode($token, strict: true); - return is_array($data) ? $data : null; + if ($data === false || strlen($data) <= 16) { + return null; + } + + $key = substr(hash('sha256', config('delegate-auth.encrypt_key'), binary: true), 0, 32); + + $payload = openssl_decrypt(substr($data, 16), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, substr($data, 0, 16)); + + if ($payload === false) { + return null; + } + + $decoded = json_decode($payload, associative: true); + + return is_array($decoded) ? $decoded : null; } } diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index 68fab21..76dbb07 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -37,8 +37,8 @@ class LoginTest extends TestCase { $wrongKey = substr(hash('sha256', 'wrong-key', binary: true), 0, 32); $iv = random_bytes(16); - $encrypted = openssl_encrypt(json_encode(['email' => 'user@example.com']), 'AES-256-CBC', $wrongKey, 0, $iv); - $token = base64_encode($iv).'.'.$encrypted; + $encrypted = openssl_encrypt(json_encode(['email' => 'user@example.com']), 'AES-256-CBC', $wrongKey, OPENSSL_RAW_DATA, $iv); + $token = base64_encode($iv.$encrypted); User::create(['email' => 'user@example.com']); diff --git a/tests/TestCase.php b/tests/TestCase.php index b1a5211..0592afb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,8 +39,8 @@ abstract class TestCase extends OrchestraTestCase { $key = substr(hash('sha256', config('delegate-auth.encrypt_key'), binary: true), 0, 32); $iv = random_bytes(16); - $encrypted = openssl_encrypt(json_encode($payload), 'AES-256-CBC', $key, 0, $iv); + $encrypted = openssl_encrypt(json_encode($payload), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv); - return base64_encode($iv).'.'.$encrypted; + return base64_encode($iv.$encrypted); } }