Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 66 additions & 48 deletions app/config/specs/open-api3-latest-client.json

Large diffs are not rendered by default.

609 changes: 314 additions & 295 deletions app/config/specs/open-api3-latest-console.json

Large diffs are not rendered by default.

493 changes: 255 additions & 238 deletions app/config/specs/open-api3-latest-server.json

Large diffs are not rendered by default.

113 changes: 66 additions & 47 deletions app/config/specs/swagger2-latest-client.json

Large diffs are not rendered by default.

608 changes: 314 additions & 294 deletions app/config/specs/swagger2-latest-console.json

Large diffs are not rendered by default.

492 changes: 255 additions & 237 deletions app/config/specs/swagger2-latest-server.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions app/controllers/api/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use Utopia\Validator\ArrayList;
use Utopia\Validator\Assoc;
use Utopia\Validator\Boolean;
use Utopia\Validator\Range;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;

Expand Down Expand Up @@ -2953,20 +2954,21 @@ function sendSessionAlert(Locale $locale, Document $user, Document $project, arr
],
contentType: ContentType::JSON,
))
->param('duration', 900, new Range(0, 3600), 'Time in seconds before JWT expires. Default duration is 900 seconds, and maximum is 3600 seconds.', true)
Comment thread
TorstenDittmann marked this conversation as resolved.
->label('abuse-limit', 100)
->label('abuse-key', 'url:{url},userId:{userId}')
->inject('response')
->inject('user')
->inject('store')
->inject('proofForToken')
->action(function (Response $response, User $user, Store $store, ProofsToken $proofForToken) {
->action(function (int $duration, Response $response, User $user, Store $store, ProofsToken $proofForToken) {
$sessionId = $user->sessionVerify($store->getProperty('secret', ''), $proofForToken);

if (!$sessionId) {
throw new Exception(Exception::USER_SESSION_NOT_FOUND);
}

$jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 0);
$jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $duration, 0);

$response
->setStatusCode(Response::STATUS_CODE_CREATED)
Expand Down
51 changes: 51 additions & 0 deletions tests/e2e/Services/Account/AccountCustomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,57 @@ public function testCreateJWT(): array

$this->assertEquals(401, $response['headers']['status-code']);

// Test JWT with custom duration
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'email' => $email,
'password' => $password,
]);

$this->assertEquals(201, $response['headers']['status-code']);

$session = $response['cookies']['a_session_' . $this->getProject()['$id']];

$response = $this->client->call(Client::METHOD_POST, '/account/jwt', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [
'duration' => 5
]);

$this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['jwt']);

$jwt = $response['body']['jwt'];

// Ensure JWT works before expiration
$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' => $jwt,
]));

$this->assertEquals(200, $response['headers']['status-code']);

// Wait for JWT to expire
\sleep(6);

// Ensure JWT no longer works after expiration
$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' => $jwt,
]));

$this->assertEquals(401, $response['headers']['status-code']);

return [];
}

Expand Down
Loading