From 84ab3b612e436f3508e4b651ba857199c971f66b Mon Sep 17 00:00:00 2001 From: James Seconde Date: Thu, 9 Feb 2023 11:40:46 +0000 Subject: [PATCH] Fix Messages AuthHandler (#366) * Correct Messages handling for auth. We should use a keypair first, then try Basic * Check we're sending a keypair header --- src/Messages/ClientFactory.php | 3 ++- test/Messages/ClientTest.php | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Messages/ClientFactory.php b/src/Messages/ClientFactory.php index 37cf3ca5..7ef55fc9 100644 --- a/src/Messages/ClientFactory.php +++ b/src/Messages/ClientFactory.php @@ -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 { @@ -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); diff --git a/test/Messages/ClientTest.php b/test/Messages/ClientTest.php index cd8cb37d..c762a29d 100644 --- a/test/Messages/ClientTest.php +++ b/test/Messages/ClientTest.php @@ -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 */ @@ -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'); @@ -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);