From 3cbc6a8cfbff1561388c8f570582672708203f29 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Wed, 17 Jul 2024 16:04:22 +0100 Subject: [PATCH] Add validation for brand (#490) --- src/Verify2/Request/BaseVerifyRequest.php | 9 +++++++++ src/Verify2/Request/EmailRequest.php | 5 +++++ src/Verify2/Request/SMSRequest.php | 5 +++++ src/Verify2/Request/SilentAuthRequest.php | 5 +++++ src/Verify2/Request/VoiceRequest.php | 7 ++++++- src/Verify2/Request/WhatsAppInteractiveRequest.php | 7 ++++++- src/Verify2/Request/WhatsAppRequest.php | 5 +++++ test/Verify2/ClientTest.php | 4 ++-- 8 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Verify2/Request/BaseVerifyRequest.php b/src/Verify2/Request/BaseVerifyRequest.php index b0465775..529a4252 100644 --- a/src/Verify2/Request/BaseVerifyRequest.php +++ b/src/Verify2/Request/BaseVerifyRequest.php @@ -170,4 +170,13 @@ public function getBaseVerifyUniversalOutputArray(): array return $returnArray; } + + public static function isBrandValid(string $brand): bool + { + if (!strlen($brand) < 16) { + return true; + } + + return false; + } } diff --git a/src/Verify2/Request/EmailRequest.php b/src/Verify2/Request/EmailRequest.php index d7694e96..9c48e7a9 100644 --- a/src/Verify2/Request/EmailRequest.php +++ b/src/Verify2/Request/EmailRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationLocale; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; @@ -13,6 +14,10 @@ public function __construct( protected string $from, protected ?VerificationLocale $locale = null, ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + if (!$this->locale) { $this->locale = new VerificationLocale(); } diff --git a/src/Verify2/Request/SMSRequest.php b/src/Verify2/Request/SMSRequest.php index 878378bb..8ea790e3 100644 --- a/src/Verify2/Request/SMSRequest.php +++ b/src/Verify2/Request/SMSRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationLocale; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; @@ -15,6 +16,10 @@ public function __construct( protected string $entityId = '', protected string $contentId = '' ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + if (!$this->locale) { $this->locale = new VerificationLocale(); } diff --git a/src/Verify2/Request/SilentAuthRequest.php b/src/Verify2/Request/SilentAuthRequest.php index 4a74c046..b391e7df 100644 --- a/src/Verify2/Request/SilentAuthRequest.php +++ b/src/Verify2/Request/SilentAuthRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; class SilentAuthRequest extends BaseVerifyRequest @@ -11,6 +12,10 @@ public function __construct( protected string $brand, protected ?string $redirectUrl = null ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_SILENT_AUTH, $to); if ($this->redirectUrl) { diff --git a/src/Verify2/Request/VoiceRequest.php b/src/Verify2/Request/VoiceRequest.php index 5d04cd89..9a70e5e9 100644 --- a/src/Verify2/Request/VoiceRequest.php +++ b/src/Verify2/Request/VoiceRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationLocale; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; @@ -12,6 +13,10 @@ public function __construct( protected string $brand, protected ?VerificationLocale $locale = null, ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + if (!$this->locale) { $this->locale = new VerificationLocale(); } @@ -29,4 +34,4 @@ public function toArray(): array { return $this->getBaseVerifyUniversalOutputArray(); } -} \ No newline at end of file +} diff --git a/src/Verify2/Request/WhatsAppInteractiveRequest.php b/src/Verify2/Request/WhatsAppInteractiveRequest.php index 1cee7d39..858c451b 100644 --- a/src/Verify2/Request/WhatsAppInteractiveRequest.php +++ b/src/Verify2/Request/WhatsAppInteractiveRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationLocale; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; @@ -12,6 +13,10 @@ public function __construct( protected string $brand, protected ?VerificationLocale $locale = null ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + if (!$this->locale) { $this->locale = new VerificationLocale(); } @@ -24,4 +29,4 @@ public function toArray(): array { return $this->getBaseVerifyUniversalOutputArray(); } -} \ No newline at end of file +} diff --git a/src/Verify2/Request/WhatsAppRequest.php b/src/Verify2/Request/WhatsAppRequest.php index 6b43c557..74d9ba6b 100644 --- a/src/Verify2/Request/WhatsAppRequest.php +++ b/src/Verify2/Request/WhatsAppRequest.php @@ -2,6 +2,7 @@ namespace Vonage\Verify2\Request; +use InvalidArgumentException; use Vonage\Verify2\VerifyObjects\VerificationLocale; use Vonage\Verify2\VerifyObjects\VerificationWorkflow; @@ -13,6 +14,10 @@ public function __construct( protected string $from, protected ?VerificationLocale $locale = null, ) { + if (!self::isBrandValid($this->brand)) { + throw new InvalidArgumentException('The brand name cannot be longer than 16 characters.'); + } + if (!$this->locale) { $this->locale = new VerificationLocale(); } diff --git a/test/Verify2/ClientTest.php b/test/Verify2/ClientTest.php index 013f523a..320be2c9 100644 --- a/test/Verify2/ClientTest.php +++ b/test/Verify2/ClientTest.php @@ -8,6 +8,7 @@ use Laminas\Diactoros\Response; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use Vonage\Messages\Channel\Messenger\InvalidCategoryException; use VonageTest\Traits\Psr7AssertionTrait; use Vonage\Client; use Vonage\Client\APIResource; @@ -500,8 +501,7 @@ public function testCannotSendConcurrentVerifications(): void public function testCannotSendWithoutBrand(): void { - $this->expectException(Client\Exception\Request::class); - $this->expectExceptionMessage('Invalid params: The value of one or more parameters is invalid. See https://www.developer.vonage.com/api-errors#invalid-params for more information'); + $this->expectException(\InvalidArgumentException::class); $payload = [ 'to' => '07785254785',