diff --git a/CHANGES.md b/CHANGES.md index 6d705a3bf9..8b01d0f480 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ - Force adding a security email on setup - SMTP is now disabled by default, no dummy SMTP is included in setup - Added a new endpoint that returns the server and SDKs latest versions numbers #941 +- Custom data strings, userId, and JWT available for cloud functions #967 ## Upgrades diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 15e591a60f..c2664f9dc3 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1,5 +1,7 @@ label('sdk.response.model', Response::MODEL_TAG) ->param('functionId', '', new UID(), 'Function unique ID.') ->param('command', '', new Text('1028'), 'Code execution command.') - ->param('code', null, new File(), 'Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. Use a path that is within the current directory.', false) + ->param('code', [], new File(), 'Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. Use a path that is within the current directory.', false) ->inject('request') ->inject('response') ->inject('projectDB') @@ -679,12 +681,13 @@ App::post('/v1/functions/:functionId/executions') ->label('abuse-limit', 60) ->label('abuse-time', 60) ->param('functionId', '', new UID(), 'Function unique ID.') + ->param('data', '', new Text(8192), 'String of custom data to send to function.', true) // ->param('async', 1, new Range(0, 1), 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true) ->inject('response') ->inject('project') ->inject('projectDB') ->inject('user') - ->action(function ($functionId, /*$async,*/ $response, $project, $projectDB, $user) { + ->action(function ($functionId, $data, /*$async,*/ $response, $project, $projectDB, $user) { /** @var Appwrite\Utopia\Response $response */ /** @var Appwrite\Database\Document $project */ /** @var Appwrite\Database\Database $projectDB */ @@ -739,12 +742,36 @@ App::post('/v1/functions/:functionId/executions') if (false === $execution) { throw new Exception('Failed saving execution to DB', 500); } - + + $jwt = ''; // initialize + if (!empty($user->getId())) { // If userId exists, generate a JWT for function + + $tokens = $user->getAttribute('tokens', []); + $session = new Document(); + + foreach ($tokens as $token) { /** @var Appwrite\Database\Document $token */ + if ($token->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too + $session = $token; + } + } + + if(!$session->isEmpty()) { + $jwtObj = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. + $jwt = $jwtObj->encode([ + 'userId' => $user->getId(), + 'sessionId' => $session->getId(), + ]); + } + } + Resque::enqueue('v1-functions', 'FunctionsV1', [ 'projectId' => $project->getId(), 'functionId' => $function->getId(), 'executionId' => $execution->getId(), 'trigger' => 'http', + 'data' => $data, + 'userId' => $user->getId(), + 'jwt' => $jwt, ]); $response diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 4ae65f491e..15ba114350 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -50,24 +50,9 @@ $usageStatsEnabled = $this->getParam('usageStatsEnabled',true);
- - +