Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Auth/JWTGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Loading