From 8e9752cb2bd50456edf772c3a30c1788a1ae02d7 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 28 Mar 2023 15:00:46 +0200 Subject: [PATCH 1/3] Allow custom session attribute names --- src/Storageless/Http/SessionMiddleware.php | 4 ++- .../Http/SessionMiddlewareTest.php | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Storageless/Http/SessionMiddleware.php b/src/Storageless/Http/SessionMiddleware.php index 7b8fb2f9..e651064b 100644 --- a/src/Storageless/Http/SessionMiddleware.php +++ b/src/Storageless/Http/SessionMiddleware.php @@ -58,12 +58,14 @@ final class SessionMiddleware implements MiddlewareInterface private Configuration $config; private SetCookie $defaultCookie; + /** @param non-empty-string $sessionAttribute */ public function __construct( Configuration $configuration, SetCookie $defaultCookie, private int $idleTimeout, private Clock $clock, private int $refreshTime = self::DEFAULT_REFRESH_TIME, + private string $sessionAttribute = self::SESSION_ATTRIBUTE, ) { $this->config = $configuration; $this->defaultCookie = clone $defaultCookie; @@ -130,7 +132,7 @@ public function process(Request $request, RequestHandlerInterface $handler): Res return $this->appendToken( $sessionContainer, - $handler->handle($request->withAttribute(self::SESSION_ATTRIBUTE, $sessionContainer)), + $handler->handle($request->withAttribute($this->sessionAttribute, $sessionContainer)), $token, ); } diff --git a/test/StoragelessTest/Http/SessionMiddlewareTest.php b/test/StoragelessTest/Http/SessionMiddlewareTest.php index 2a3a4cc6..b16a0b04 100644 --- a/test/StoragelessTest/Http/SessionMiddlewareTest.php +++ b/test/StoragelessTest/Http/SessionMiddlewareTest.php @@ -735,6 +735,34 @@ public function testMutableCookieWillNotBeUsed(): void ); } + public function testAllowCustomRequestAttributeName(): void + { + $customAttributeName = uniqid('my_name_'); + self::assertNotEmpty($customAttributeName); + + $middleware = new SessionMiddleware( + Configuration::forSymmetricSigner( + new Sha256(), + self::makeRandomSymmetricKey(), + ), + SetCookie::create(SessionMiddleware::DEFAULT_COOKIE), + 100, + SystemClock::fromSystemTimezone(), + 100, + $customAttributeName, + ); + + $middleware->process( + new ServerRequest(), + $this->fakeDelegate(static function (ServerRequestInterface $request) use ($customAttributeName) { + self::assertInstanceOf(SessionInterface::class, $request->getAttribute($customAttributeName)); + self::assertNull($request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE)); + + return new Response(); + }), + ); + } + private function ensureSameResponse( SessionMiddleware $middleware, ServerRequestInterface $request, From a574070611b76e1279efdae479fef674d4d50439 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 28 Mar 2023 15:40:37 +0200 Subject: [PATCH 2/3] Remove randomness from test case --- test/StoragelessTest/Http/SessionMiddlewareTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/StoragelessTest/Http/SessionMiddlewareTest.php b/test/StoragelessTest/Http/SessionMiddlewareTest.php index b16a0b04..5dbb6abb 100644 --- a/test/StoragelessTest/Http/SessionMiddlewareTest.php +++ b/test/StoragelessTest/Http/SessionMiddlewareTest.php @@ -737,7 +737,7 @@ public function testMutableCookieWillNotBeUsed(): void public function testAllowCustomRequestAttributeName(): void { - $customAttributeName = uniqid('my_name_'); + $customAttributeName = 'my_custom_session_attribute_name'; self::assertNotEmpty($customAttributeName); $middleware = new SessionMiddleware( From aae8ed0205df5a66e1eb842052b009f60477ba4b Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 28 Mar 2023 15:55:01 +0200 Subject: [PATCH 3/3] Prefer `literal-string` over `non-empty-string` --- src/Storageless/Http/SessionMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storageless/Http/SessionMiddleware.php b/src/Storageless/Http/SessionMiddleware.php index e651064b..64e9972c 100644 --- a/src/Storageless/Http/SessionMiddleware.php +++ b/src/Storageless/Http/SessionMiddleware.php @@ -58,7 +58,7 @@ final class SessionMiddleware implements MiddlewareInterface private Configuration $config; private SetCookie $defaultCookie; - /** @param non-empty-string $sessionAttribute */ + /** @param literal-string $sessionAttribute */ public function __construct( Configuration $configuration, SetCookie $defaultCookie,