mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13125 from appwrite/fix-8000-jwt-session-expiry
fix(auth): reject JWT when linked session is expired
This commit is contained in:
4 files changed
+56
-5
No files matched your search
@@ -288,14 +288,16 @@ return function (Container $container): void {
|
||||
$jwtUserId = $payload['userId'] ?? '';
|
||||
if (!empty($jwtUserId)) {
|
||||
if ($mode === APP_MODE_ADMIN) {
|
||||
/** @var User $user */
|
||||
$user = $dbForPlatform->getDocument('users', $jwtUserId);
|
||||
} else {
|
||||
/** @var User $user */
|
||||
$user = $dbForProject->getDocument('users', $jwtUserId);
|
||||
}
|
||||
}
|
||||
|
||||
$jwtSessionId = $payload['sessionId'] ?? '';
|
||||
if (!empty($jwtSessionId) && empty($user->find('$id', $jwtSessionId, 'sessions'))) {
|
||||
if (!empty($jwtSessionId) && !$user->sessionActive($jwtSessionId)) {
|
||||
$user = new User([]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,16 +536,16 @@ return function (Container $context): void {
|
||||
$jwtUserId = $payload['userId'] ?? '';
|
||||
if (! empty($jwtUserId)) {
|
||||
if ($mode === APP_MODE_ADMIN) {
|
||||
/** @var User $user */
|
||||
$user = $dbForPlatform->getDocument('users', $jwtUserId);
|
||||
} else {
|
||||
/** @var User $user */
|
||||
$user = $dbForProject->getDocument('users', $jwtUserId);
|
||||
}
|
||||
}
|
||||
$jwtSessionId = $payload['sessionId'] ?? '';
|
||||
if (! empty($jwtSessionId)) {
|
||||
if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token
|
||||
$user = new User([]);
|
||||
}
|
||||
if (! empty($jwtSessionId) && ! $user->sessionActive($jwtSessionId)) { // Match JWT to active session
|
||||
$user = new User([]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -174,4 +174,22 @@ class User extends Document
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a session exists on the user and has not expired.
|
||||
*
|
||||
* Used by JWT authentication, which binds to a session ID rather than a
|
||||
* session secret.
|
||||
*/
|
||||
public function sessionActive(string $sessionId): bool
|
||||
{
|
||||
$session = $this->find('$id', $sessionId, 'sessions');
|
||||
|
||||
if (empty($session)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $session->isSet('expire')
|
||||
&& DateTime::formatTz(DateTime::format(new \DateTime($session->getAttribute('expire')))) >= DateTime::formatTz(DateTime::now());
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,37 @@ final class UserTest extends TestCase
|
||||
$this->assertEquals(false, $user2->sessionVerify('false-secret', $proofForToken));
|
||||
}
|
||||
|
||||
public function testSessionActive(): void
|
||||
{
|
||||
$user = new User([
|
||||
'$id' => ID::custom('user1'),
|
||||
'sessions' => [
|
||||
new Document([
|
||||
'$id' => ID::custom('active'),
|
||||
'secret' => 'secret',
|
||||
'provider' => SESSION_PROVIDER_EMAIL,
|
||||
'expire' => DateTime::addSeconds(new \DateTime(), 60 * 60),
|
||||
]),
|
||||
new Document([
|
||||
'$id' => ID::custom('expired'),
|
||||
'secret' => 'secret',
|
||||
'provider' => SESSION_PROVIDER_EMAIL,
|
||||
'expire' => DateTime::addSeconds(new \DateTime(), -60),
|
||||
]),
|
||||
new Document([
|
||||
'$id' => ID::custom('missing-expire'),
|
||||
'secret' => 'secret',
|
||||
'provider' => SESSION_PROVIDER_EMAIL,
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertTrue($user->sessionActive('active'));
|
||||
$this->assertFalse($user->sessionActive('expired'));
|
||||
$this->assertFalse($user->sessionActive('missing-expire'));
|
||||
$this->assertFalse($user->sessionActive('missing'));
|
||||
}
|
||||
|
||||
public function testTokenVerify(): void
|
||||
{
|
||||
$proofForToken = new Token();
|
||||
|
||||
Reference in new issue
Block a user