From bbda4217c7ad4c8a22e518e52714752bec19861c Mon Sep 17 00:00:00 2001 From: Leonardo Ralph Date: Thu, 26 Sep 2024 17:20:15 -0300 Subject: [PATCH 1/5] Add cookie key name customization and documentation --- config/config.php | 11 +++++++++++ docs/quick-start.md | 14 ++++++++++++++ src/Http/Parser/Cookies.php | 6 +++++- src/Providers/LaravelServiceProvider.php | 9 +++++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/config/config.php b/config/config.php index 8f7bec2a..865c028a 100644 --- a/config/config.php +++ b/config/config.php @@ -254,6 +254,17 @@ 'decrypt_cookies' => false, + /* + |-------------------------------------------------------------------------- + | Cookie key name + |-------------------------------------------------------------------------- + | + | Specify the cookie key name that you would like to use for the cookie token. + | + */ + + 'cookie_key_name' => 'token', + /* |-------------------------------------------------------------------------- | Providers diff --git a/docs/quick-start.md b/docs/quick-start.md index c1843746..340e0d14 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -221,4 +221,18 @@ There are a number of ways to send the token via http: **Cookies** +By default, you can send a token via a cookie. You can customize the cookie name within the `jwt.cookie_name` configuration. + +```php +// config/jwt.php + +return [ + // ... + 'cookie_name' => 'token', + // ... +]; +``` + +When making requests, the token will be automatically read from the specified cookie, the default key name is `token`. + **Laravel route parameter** diff --git a/src/Http/Parser/Cookies.php b/src/Http/Parser/Cookies.php index 91b8ccab..298a0acd 100644 --- a/src/Http/Parser/Cookies.php +++ b/src/Http/Parser/Cookies.php @@ -29,9 +29,13 @@ class Cookies implements ParserContract */ private $decrypt; - public function __construct($decrypt = true) + public function __construct(bool $decrypt = true, ?string $keyName = null) { $this->decrypt = $decrypt; + + if ($keyName) { + $this->setKey($keyName); + } } /** diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index ccf73166..3fb99560 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -25,7 +25,7 @@ class LaravelServiceProvider extends AbstractServiceProvider { public function boot() { - $path = realpath(__DIR__.'/../../config/config.php'); + $path = realpath(__DIR__ . '/../../config/config.php'); $this->publishes([$path => $this->app->configPath('jwt.php')], 'config'); $this->mergeConfigFrom($path, 'jwt'); @@ -34,9 +34,14 @@ public function boot() $this->extendAuthGuard(); + $config = $this->app->make('config'); + $this->app['tymon.jwt.parser']->addParser([ new RouteParams(), - new Cookies($this->app->make('config')->get('jwt.decrypt_cookies')), + new Cookies( + $config->get('jwt.decrypt_cookies'), + $config->get('jwt.cookie_key_name'), + ), ]); if (isset($_SERVER['LARAVEL_OCTANE'])) { From 365d6267e34905f0336bc316a8426c020cc29d81 Mon Sep 17 00:00:00 2001 From: Leonardo Ralph Date: Thu, 26 Sep 2024 17:52:36 -0300 Subject: [PATCH 2/5] Add test --- src/Providers/LaravelServiceProvider.php | 5 ++--- tests/DefaultConfigValuesTest.php | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index 3fb99560..bd3544cb 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -38,10 +38,9 @@ public function boot() $this->app['tymon.jwt.parser']->addParser([ new RouteParams(), - new Cookies( + (new Cookies( $config->get('jwt.decrypt_cookies'), - $config->get('jwt.cookie_key_name'), - ), + ))->setKey($config->get('jwt.cookie_key_name')), ]); if (isset($_SERVER['LARAVEL_OCTANE'])) { diff --git a/tests/DefaultConfigValuesTest.php b/tests/DefaultConfigValuesTest.php index 92c2476c..40dd94ae 100644 --- a/tests/DefaultConfigValuesTest.php +++ b/tests/DefaultConfigValuesTest.php @@ -109,4 +109,9 @@ public function testProvidersShouldBeSet() $this->assertEquals(AuthIlluminate::class, $this->configuration['providers']['auth']); $this->assertEquals(StorageIlluminate::class, $this->configuration['providers']['storage']); } + + public function testCookieKeyNameShouldBeSet() + { + $this->assertEquals('token', $this->configuration['cookie_key_name']); + } } From 3fc2f339419be71619d0ea1d411e1186cf86a9c1 Mon Sep 17 00:00:00 2001 From: Leonardo Ralph Date: Thu, 26 Sep 2024 17:54:21 -0300 Subject: [PATCH 3/5] Cs fixing --- src/Providers/JWT/Lcobucci.php | 2 +- src/Providers/LaravelServiceProvider.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Providers/JWT/Lcobucci.php b/src/Providers/JWT/Lcobucci.php index 4d9b6439..975f1b82 100644 --- a/src/Providers/JWT/Lcobucci.php +++ b/src/Providers/JWT/Lcobucci.php @@ -71,7 +71,7 @@ public function __construct( $secret, $algo, array $keys, - $config = null + $config = null, ) { parent::__construct($secret, $algo, $keys); $this->generateConfig($config); diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index bd3544cb..6ac429b4 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -25,7 +25,7 @@ class LaravelServiceProvider extends AbstractServiceProvider { public function boot() { - $path = realpath(__DIR__ . '/../../config/config.php'); + $path = realpath(__DIR__.'/../../config/config.php'); $this->publishes([$path => $this->app->configPath('jwt.php')], 'config'); $this->mergeConfigFrom($path, 'jwt'); From a7c809cef0b46c08c3f2576f40d0a1272512e7eb Mon Sep 17 00:00:00 2001 From: Leonardo Ralph Date: Thu, 26 Sep 2024 17:57:49 -0300 Subject: [PATCH 4/5] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8276a763..9535882a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ You can find and compare releases at the GitHub release page. ### Added - Fixes #259 - Can't logout with an expired token +- Add `cookie_key_name` config to customize cookie name for authentication ### Removed From c72633ab42cefd78a78562e520c0098f216c3afc Mon Sep 17 00:00:00 2001 From: Leonardo Ralph Date: Fri, 27 Sep 2024 13:07:56 -0300 Subject: [PATCH 5/5] Applying requested changes --- src/Http/Parser/Cookies.php | 6 +----- src/Providers/LaravelServiceProvider.php | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Http/Parser/Cookies.php b/src/Http/Parser/Cookies.php index 298a0acd..91b8ccab 100644 --- a/src/Http/Parser/Cookies.php +++ b/src/Http/Parser/Cookies.php @@ -29,13 +29,9 @@ class Cookies implements ParserContract */ private $decrypt; - public function __construct(bool $decrypt = true, ?string $keyName = null) + public function __construct($decrypt = true) { $this->decrypt = $decrypt; - - if ($keyName) { - $this->setKey($keyName); - } } /** diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index 6ac429b4..7339a70a 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -40,7 +40,7 @@ public function boot() new RouteParams(), (new Cookies( $config->get('jwt.decrypt_cookies'), - ))->setKey($config->get('jwt.cookie_key_name')), + ))->setKey($config->get('jwt.cookie_key_name', 'token')), ]); if (isset($_SERVER['LARAVEL_OCTANE'])) {