From 2391415969b940f6e83908137a81897ead2dc009 Mon Sep 17 00:00:00 2001 From: Xu Date: Fri, 21 Oct 2022 18:16:49 +0200 Subject: [PATCH] make session lifetime configurable --- config/config.default.php | 3 +++ src/Http/SessionServiceProvider.php | 1 + tests/Unit/Http/SessionServiceProviderTest.php | 14 ++++++++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/config/config.default.php b/config/config.default.php index decbe847..ef2c4ee7 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -345,6 +345,9 @@ return [ // Cookie name 'name' => env('SESSION_NAME', 'session'), + + // Lifetime in days + 'lifetime' => env('SESSION_LIFETIME', 30) ], // IP addresses of reverse proxies that are trusted, can be an array or a comma separated list diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php index d187b439..97e8c9b6 100644 --- a/src/Http/SessionServiceProvider.php +++ b/src/Http/SessionServiceProvider.php @@ -62,6 +62,7 @@ class SessionServiceProvider extends ServiceProvider 'options' => [ 'cookie_httponly' => true, 'name' => $sessionConfig['name'], + 'cookie_lifetime' => (int)($sessionConfig['lifetime'] * 24 * 60 * 60), ], 'handler' => $handler, ]); diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php index 84dfd9d1..31d196c8 100644 --- a/tests/Unit/Http/SessionServiceProviderTest.php +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -53,13 +53,19 @@ class SessionServiceProviderTest extends ServiceProviderTest [Session::class], [ NativeSessionStorage::class, - ['options' => ['cookie_httponly' => true, 'name' => 'session'], 'handler' => null], + [ + 'options' => ['cookie_httponly' => true, 'name' => 'session', 'cookie_lifetime' => 172800], + 'handler' => null + ], ], [Session::class], [DatabaseHandler::class], [ NativeSessionStorage::class, - ['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $databaseHandler], + [ + 'options' => ['cookie_httponly' => true, 'name' => 'foobar', 'cookie_lifetime' => 432000], + 'handler' => $databaseHandler + ], ], [Session::class] ) @@ -101,8 +107,8 @@ class SessionServiceProviderTest extends ServiceProviderTest ->method('get') ->with('session') ->willReturnOnConsecutiveCalls( - ['driver' => 'native', 'name' => 'session'], - ['driver' => 'pdo', 'name' => 'foobar'] + ['driver' => 'native', 'name' => 'session', 'lifetime' => 2], + ['driver' => 'pdo', 'name' => 'foobar', 'lifetime' => 5] ); $app->expects($this->atLeastOnce())