mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13647 from appwrite/codex/fix-7247-function-jwt
fix(functions): forward authenticated JWT to executions
This commit is contained in:
3 files changed
+60
No files matched your search
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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([
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
Reference in new issue
Block a user