From 2b6d6802ed2d35c5aeae2c35b6e13a8ba6a5ff96 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Fri, 4 Dec 2026 15:52:31 +0100 Subject: [PATCH] fix: send channel uploads as multipart and sign tokens with short API secrets uploadChannelFile and uploadChannelImage passed UploadChannelFileRequest and UploadChannelRequest, which makeRequest sent as JSON. php-jwt 7 refuses an HMAC key shorter than the hash, so the generator signs HS256/384/512 tokens itself. --- src/Auth/JWTGenerator.php | 18 ++++++++++++++++-- src/Client.php | 4 ++-- tests/ClientTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Auth/JWTGenerator.php b/src/Auth/JWTGenerator.php index 9a0c1c3d..af8c3ce8 100644 --- a/src/Auth/JWTGenerator.php +++ b/src/Auth/JWTGenerator.php @@ -53,7 +53,7 @@ public function generateServerToken(array $claims = [], ?int $expiration = null) $payload['exp'] = $now + $expiration; } - return JWT::encode($payload, $this->secret, $this->algorithm); + return $this->encode($payload); } /** @@ -82,7 +82,21 @@ public function generateUserToken(string $userId, array $claims = [], ?int $expi $payload['exp'] = $now + $expiration; } - return JWT::encode($payload, $this->secret, $this->algorithm); + return $this->encode($payload); + } + + private function encode(array $payload): string + { + $hash = ['HS256' => 'sha256', 'HS384' => 'sha384', 'HS512' => 'sha512'][$this->algorithm] ?? null; + if ($hash === null) { + return JWT::encode($payload, $this->secret, $this->algorithm); + } + + // Signed by hand: php-jwt 7 refuses an HMAC key shorter than the hash, and the API accepts any app secret. + $unsigned = JWT::urlsafeB64Encode(JWT::jsonEncode(['typ' => 'JWT', 'alg' => $this->algorithm])) + . '.' . JWT::urlsafeB64Encode(JWT::jsonEncode($payload)); + + return $unsigned . '.' . JWT::urlsafeB64Encode(hash_hmac($hash, $unsigned, $this->secret, true)); } /** diff --git a/src/Client.php b/src/Client.php index 29e392be..623e02c0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -218,11 +218,11 @@ public function makeRequest( ]); // Check if this is an upload request (needs multipart/form-data) - like Go SDK - if ($body instanceof GeneratedModels\ImageUploadRequest || $body instanceof GeneratedModels\FileUploadRequest) { + $isImage = $body instanceof GeneratedModels\ImageUploadRequest || $body instanceof GeneratedModels\UploadChannelRequest; + if ($isImage || $body instanceof GeneratedModels\FileUploadRequest || $body instanceof GeneratedModels\UploadChannelFileRequest) { unset($headers['Content-Type']); // Let Guzzle set multipart Content-Type $multipart = []; - $isImage = $body instanceof GeneratedModels\ImageUploadRequest; // File field if ($body->file !== null) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 24cc7e90..5fb12a35 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -4,11 +4,15 @@ namespace GetStream\Tests; +use Firebase\JWT\JWT; use GetStream\Auth\JWTGenerator; +use GetStream\ChatClient; use GetStream\Client; use GetStream\ClientBuilder; use GetStream\Exceptions\StreamException; use GetStream\Feed; +use GetStream\GeneratedModels\UploadChannelFileRequest; +use GetStream\GeneratedModels\UploadChannelRequest; use GetStream\Http\HttpClientInterface; use GetStream\StreamResponse; use PHPUnit\Framework\MockObject\MockObject; @@ -175,6 +179,35 @@ public function listQueryParamsAreSentCommaSeparated(): void $client->makeRequest('GET', '/api/v2/hooks/events', ['product' => ['chat', 'video'], 'limit' => 10]); } + /** + * @test + */ + public function channelUploadsAreSentAsMultipart(): void + { + $client = new ChatClient('key', 'secret', 'https://example.com', $this->mockHttpClient); + $file = base64_encode('hello'); + + $this->mockHttpClient->expects(self::exactly(2)) + ->method('request') + ->with('POST', self::anything(), self::callback(fn (array $headers) => !isset($headers['Content-Type'])), self::callback(fn ($body) => is_array($body) && $body[0]['name'] === 'file' && $body[0]['contents'] === 'hello')) + ->willReturn(new StreamResponse(200, [], [])); + + $client->uploadChannelFile('messaging', 'general', new UploadChannelFileRequest(file: $file)); + $client->uploadChannelImage('messaging', 'general', new UploadChannelRequest(file: $file)); + } + + /** + * @test + */ + public function shortSecretSignsTokens(): void + { + $token = (new JWTGenerator('short'))->generateUserToken('john'); + [$header, $payload, $signature] = explode('.', $token); + + self::assertSame(JWT::urlsafeB64Encode(hash_hmac('sha256', $header . '.' . $payload, 'short', true)), $signature); + self::assertSame('john', JWT::jsonDecode(JWT::urlsafeB64Decode($payload))->user_id); + } + /** * @test */