Skip to content

Commit b9d3b25

Browse files
authored
Token body never worked in the first place, mostly due to the body not being added before the auth handler has access to it (#394)
1 parent a9d0756 commit b9d3b25

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/Client/APIResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ public function create(array $body, string $uri = '', array $headers = []): ?arr
112112
$headers
113113
);
114114

115+
$request->getBody()->write(json_encode($body));
116+
115117
if ($this->getAuthHandler()) {
116118
$request = $this->addAuth($request);
117119
}
118120

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

122123
$response = $this->getClient()->send($request);

src/Client/Credentials/Handler/TokenBodyHandler.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ class TokenBodyHandler extends AbstractHandler
1111
public function __invoke(RequestInterface $request, CredentialsInterface $credentials): RequestInterface
1212
{
1313
$credentials = $this->extract(Basic::class, $credentials);
14-
$body = $request->getBody();
15-
$body->rewind();
16-
$content = $body->getContents();
17-
$params = json_decode($content, true);
1814

19-
if (!$params) {
20-
$params = [];
21-
}
15+
// We have to do some clunky body pointer rewinding here
16+
$existingBody = $request->getBody();
17+
$existingBody->rewind();
18+
$existingBodyContent = $existingBody->getContents();
19+
$existingBody->rewind();
20+
$existingBodyArray = json_decode($existingBodyContent, true);
2221

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

27-
return $request;
25+
return $request->withBody(\GuzzleHttp\Psr7\Utils::streamFor(json_encode($mergedBodyArray)));
2826
}
2927
}

src/Verify/ClientFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Vonage\Client\APIResource;
1616
use Vonage\Client\Credentials\Handler\BasicHandler;
1717
use Vonage\Client\Credentials\Handler\KeypairHandler;
18+
use Vonage\Client\Credentials\Handler\TokenBodyHandler;
1819

1920
class ClientFactory
2021
{
@@ -27,7 +28,7 @@ public function __invoke(ContainerInterface $container): Client
2728
->setIsHAL(false)
2829
->setBaseUri('/verify')
2930
->setErrorsOn200(true)
30-
->setAuthHandler(new BasicHandler())
31+
->setAuthHandler(new TokenBodyHandler())
3132
->setExceptionErrorHandler(new ExceptionErrorHandler());
3233

3334
return new Client($api);

test/Verify/ClientTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,32 @@ public function setUp(): void
5858
->setIsHAL(false)
5959
->setBaseUri('/verify')
6060
->setErrorsOn200(true)
61-
->setAuthHandler(new BasicHandler())
61+
->setAuthHandler(new Client\Credentials\Handler\TokenBodyHandler())
6262
->setClient($this->vonageClient->reveal())
6363
->setExceptionErrorHandler(new ExceptionErrorHandler());
6464

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

68+
public function testUsesCorrectAuthInBody(): void
69+
{
70+
$this->vonageClient->send(
71+
Argument::that(
72+
function (RequestInterface $request) {
73+
$this->assertRequestJsonBodyContains('api_key', 'abc', $request);
74+
$this->assertRequestJsonBodyContains('api_secret', 'def', $request);
75+
$this->assertRequestMatchesUrl('https://api.nexmo.com/verify/psd2/json', $request);
76+
77+
return true;
78+
}
79+
)
80+
)->willReturn($this->getResponse('start'))
81+
->shouldBeCalledTimes(1);
82+
83+
$request = new RequestPSD2('14845551212', 'Test Verify', '5.25');
84+
$response = @$this->client->requestPSD2($request);
85+
}
86+
6887
public function testUnserializeAcceptsObject(): void
6988
{
7089
$mock = @$this->getMockBuilder(Verification::class)

0 commit comments

Comments
 (0)