Skip to content

Commit

Permalink
Fix Messages AuthHandler (#366)
Browse files Browse the repository at this point in the history
* Correct Messages handling for auth. We should use a keypair first, then try Basic

* Check we're sending a keypair header
  • Loading branch information
SecondeJK authored Feb 9, 2023
1 parent 5081434 commit 84ab3b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Messages/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\Container\ContainerInterface;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\BasicHandler;
use Vonage\Client\Credentials\Handler\KeypairHandler;

class ClientFactory
{
Expand All @@ -16,7 +17,7 @@ public function __invoke(ContainerInterface $container): Client
->setBaseUrl($api->getClient()->getApiUrl() . '/v1/messages')
->setIsHAL(false)
->setErrorsOn200(false)
->setAuthHandler(new BasicHandler())
->setAuthHandler([new KeypairHandler(), new BasicHandler()])
->setExceptionErrorHandler(new ExceptionErrorHandler());

return new Client($api);
Expand Down
11 changes: 9 additions & 2 deletions test/Messages/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function setUp(): void
$this->vonageClient = $this->prophesize(Client::class);
$this->vonageClient->getRestUrl()->willReturn('https://rest.nexmo.com');
$this->vonageClient->getCredentials()->willReturn(
new Client\Credentials\Container(new Client\Credentials\Basic('abc', 'def'))
new Client\Credentials\Container(new Client\Credentials\Keypair(
file_get_contents(__DIR__ . '/../Client/Credentials/test.key'),
'def'
))
);

/** @noinspection PhpParamsInspection */
Expand All @@ -72,7 +75,7 @@ public function setUp(): void
->setIsHAL(false)
->setErrorsOn200(false)
->setClient($this->vonageClient->reveal())
->setAuthHandler(new Client\Credentials\Handler\BasicHandler())
->setAuthHandler([new Client\Credentials\Handler\KeypairHandler(), new Client\Credentials\Handler\BasicHandler()])
->setExceptionErrorHandler(new ExceptionErrorHandler())
->setBaseUrl('https://rest.nexmo.com');

Expand All @@ -95,6 +98,10 @@ public function testCanSendSMS(): void
$message = new SMSText($payload['to'], $payload['from'], $payload['text']);

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$this->assertEquals(
'Bearer ',
mb_substr($request->getHeaders()['Authorization'][0], 0, 7)
);
$this->assertRequestJsonBodyContains('to', $payload['to'], $request);
$this->assertRequestJsonBodyContains('from', $payload['from'], $request);
$this->assertRequestJsonBodyContains('text', $payload['text'], $request);
Expand Down

0 comments on commit 84ab3b6

Please sign in to comment.