Skip to content

Commit

Permalink
Token body never worked in the first place, mostly due to the body no…
Browse files Browse the repository at this point in the history
…t being added before the auth handler has access to it (#394)
  • Loading branch information
SecondeJK authored Apr 3, 2023
1 parent a9d0756 commit b9d3b25
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Client/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ public function create(array $body, string $uri = '', array $headers = []): ?arr
$headers
);

$request->getBody()->write(json_encode($body));

if ($this->getAuthHandler()) {
$request = $this->addAuth($request);
}

$request->getBody()->write(json_encode($body));
$this->lastRequest = $request;

$response = $this->getClient()->send($request);
Expand Down
20 changes: 9 additions & 11 deletions src/Client/Credentials/Handler/TokenBodyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ class TokenBodyHandler extends AbstractHandler
public function __invoke(RequestInterface $request, CredentialsInterface $credentials): RequestInterface
{
$credentials = $this->extract(Basic::class, $credentials);
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();
$params = json_decode($content, true);

if (!$params) {
$params = [];
}
// We have to do some clunky body pointer rewinding here
$existingBody = $request->getBody();
$existingBody->rewind();
$existingBodyContent = $existingBody->getContents();
$existingBody->rewind();
$existingBodyArray = json_decode($existingBodyContent, true);

$params = array_merge($params, $credentials->asArray());
$body->rewind();
$body->write(json_encode($params));
// The request body will now be the existing body plus the basic creds
$mergedBodyArray = array_merge($existingBodyArray, $credentials->asArray());

return $request;
return $request->withBody(\GuzzleHttp\Psr7\Utils::streamFor(json_encode($mergedBodyArray)));
}
}
3 changes: 2 additions & 1 deletion src/Verify/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\BasicHandler;
use Vonage\Client\Credentials\Handler\KeypairHandler;
use Vonage\Client\Credentials\Handler\TokenBodyHandler;

class ClientFactory
{
Expand All @@ -27,7 +28,7 @@ public function __invoke(ContainerInterface $container): Client
->setIsHAL(false)
->setBaseUri('/verify')
->setErrorsOn200(true)
->setAuthHandler(new BasicHandler())
->setAuthHandler(new TokenBodyHandler())
->setExceptionErrorHandler(new ExceptionErrorHandler());

return new Client($api);
Expand Down
21 changes: 20 additions & 1 deletion test/Verify/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,32 @@ public function setUp(): void
->setIsHAL(false)
->setBaseUri('/verify')
->setErrorsOn200(true)
->setAuthHandler(new BasicHandler())
->setAuthHandler(new Client\Credentials\Handler\TokenBodyHandler())
->setClient($this->vonageClient->reveal())
->setExceptionErrorHandler(new ExceptionErrorHandler());

$this->client = new VerifyClient($api);
}

public function testUsesCorrectAuthInBody(): void
{
$this->vonageClient->send(
Argument::that(
function (RequestInterface $request) {
$this->assertRequestJsonBodyContains('api_key', 'abc', $request);
$this->assertRequestJsonBodyContains('api_secret', 'def', $request);
$this->assertRequestMatchesUrl('https://api.nexmo.com/verify/psd2/json', $request);

return true;
}
)
)->willReturn($this->getResponse('start'))
->shouldBeCalledTimes(1);

$request = new RequestPSD2('14845551212', 'Test Verify', '5.25');
$response = @$this->client->requestPSD2($request);
}

public function testUnserializeAcceptsObject(): void
{
$mock = @$this->getMockBuilder(Verification::class)
Expand Down

0 comments on commit b9d3b25

Please sign in to comment.