From d68569d8204b1401dd7ce749a076aa329921b95f Mon Sep 17 00:00:00 2001 From: Jacob Mulford <39915377+jmulford-bw@users.noreply.github.com> Date: Fri, 5 Feb 2021 10:17:53 -0500 Subject: [PATCH] MFA error update and message priority (#20) * New deploy * Update APIController.php Co-authored-by: jmulford-bw --- src/Configuration.php | 2 +- src/Messaging/Models/BandwidthMessage.php | 10 ++- src/Messaging/Models/MessageRequest.php | 11 ++- src/Messaging/Models/PriorityEnum.php | 25 ++++++ .../{APIController.php => MFAController.php} | 84 +++++++++++++++++-- .../Exceptions/ErrorWithRequestException.php | 44 ++++++++++ .../Exceptions/ForbiddenRequestException.php | 39 +++++++++ ...n.php => UnauthorizedRequestException.php} | 8 +- src/TwoFactorAuth/TwoFactorAuthClient.php | 14 ++-- 9 files changed, 216 insertions(+), 21 deletions(-) create mode 100644 src/Messaging/Models/PriorityEnum.php rename src/TwoFactorAuth/Controllers/{APIController.php => MFAController.php} (75%) create mode 100644 src/TwoFactorAuth/Exceptions/ErrorWithRequestException.php create mode 100644 src/TwoFactorAuth/Exceptions/ForbiddenRequestException.php rename src/TwoFactorAuth/Exceptions/{InvalidRequestException.php => UnauthorizedRequestException.php} (74%) diff --git a/src/Configuration.php b/src/Configuration.php index e0ebf29..7094a77 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -247,7 +247,7 @@ public function getBaseUri($server = Servers::DEFAULT_) Environments::PRODUCTION => array( Servers::DEFAULT_ => 'api.bandwidth.com', Servers::MESSAGINGDEFAULT => 'https://messaging.bandwidth.com/api/v2', - Servers::TWOFACTORAUTHDEFAULT => 'https://mfa.bandwidth.com/api/v1/', + Servers::TWOFACTORAUTHDEFAULT => 'https://mfa.bandwidth.com/api/v1', Servers::VOICEDEFAULT => 'https://voice.bandwidth.com', Servers::WEBRTCDEFAULT => 'https://api.webrtc.bandwidth.com/v1', ), diff --git a/src/Messaging/Models/BandwidthMessage.php b/src/Messaging/Models/BandwidthMessage.php index 6b74e45..2a3c86b 100644 --- a/src/Messaging/Models/BandwidthMessage.php +++ b/src/Messaging/Models/BandwidthMessage.php @@ -79,12 +79,18 @@ class BandwidthMessage implements \JsonSerializable */ public $tag; + /** + * The priority specified by the user + * @var string|null $priority public property + */ + public $priority; + /** * Constructor to set initial or default values of member properties */ public function __construct() { - if (11 == func_num_args()) { + if (12 == func_num_args()) { $this->id = func_get_arg(0); $this->owner = func_get_arg(1); $this->applicationId = func_get_arg(2); @@ -96,6 +102,7 @@ public function __construct() $this->media = func_get_arg(8); $this->text = func_get_arg(9); $this->tag = func_get_arg(10); + $this->priority = func_get_arg(11); } } @@ -118,6 +125,7 @@ public function jsonSerialize() array_values($this->media) : null; $json['text'] = $this->text; $json['tag'] = $this->tag; + $json['priority'] = $this->priority; return array_filter($json); } diff --git a/src/Messaging/Models/MessageRequest.php b/src/Messaging/Models/MessageRequest.php index b75c646..95d6b70 100644 --- a/src/Messaging/Models/MessageRequest.php +++ b/src/Messaging/Models/MessageRequest.php @@ -52,18 +52,26 @@ class MessageRequest implements \JsonSerializable */ public $tag; + /** + * The message's priority, currently for toll-free or short code SMS only. Messages with a priority + * value of `"high"` are given preference over your other traffic. + * @var string|null $priority public property + */ + public $priority; + /** * Constructor to set initial or default values of member properties */ public function __construct() { - if (6 == func_num_args()) { + if (7 == func_num_args()) { $this->applicationId = func_get_arg(0); $this->to = func_get_arg(1); $this->from = func_get_arg(2); $this->text = func_get_arg(3); $this->media = func_get_arg(4); $this->tag = func_get_arg(5); + $this->priority = func_get_arg(6); } } @@ -80,6 +88,7 @@ public function jsonSerialize() $json['media'] = isset($this->media) ? array_values($this->media) : null; $json['tag'] = $this->tag; + $json['priority'] = $this->priority; return array_filter($json); } diff --git a/src/Messaging/Models/PriorityEnum.php b/src/Messaging/Models/PriorityEnum.php new file mode 100644 index 0000000..cb7ce74 --- /dev/null +++ b/src/Messaging/Models/PriorityEnum.php @@ -0,0 +1,25 @@ +code == 400) { - throw new Exceptions\InvalidRequestException('client request error', $_httpContext); + throw new Exceptions\ErrorWithRequestException( + 'If there is any issue with values passed in by the user', + $_httpContext + ); + } + + if ($response->code == 401) { + throw new Exceptions\UnauthorizedRequestException( + 'Authentication is either incorrect or not present', + $_httpContext + ); + } + + if ($response->code == 403) { + throw new Exceptions\ForbiddenRequestException( + 'The user is not authorized to access this resource', + $_httpContext + ); + } + + if ($response->code == 500) { + throw new Exceptions\ErrorWithRequestException('An internal server error occurred', $_httpContext); } //handle errors defined at the API level @@ -103,7 +124,7 @@ public function createVoiceTwoFactor( } /** - * Two-Factor authentication with Bandwidth messaging services + * Allows a user to send a MFA code through a text message (SMS) * * @param string $accountId Bandwidth Account ID with Messaging service enabled * @param Models\TwoFactorCodeRequestSchema $body TODO: type description here @@ -161,7 +182,28 @@ public function createMessagingTwoFactor( //Error handling using HTTP status codes if ($response->code == 400) { - throw new Exceptions\InvalidRequestException('client request error', $_httpContext); + throw new Exceptions\ErrorWithRequestException( + 'If there is any issue with values passed in by the user', + $_httpContext + ); + } + + if ($response->code == 401) { + throw new Exceptions\UnauthorizedRequestException( + 'Authentication is either incorrect or not present', + $_httpContext + ); + } + + if ($response->code == 403) { + throw new Exceptions\ForbiddenRequestException( + 'The user is not authorized to access this resource', + $_httpContext + ); + } + + if ($response->code == 500) { + throw new Exceptions\ErrorWithRequestException('An internal server error occurred', $_httpContext); } //handle errors defined at the API level @@ -175,7 +217,7 @@ public function createMessagingTwoFactor( } /** - * Verify a previously sent two-factor authentication code + * Allows a user to verify an MFA code * * @param string $accountId Bandwidth Account ID with Two-Factor enabled * @param Models\TwoFactorVerifyRequestSchema $body TODO: type description here @@ -233,7 +275,35 @@ public function createVerifyTwoFactor( //Error handling using HTTP status codes if ($response->code == 400) { - throw new Exceptions\InvalidRequestException('client request error', $_httpContext); + throw new Exceptions\ErrorWithRequestException( + 'If there is any issue with values passed in by the user', + $_httpContext + ); + } + + if ($response->code == 401) { + throw new Exceptions\UnauthorizedRequestException( + 'Authentication is either incorrect or not present', + $_httpContext + ); + } + + if ($response->code == 403) { + throw new Exceptions\ForbiddenRequestException( + 'The user is not authorized to access this resource', + $_httpContext + ); + } + + if ($response->code == 429) { + throw new Exceptions\ErrorWithRequestException( + 'The user has made too many bad requests and is temporarily locked out', + $_httpContext + ); + } + + if ($response->code == 500) { + throw new Exceptions\ErrorWithRequestException('An internal server error occurred', $_httpContext); } //handle errors defined at the API level diff --git a/src/TwoFactorAuth/Exceptions/ErrorWithRequestException.php b/src/TwoFactorAuth/Exceptions/ErrorWithRequestException.php new file mode 100644 index 0000000..cd59b2f --- /dev/null +++ b/src/TwoFactorAuth/Exceptions/ErrorWithRequestException.php @@ -0,0 +1,44 @@ +client == null) { - $this->client = new Controllers\APIController($this->config); + if ($this->mFA == null) { + $this->mFA = new Controllers\MFAController($this->config); } - return $this->client; + return $this->mFA; } }