diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 6328792e5b..b0361eb495 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -229,6 +229,9 @@ class Create extends Base 'userId' => $user->getId(), 'sessionId' => $current->getId(), ]); + } else { + // A JWT cannot be used to create another JWT, so forward the caller's token as-is + $jwt = $request->getHeaderLine('x-appwrite-jwt'); } } diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index a4b459bd9f..be1e7f35c1 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -279,6 +279,62 @@ final class FunctionsCustomClientTest extends Scope $this->cleanupFunction($functionId); } + public function testCreateExecutionWithJWT() + { + /** + * Test for SUCCESS + */ + $functionId = $this->setupFunction([ + 'functionId' => ID::unique(), + 'name' => 'Test JWT execution', + 'execute' => [Role::user($this->getUser()['$id'])->toString()], + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'timeout' => 10, + ]); + $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('basic'), + 'activate' => true + ]); + + $token = $this->client->call(Client::METHOD_POST, '/account/jwts', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $jwt = $token['body']['jwt']; + + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ]); + $output = json_decode($execution['body']['responseBody'], true); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals($jwt, $output['APPWRITE_FUNCTION_JWT']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-jwt' => $jwt, + ], [ + 'async' => true, + ]); + $this->assertEquals(202, $execution['headers']['status-code']); + $executionId = $execution['body']['$id']; + + // Async executions do not store the response body, so read the forwarded JWT from the logs + $this->assertEventually(function () use ($functionId, $executionId, $jwt) { + $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $executionId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertStringContainsString('user-jwt-is-' . $jwt, (string) $execution['body']['logs']); + }, 60000, 500); + + $this->cleanupFunction($functionId); + } + public function testNonOverrideOfHeaders() { $functionId = $this->setupFunction([ diff --git a/tests/resources/functions/basic/index.js b/tests/resources/functions/basic/index.js index 47f7100b5b..85f222430a 100644 --- a/tests/resources/functions/basic/index.js +++ b/tests/resources/functions/basic/index.js @@ -19,6 +19,7 @@ module.exports = async(context) => { context.log('method-is-' + (context.req.method ?? '').toLowerCase()); context.log('path-is-' + (context.req.path ?? '')); context.log('user-is-' + (context.req.headers['x-appwrite-user-id'] ?? '')); + context.log('user-jwt-is-' + (context.req.headers['x-appwrite-user-jwt'] ?? '')); const statusCode = context.req.query['code'] || '200';