From 58d00055d9f4a9abbcf498aac34ecf05457f2138 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 23 May 2022 06:33:59 +0000 Subject: [PATCH] refactor jwt secrets --- app/controllers/api/account.php | 2 +- app/controllers/api/functions.php | 2 +- app/init.php | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 571cd5d4ea..40999c7700 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1042,7 +1042,7 @@ App::post('/v1/account/jwt') throw new Exception('No valid session found', 404, Exception::USER_SESSION_NOT_FOUND); } - $jwt = new JWT($project->getId() == 'console' ? App::getEnv('_APP_OPENSSL_KEY_V1') : $project->getAttribute('jwtSecret'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. + $jwt = new JWT($project->getAttribute('jwtSecrets'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. $response->setStatusCode(Response::STATUS_CODE_CREATED); $response->dynamic(new Document(['jwt' => $jwt->encode([ diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index a97bf6d8b5..8d43f30ecd 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -928,7 +928,7 @@ App::post('/v1/functions/:functionId/executions') } if(!$current->isEmpty()) { - $jwtObj = new JWT($project->getId() == 'console' ? App::getEnv('_APP_OPENSSL_KEY_V1') : $project->getAttribute('jwtSecret'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. + $jwtObj = new JWT($project->getAttribute('jwtSecrets'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. $jwt = $jwtObj->encode([ 'userId' => $user->getId(), 'sessionId' => $current->getId(), diff --git a/app/init.php b/app/init.php index 35b117794d..00a5dbbddc 100644 --- a/app/init.php +++ b/app/init.php @@ -755,7 +755,7 @@ App::setResource('user', function($mode, $project, $console, $request, $response $authJWT = $request->getHeader('x-appwrite-jwt', ''); if (!empty($authJWT) && !$project->isEmpty()) { // JWT authentication - $jwt = new JWT($project->getId() == 'console' ? App::getEnv('_APP_OPENSSL_KEY_V1') : $project->getAttribute('jwtSecret'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. + $jwt = new JWT($project->getAttribute('jwtSecrets'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. try { $payload = $jwt->decode($authJWT); @@ -819,6 +819,7 @@ App::setResource('console', function() { 'legalAddress' => '', 'legalTaxId' => '', 'databaseSecrets' => ['v1' => App::getEnv('_APP_OPENSSL_KEY_V1')], + 'jwtSecrets' => App::getEnv('_APP_OPENSSL_KEY_V1'), 'auths' => [ 'limit' => (App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled') === 'enabled') ? 1 : 0, // limit signup to 1 user ],