From d4a5891c0dc9853cc460d4547f502a287900f26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 21 Jul 2024 13:28:23 +0000 Subject: [PATCH] Fix user JWTs without sessionID --- app/init.php | 11 +++--- tests/e2e/Services/Users/UsersBase.php | 47 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/app/init.php b/app/init.php index ef7742956f..c6a0680a9d 100644 --- a/app/init.php +++ b/app/init.php @@ -1242,14 +1242,15 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons } $jwtUserId = $payload['userId'] ?? ''; - $jwtSessionId = $payload['sessionId'] ?? ''; - - if ($jwtUserId && $jwtSessionId) { + if (!empty($jwtUserId)) { $user = $dbForProject->getDocument('users', $jwtUserId); } - if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token - $user = new Document([]); + $jwtSessionId = $payload['sessionId'] ?? ''; + if(!empty($jwtSessionId)) { + if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token + $user = new Document([]); + } } } diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index c34227e4de..c06bc6f4a8 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -1589,6 +1589,27 @@ trait UsersBase ], false); $this->assertEquals($user['headers']['status-code'], 201); + // Create JWT 0, with no session available + $response = $this->client->call(Client::METHOD_POST, '/users/' . $userId . '/jwts', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['jwt']); + $jwt0 = $response['body']['jwt']; + + // Ensure JWT 0 works + $response = $this->client->call(Client::METHOD_GET, '/account', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt0, + ])); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($userId, $response['body']['$id']); + // Create two sessions $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([ 'origin' => 'http://localhost', @@ -1641,12 +1662,13 @@ trait UsersBase $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals($userId, $response['body']['$id']); - // Create JWT 2 for latest session using default param + // Create JWT 2 for latest session using 'current' param $response = $this->client->call(Client::METHOD_POST, '/users/' . $userId . '/jwts', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'duration' => 5 + 'duration' => 5, + 'sessionId' => 'current' ]); $this->assertEquals(201, $response['headers']['status-code']); @@ -1696,6 +1718,27 @@ trait UsersBase $this->assertEquals(401, $response['headers']['status-code']); + // Ensure JWT 0 works still even with no sessions + + $response = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId . '/sessions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'sessionId' => $session2Id + ]); + + $this->assertEquals(204, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/account', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt0, + ])); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($userId, $response['body']['$id']); + // Cleanup after test $response = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, array_merge([