Improve code quality of folder decoding project ID

This commit is contained in:
Matej Bačo
2026-04-28 17:10:58 +02:00
parent 15917ac7ba
commit c96836b1c0
+23 -14
View File
@@ -2,8 +2,11 @@
namespace Appwrite\Utopia\Response\Filters;
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filter;
use Utopia\System\System;
// Convert 1.9.3 Data format to 1.9.2 format
class V24 extends Filter
@@ -26,22 +29,28 @@ class V24 extends Filter
unset($content['sdks']);
unset($content['accessedAt']);
$projectId = '';
if (isset($content['secret'])) {
$parts = explode('_', $content['secret'], 2);
if (count($parts) === 2) {
$jwtParts = explode('.', $parts[1]);
if (count($jwtParts) >= 2) {
$payload = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $jwtParts[1])), true);
$projectId = $payload['projectId'] ?? '';
}
}
}
$content['projectId'] = $projectId;
$content['jwt'] = $content['secret'] ?? '';
$secret = $content['secret'] ?? '';
unset($content['secret']);
$content['projectId'] = $this->extractProjectId($secret);
$content['jwt'] = $secret;
return $content;
}
private function extractProjectId(string $secret): string
{
$token = explode('_', $secret, 2)[1] ?? '';
if ($token === '') {
return '';
}
$jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256');
try {
return $jwt->decode($token, false)['projectId'] ?? '';
} catch (JWTException) {
return '';
}
}
}