Merge pull request #1421 from appwrite/fix-jwt-for-functions
Fixed JWT + Functions bug
This commit is contained in:
commit
060e50f644
5 changed files with 70 additions and 19 deletions
|
@ -748,20 +748,20 @@ App::post('/v1/functions/:functionId/executions')
|
||||||
$jwt = ''; // initialize
|
$jwt = ''; // initialize
|
||||||
if (!empty($user->getId())) { // If userId exists, generate a JWT for function
|
if (!empty($user->getId())) { // If userId exists, generate a JWT for function
|
||||||
|
|
||||||
$tokens = $user->getAttribute('tokens', []);
|
$sessions = $user->getAttribute('sessions', []);
|
||||||
$session = new Document();
|
$current = new Document();
|
||||||
|
|
||||||
foreach ($tokens as $token) { /** @var Appwrite\Database\Document $token */
|
foreach ($sessions as $session) { /** @var Appwrite\Database\Document $session */
|
||||||
if ($token->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too
|
if ($session->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too
|
||||||
$session = $token;
|
$current = $session;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$session->isEmpty()) {
|
if(!$current->isEmpty()) {
|
||||||
$jwtObj = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway.
|
$jwtObj = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway.
|
||||||
$jwt = $jwtObj->encode([
|
$jwt = $jwtObj->encode([
|
||||||
'userId' => $user->getId(),
|
'userId' => $user->getId(),
|
||||||
'sessionId' => $session->getId(),
|
'sessionId' => $current->getId(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,7 +185,32 @@ class FunctionsCustomClientTest extends Scope
|
||||||
|
|
||||||
$this->assertEquals(201, $execution['headers']['status-code']);
|
$this->assertEquals(201, $execution['headers']['status-code']);
|
||||||
|
|
||||||
|
$executionId = $execution['body']['$id'] ?? '';
|
||||||
|
|
||||||
sleep(10);
|
sleep(10);
|
||||||
|
|
||||||
|
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, [
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $projectId,
|
||||||
|
'x-appwrite-key' => $apikey,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$output = json_decode($executions['body']['stdout'], true);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $executions['headers']['status-code']);
|
||||||
|
$this->assertEquals('completed', $executions['body']['status']);
|
||||||
|
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
|
||||||
|
$this->assertEquals('Test', $output['APPWRITE_FUNCTION_NAME']);
|
||||||
|
$this->assertEquals($tagId, $output['APPWRITE_FUNCTION_TAG']);
|
||||||
|
$this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']);
|
||||||
|
$this->assertEquals('PHP', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
|
||||||
|
$this->assertEquals('8.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
|
||||||
|
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
|
||||||
|
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']);
|
||||||
|
$this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']);
|
||||||
|
$this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']);
|
||||||
|
$this->assertNotEmpty($output['APPWRITE_FUNCTION_JWT']);
|
||||||
|
|
||||||
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', [
|
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', [
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $projectId,
|
'x-appwrite-project' => $projectId,
|
||||||
|
|
|
@ -582,8 +582,31 @@ class FunctionsCustomServerTest extends Scope
|
||||||
|
|
||||||
$this->assertEquals(201, $execution['headers']['status-code']);
|
$this->assertEquals(201, $execution['headers']['status-code']);
|
||||||
|
|
||||||
|
$executionId = $execution['body']['$id'] ?? '';
|
||||||
|
|
||||||
sleep(10);
|
sleep(10);
|
||||||
|
|
||||||
|
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()));
|
||||||
|
|
||||||
|
$output = json_decode($executions['body']['stdout'], true);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $executions['headers']['status-code']);
|
||||||
|
$this->assertEquals('completed', $executions['body']['status']);
|
||||||
|
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
|
||||||
|
$this->assertEquals('Test '.$name, $output['APPWRITE_FUNCTION_NAME']);
|
||||||
|
$this->assertEquals($tagId, $output['APPWRITE_FUNCTION_TAG']);
|
||||||
|
$this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']);
|
||||||
|
$this->assertEquals('PHP', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
|
||||||
|
$this->assertEquals('8.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
|
||||||
|
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
|
||||||
|
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']);
|
||||||
|
$this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']);
|
||||||
|
$this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']);
|
||||||
|
$this->assertEmpty($output['APPWRITE_FUNCTION_JWT']);
|
||||||
|
|
||||||
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([
|
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
|
Binary file not shown.
|
@ -17,15 +17,18 @@ use Appwrite\Services\Storage;
|
||||||
|
|
||||||
// $result = $storage->getFile($_ENV['APPWRITE_FILEID']);
|
// $result = $storage->getFile($_ENV['APPWRITE_FILEID']);
|
||||||
|
|
||||||
echo $_ENV['APPWRITE_FUNCTION_ID']."\n";
|
$output = [
|
||||||
echo $_ENV['APPWRITE_FUNCTION_NAME']."\n";
|
'APPWRITE_FUNCTION_ID' => $_ENV['APPWRITE_FUNCTION_ID'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_TAG']."\n";
|
'APPWRITE_FUNCTION_NAME' => $_ENV['APPWRITE_FUNCTION_NAME'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_TRIGGER']."\n";
|
'APPWRITE_FUNCTION_TAG' => $_ENV['APPWRITE_FUNCTION_TAG'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_RUNTIME_NAME']."\n";
|
'APPWRITE_FUNCTION_TRIGGER' => $_ENV['APPWRITE_FUNCTION_TRIGGER'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_RUNTIME_VERSION']."\n";
|
'APPWRITE_FUNCTION_RUNTIME_NAME' => $_ENV['APPWRITE_FUNCTION_RUNTIME_NAME'],
|
||||||
// echo $result['$id'];
|
'APPWRITE_FUNCTION_RUNTIME_VERSION' => $_ENV['APPWRITE_FUNCTION_RUNTIME_VERSION'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_EVENT']."\n";
|
'APPWRITE_FUNCTION_EVENT' => $_ENV['APPWRITE_FUNCTION_EVENT'],
|
||||||
echo $_ENV['APPWRITE_FUNCTION_EVENT_DATA']."\n";
|
'APPWRITE_FUNCTION_EVENT_DATA' => $_ENV['APPWRITE_FUNCTION_EVENT_DATA'],
|
||||||
echo 'data:'.$_ENV['APPWRITE_FUNCTION_DATA']."\n";
|
'APPWRITE_FUNCTION_DATA' => $_ENV['APPWRITE_FUNCTION_DATA'],
|
||||||
echo 'userId:'.$_ENV['APPWRITE_FUNCTION_USER_ID']."\n";
|
'APPWRITE_FUNCTION_USER_ID' => $_ENV['APPWRITE_FUNCTION_USER_ID'],
|
||||||
echo 'jwt:'.$_ENV['APPWRITE_FUNCTION_JWT']."\n";
|
'APPWRITE_FUNCTION_JWT' => $_ENV['APPWRITE_FUNCTION_JWT'],
|
||||||
|
];
|
||||||
|
|
||||||
|
echo json_encode($output);
|
||||||
|
|
Loading…
Reference in a new issue