Skip to content

Commit

Permalink
Fixed the Signature Body and wrote test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Jan 30, 2025
1 parent 963b16f commit b76ac90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Client/Credentials/Handler/SignatureBodyFormHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vonage\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestInterface;
use Vonage\Client\Credentials\CredentialsInterface;
use Vonage\Client\Credentials\SignatureSecret;
Expand All @@ -17,6 +18,7 @@ public function __invoke(RequestInterface $request, CredentialsInterface $creden
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();

$params = [];
parse_str($content, $params);
$params['api_key'] = $credentialsArray['api_key'];
Expand All @@ -28,9 +30,8 @@ public function __invoke(RequestInterface $request, CredentialsInterface $creden
);

$params = $signature->getSignedParams();
$body->rewind();
$body->write(http_build_query($params, '', '&'));

return $request;
$newBody = Utils::streamFor(http_build_query($params, '', '&'));
return $request->withBody($newBody);
}
}
40 changes: 40 additions & 0 deletions test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
use Vonage\Client\Credentials\SignatureSecret;
use VonageTest\VonageTestCase;
use Vonage\Client\Credentials\Handler\SignatureBodyFormHandler;

class SignatureBodyFormHandlerTest extends VonageTestCase
{
public function testSignatureBodyFormHandler()
{
$initialBody = http_build_query(['param1' => 'value1', 'param2' => 'value2']);

$request = new Request(
'POST',
'/test',
['Content-Type' => 'application/x-www-form-urlencoded'],
Utils::streamFor($initialBody)
);

$credentials = new SignatureSecret('secret', 'sha256');

$handler = new SignatureBodyFormHandler();
$authRequest = $handler($request, $credentials);

// Check the modified body
$authRequest->getBody()->rewind();
$newBody = $authRequest->getBody()->getContents();

parse_str($newBody, $params);

$this->assertArrayHasKey('api_key', $params);
$this->assertArrayHasKey('sig', $params); // assuming Signature adds 'sig'
}
}

0 comments on commit b76ac90

Please sign in to comment.