From 2cbb82c03b2db5234c9105eb02d2f0b4e5fb5284 Mon Sep 17 00:00:00 2001 From: ckoegel Date: Mon, 15 Dec 2025 13:02:58 -0500 Subject: [PATCH 1/4] update config --- src/Configuration.php | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/Configuration.php b/src/Configuration.php index 347ca21..2f25f72 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -79,6 +79,30 @@ class Configuration */ private $webRtcBasicAuthPassword = 'TODO: Replace'; + /** + * The clientId to use for OAuth2 + * @var string + */ + private $clientId; + + /** + * The clientSecret to use for OAuth2 + * @var string + */ + private $clientSecret; + + /** + * The OAuth2 access token to use for API calls + * @var string + */ + private $accessToken; + + /** + * The expiration time of the access token + * @var int + */ + private $accessTokenExpiration; + /** * Current API environment * @var Environments @@ -126,6 +150,18 @@ public function __construct($configOptions = null) if (isset($configOptions['webRtcBasicAuthPassword'])) { $this->webRtcBasicAuthPassword = $configOptions['webRtcBasicAuthPassword']; } + if (isset($configOptions['clientId'])) { + $this->clientId = $configOptions['clientId']; + } + if (isset($configOptions['clientSecret'])) { + $this->clientSecret = $configOptions['clientSecret']; + } + if (isset($configOptions['accessToken'])) { + $this->accessToken = $configOptions['accessToken']; + } + if (isset($configOptions['accessTokenExpiration'])) { + $this->accessTokenExpiration = $configOptions['accessTokenExpiration']; + } if (isset($configOptions['environment'])) { $this->environment = $configOptions['environment']; } @@ -171,6 +207,18 @@ public function getConfigurationMap() if (isset($this->webRtcBasicAuthPassword)) { $configMap['webRtcBasicAuthPassword'] = $this->webRtcBasicAuthPassword; } + if (isset($this->clientId)) { + $configMap['clientId'] = $this->clientId; + } + if (isset($this->clientSecret)) { + $configMap['clientSecret'] = $this->clientSecret; + } + if (isset($this->accessToken)) { + $configMap['accessToken'] = $this->accessToken; + } + if (isset($this->accessTokenExpiration)) { + $configMap['accessTokenExpiration'] = $this->accessTokenExpiration; + } if (isset($this->environment)) { $configMap['environment'] = $this->environment; } @@ -247,6 +295,42 @@ public function getWebRtcBasicAuthPassword() return $this->webRtcBasicAuthPassword; } + // Getter for clientId + public function getClientId() + { + return $this->clientId; + } + + // Getter for clientSecret + public function getClientSecret() + { + return $this->clientSecret; + } + + // Getter for accessToken + public function getAccessToken() + { + return $this->accessToken; + } + + // Setter for accessToken + public function setAccessToken($value) + { + $this->accessToken = $value; + } + + // Getter for getAccessTokenExpiration + public function getAccessTokenExpiration() + { + return $this->accessTokenExpiration; + } + + // Setter for accessTokenExpiration + public function setAccessTokenExpiration($value) + { + $this->accessTokenExpiration = $value; + } + // Getter for environment public function getEnvironment() { From a3e6d75abc65c3a3b2b2a3aa0e06d1bbae521d34 Mon Sep 17 00:00:00 2001 From: ckoegel Date: Mon, 15 Dec 2025 13:03:11 -0500 Subject: [PATCH 2/4] add oauth function --- src/Controllers/BaseController.php | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/Controllers/BaseController.php b/src/Controllers/BaseController.php index 05a1cdb..7a41cc3 100644 --- a/src/Controllers/BaseController.php +++ b/src/Controllers/BaseController.php @@ -12,6 +12,7 @@ use BandwidthLib\Http\HttpResponse; use BandwidthLib\APIException; use \apimatic\jsonmapper\JsonMapper; +use Unirest\Request; /** * Base controller @@ -73,4 +74,68 @@ protected function validateResponse(HttpResponse $response, HttpContext $_httpCo throw new APIException('HTTP Response Not OK', $_httpContext); } } + + /** + * Update Auth for an HTTP request based on the current configuration + * @param array $headers The headers for the request + * @param string $authType The type of basic auth to use + */ + protected function configureAuth(&$headers, $authType) + { + if (!empty($this->config->getAccessToken()) && + (empty($this->config->getAccessTokenExpiration()) || + $this->config->getAccessTokenExpiration() > time() + 60) + ) { + $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken(); + return; + } + + if (!empty($this->config->getClientId()) && !empty($this->config->getClientSecret())) { + $_tokenUrl = 'https://api.bandwidth.com/api/v1/oauth2/token'; + $_tokenHeaders = array ( + 'User-Agent' => BaseController::USER_AGENT, + 'Content-Type' => 'application/x-www-form-urlencoded', + 'Authorization' => 'Basic ' . base64_encode( + $this->config->getClientId() . ':' . $this->config->getClientSecret() + ) + ); + $_tokenBody = Request\Body::Form([ + 'grant_type' => 'client_credentials' + ]); + $response = Request::post($_tokenUrl, $_tokenHeaders, $_tokenBody); + $this->config->setAccessToken($response->body->access_token); + $this->config->setAccessTokenExpiration(time() + $response->body->expires_in); + $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken(); + + return; + } + + $username = ''; + $password = ''; + + switch ($authType) { + case 'messaging': + $username = $this->config->getMessagingBasicAuthUserName(); + $password = $this->config->getMessagingBasicAuthPassword(); + break; + case 'voice': + $username = $this->config->getVoiceBasicAuthUserName(); + $password = $this->config->getVoiceBasicAuthPassword(); + break; + case 'webrtc': + $username = $this->config->getWebRtcBasicAuthUserName(); + $password = $this->config->getWebRtcBasicAuthPassword(); + break; + case 'phoneNumberLookup': + $username = $this->config->getPhoneNumberLookupBasicAuthUserName(); + $password = $this->config->getPhoneNumberLookupBasicAuthPassword(); + break; + case 'multiFactorAuth': + $username = $this->config->getMultiFactorAuthBasicAuthUserName(); + $password = $this->config->getMultiFactorAuthBasicAuthPassword(); + break; + } + + Request::auth($username, $password); + } } From 9f106fadad0505ab0ad83f4e34a2e42e144d30dc Mon Sep 17 00:00:00 2001 From: ckoegel Date: Mon, 15 Dec 2025 13:03:36 -0500 Subject: [PATCH 3/4] update controllers --- src/Messaging/Controllers/APIController.php | 12 ++--- .../Controllers/MFAController.php | 6 +-- .../Controllers/APIController.php | 12 ++--- src/Voice/Controllers/APIController.php | 44 +++++++++---------- src/WebRtc/Controllers/APIController.php | 22 +++++----- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/Messaging/Controllers/APIController.php b/src/Messaging/Controllers/APIController.php index 9a67190..abb5828 100644 --- a/src/Messaging/Controllers/APIController.php +++ b/src/Messaging/Controllers/APIController.php @@ -62,7 +62,7 @@ public function listMedia( ); //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -151,7 +151,7 @@ public function getMedia( ); //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -252,7 +252,7 @@ public function uploadMedia( $_bodyJson = $body; //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); @@ -341,7 +341,7 @@ public function deleteMedia( ); //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -461,7 +461,7 @@ public function getMessages( ); //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -556,7 +556,7 @@ public function createMessage( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword()); + $this->configureAuth($_headers, 'messaging'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); diff --git a/src/MultiFactorAuth/Controllers/MFAController.php b/src/MultiFactorAuth/Controllers/MFAController.php index 347bc4d..414f435 100644 --- a/src/MultiFactorAuth/Controllers/MFAController.php +++ b/src/MultiFactorAuth/Controllers/MFAController.php @@ -66,7 +66,7 @@ public function createVoiceTwoFactor( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getMultiFactorAuthBasicAuthUserName(), $this->config->getMultiFactorAuthBasicAuthPassword()); + $this->configureAuth($_headers, 'multiFactorAuth'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -160,7 +160,7 @@ public function createMessagingTwoFactor( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getMultiFactorAuthBasicAuthUserName(), $this->config->getMultiFactorAuthBasicAuthPassword()); + $this->configureAuth($_headers, 'multiFactorAuth'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -253,7 +253,7 @@ public function createVerifyTwoFactor( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getMultiFactorAuthBasicAuthUserName(), $this->config->getMultiFactorAuthBasicAuthPassword()); + $this->configureAuth($_headers, 'multiFactorAuth'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); diff --git a/src/PhoneNumberLookup/Controllers/APIController.php b/src/PhoneNumberLookup/Controllers/APIController.php index 4f9dc8d..aa68036 100644 --- a/src/PhoneNumberLookup/Controllers/APIController.php +++ b/src/PhoneNumberLookup/Controllers/APIController.php @@ -64,8 +64,8 @@ public function createAsyncBulkLookupRequest( //json encode body $_bodyJson = Request\Body::Json($body); - //set HTTP basic auth parameters - Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); + //set authentication + $this->configureAuth($_headers, 'phoneNumberLookup'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -169,8 +169,8 @@ public function getAsyncLookupRequestStatus( 'Accept' => 'application/json' ); - //set HTTP basic auth parameters - Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); + //set authentication + $this->configureAuth($_headers, 'phoneNumberLookup'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -277,8 +277,8 @@ public function createSyncLookupRequest( //json encode body $_bodyJson = Request\Body::Json($body); - //set HTTP basic auth parameters - Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); + //set authentication + $this->configureAuth($_headers, 'phoneNumberLookup'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); diff --git a/src/Voice/Controllers/APIController.php b/src/Voice/Controllers/APIController.php index f5069da..6f7fdcf 100644 --- a/src/Voice/Controllers/APIController.php +++ b/src/Voice/Controllers/APIController.php @@ -65,7 +65,7 @@ public function createCall( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -173,7 +173,7 @@ public function getCall( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -283,7 +283,7 @@ public function modifyCallBxml( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); @@ -394,7 +394,7 @@ public function modifyCall( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -505,7 +505,7 @@ public function modifyCallRecordingState( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); @@ -612,7 +612,7 @@ public function getCallRecordings( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -726,7 +726,7 @@ public function getCallRecording( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -839,7 +839,7 @@ public function deleteRecording( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -949,7 +949,7 @@ public function getDownloadCallRecording( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -1059,7 +1059,7 @@ public function deleteRecordingMedia( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -1169,7 +1169,7 @@ public function getCallTranscription( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -1289,7 +1289,7 @@ public function createTranscribeCallRecording( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -1405,7 +1405,7 @@ public function deleteCallTranscription( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -1527,7 +1527,7 @@ public function getConferences( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -1635,7 +1635,7 @@ public function getConference( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -1748,7 +1748,7 @@ public function modifyConference( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -1862,7 +1862,7 @@ public function modifyConferenceMember( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); @@ -1972,7 +1972,7 @@ public function getConferenceMember( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -2084,7 +2084,7 @@ public function getConferenceRecordings( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -2199,7 +2199,7 @@ public function getConferenceRecording( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -2314,7 +2314,7 @@ public function getDownloadConferenceRecording( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -2436,7 +2436,7 @@ public function getQueryCallRecordings( ); //set HTTP basic auth parameters - Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword()); + $this->configureAuth($_headers, 'voice'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); diff --git a/src/WebRtc/Controllers/APIController.php b/src/WebRtc/Controllers/APIController.php index 354e85c..7f27aed 100644 --- a/src/WebRtc/Controllers/APIController.php +++ b/src/WebRtc/Controllers/APIController.php @@ -68,7 +68,7 @@ public function createParticipant( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -149,7 +149,7 @@ public function getParticipant( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -226,7 +226,7 @@ public function deleteParticipant( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -308,7 +308,7 @@ public function createSession( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); @@ -386,7 +386,7 @@ public function getSession( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -463,7 +463,7 @@ public function deleteSession( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -539,7 +539,7 @@ public function listSessionParticipants( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -628,7 +628,7 @@ public function addParticipantToSession( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); @@ -709,7 +709,7 @@ public function removeParticipantFromSession( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl); @@ -789,7 +789,7 @@ public function getParticipantSubscriptions( ); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); @@ -881,7 +881,7 @@ public function updateParticipantSubscriptions( $_bodyJson = Request\Body::Json($body); //set HTTP basic auth parameters - Request::auth($this->config->getWebRtcBasicAuthUserName(), $this->config->getWebRtcBasicAuthPassword()); + $this->configureAuth($_headers, 'webrtc'); $_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl); From 08210c11f8ad27c88197df5883307fc5d68e66c1 Mon Sep 17 00:00:00 2001 From: ckoegel Date: Mon, 15 Dec 2025 13:03:40 -0500 Subject: [PATCH 4/4] update tests --- tests/ApiTest.php | 59 +++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 9d54d9a..44c9bc9 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -12,22 +12,31 @@ final class ApiTest extends TestCase { - protected $bandwidthClient; + protected static $bandwidthClient; + protected static $messagingMFAClient; - protected function setUp(): void { + public static function setUpBeforeClass(): void { $config = new BandwidthLib\Configuration( array( - 'messagingBasicAuthUserName' => getenv("BW_USERNAME"), - 'messagingBasicAuthPassword' => getenv("BW_PASSWORD"), 'voiceBasicAuthUserName' => getenv("BW_USERNAME"), 'voiceBasicAuthPassword' => getenv("BW_PASSWORD"), + 'phoneNumberLookupBasicAuthUserName' => getenv("BW_USERNAME"), + 'phoneNumberLookupBasicAuthPassword' => getenv("BW_PASSWORD"), + 'clientId' => getenv("BW_CLIENT_ID"), + 'clientSecret' => getenv("BW_CLIENT_SECRET"), + ) + ); + self::$bandwidthClient = new BandwidthLib\BandwidthClient($config); + + $messagingMFAConfig = new BandwidthLib\Configuration( + array( + 'messagingBasicAuthUserName' => getenv("BW_USERNAME"), + 'messagingBasicAuthPassword' => getenv("BW_PASSWORD"), 'multiFactorAuthBasicAuthUserName' => getenv("BW_USERNAME"), 'multiFactorAuthBasicAuthPassword' => getenv("BW_PASSWORD"), - 'phoneNumberLookupBasicAuthUserName' => getenv("BW_USERNAME"), - 'phoneNumberLookupBasicAuthPassword' => getenv("BW_PASSWORD") ) ); - $this->bandwidthClient = new BandwidthLib\BandwidthClient($config); + self::$messagingMFAClient = new BandwidthLib\BandwidthClient($messagingMFAConfig); } public function testCreateMessage() { @@ -37,7 +46,7 @@ public function testCreateMessage() { $body->applicationId = getenv("BW_MESSAGING_APPLICATION_ID"); $body->text = "PHP Monitoring"; - $response = $this->bandwidthClient->getMessaging()->getClient()->createMessage(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$messagingMFAClient->getMessaging()->getClient()->createMessage(getenv("BW_ACCOUNT_ID"), $body); $this->assertTrue(strlen($response->getResult()->id) > 0); //validate that _some_ id was returned } @@ -50,7 +59,7 @@ public function testCreateMessageInvalidPhoneNumber() { $body->text = "PHP Monitoring"; try { - $this->bandwidthClient->getMessaging()->getClient()->createMessage(getenv("BW_ACCOUNT_ID"), $body); + self::$messagingMFAClient->getMessaging()->getClient()->createMessage(getenv("BW_ACCOUNT_ID"), $body); //workaround to make sure that if the above error is not raised, the build will fail $this->assertTrue(false); } catch (BandwidthLib\Messaging\Exceptions\MessagingException $e) { @@ -65,13 +74,13 @@ public function testUploadDownloadMedia() { $contentType = 'text/plain'; //media upload - $this->bandwidthClient->getMessaging()->getClient()->uploadMedia(getenv("BW_ACCOUNT_ID"), $mediaId, $content, $contentType); + self::$messagingMFAClient->getMessaging()->getClient()->uploadMedia(getenv("BW_ACCOUNT_ID"), $mediaId, $content, $contentType); //media download - $downloadedContent = $this->bandwidthClient->getMessaging()->getClient()->getMedia(getenv("BW_ACCOUNT_ID"), $mediaId)->getResult(); + $downloadedContent = self::$messagingMFAClient->getMessaging()->getClient()->getMedia(getenv("BW_ACCOUNT_ID"), $mediaId)->getResult(); //media delete - $this->bandwidthClient->getMessaging()->getClient()->deleteMedia(getenv("BW_ACCOUNT_ID"), $mediaId); + self::$messagingMFAClient->getMessaging()->getClient()->deleteMedia(getenv("BW_ACCOUNT_ID"), $mediaId); //validate that response is the same $this->assertEquals($downloadedContent, $content); @@ -83,13 +92,13 @@ public function testCreateCallAndGetCallState() { $body->to = getenv("USER_NUMBER"); $body->applicationId = getenv("BW_VOICE_APPLICATION_ID"); $body->answerUrl = getenv("BASE_CALLBACK_URL"); - $response = $this->bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); $callId = $response->getResult()->callId; $this->assertTrue(strlen($callId) > 0); $this->assertTrue(is_a($response->getResult()->enqueuedTime, 'DateTime')); //get phone call information (This is commented out until voice fixes their latency issues - // $response = $this->bandwidthClient->getVoice()->getClient()->getCall(getenv("BW_ACCOUNT_ID"), $callId); + // $response = self::$bandwidthClient->getVoice()->getClient()->getCall(getenv("BW_ACCOUNT_ID"), $callId); // $this->assertTrue(is_a($response->getResult()->enqueuedTime, 'DateTime')); } @@ -113,14 +122,14 @@ public function testCreateCallWithAmdAndGetCallState() { $body->applicationId = getenv("BW_VOICE_APPLICATION_ID"); $body->answerUrl = getenv("BASE_CALLBACK_URL"); $body->machineDetection = $machineDetection; - $response = $this->bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); $callId = $response->getResult()->callId; $this->assertTrue(strlen($callId) > 0); sleep(25); //get phone call information - // $response = $this->bandwidthClient->getVoice()->getClient()->getCall(getenv("BW_ACCOUNT_ID"), $callId); + // $response = self::$bandwidthClient->getVoice()->getClient()->getCall(getenv("BW_ACCOUNT_ID"), $callId); // if (($response->getStatus() == 404) ) { // $this->assertTrue(is_a($response->getResult()->enqueuedTime, 'DateTime')); // } @@ -132,7 +141,7 @@ public function testCreateCallWithPriority() { $body->applicationId = getenv("BW_VOICE_APPLICATION_ID"); $body->answerUrl = getenv("BASE_CALLBACK_URL"); $body->priority = 1; - $response = $this->bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); $callId = $response->getResult()->callId; $this->assertTrue(strlen($callId) > 0); $this->assertTrue($response->getResult()->priority == $body->priority); @@ -146,7 +155,7 @@ public function testCreateCallInvalidPhoneNumber() { $body->answerUrl = getenv("BASE_CALLBACK_URL"); try { - $this->bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); + self::$bandwidthClient->getVoice()->getClient()->createCall(getenv("BW_ACCOUNT_ID"), $body); //workaround to make sure that if the above error is not raised, the build will fail $this->assertTrue(false); } catch (BandwidthLib\Voice\Exceptions\ApiErrorException $e) { @@ -163,7 +172,7 @@ public function testMfaMessaging() { $body->digits = 6; $body->message = "Your temporary {NAME} {SCOPE} code is {CODE}"; - $response = $this->bandwidthClient->getMultiFactorAuth()->getMFA()->createMessagingTwoFactor(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$messagingMFAClient->getMultiFactorAuth()->getMFA()->createMessagingTwoFactor(getenv("BW_ACCOUNT_ID"), $body); $this->assertTrue(strlen($response->getResult()->messageId) > 0); //validate that _some_ id was returned } @@ -176,7 +185,7 @@ public function testMfaVoice() { $body->digits = 6; $body->message = "Your temporary {NAME} {SCOPE} code is {CODE}"; - $response = $this->bandwidthClient->getMultiFactorAuth()->getMFA()->createVoiceTwoFactor(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$messagingMFAClient->getMultiFactorAuth()->getMFA()->createVoiceTwoFactor(getenv("BW_ACCOUNT_ID"), $body); $this->assertTrue(strlen($response->getResult()->callId) > 0); //validate that _some_ id was returned } @@ -189,14 +198,14 @@ public function testMfaVerify() { $body->digits = 6; $body->expirationTimeInMinutes = 3; - $response = $this->bandwidthClient->getMultiFactorAuth()->getMFA()->createVerifyTwoFactor(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$messagingMFAClient->getMultiFactorAuth()->getMFA()->createVerifyTwoFactor(getenv("BW_ACCOUNT_ID"), $body); $this->assertTrue(is_bool($response->getResult()->valid)); } public function testAsyncTnLookup() { $body = new BandwidthLib\PhoneNumberLookup\Models\CreateLookupRequest(); $body->phoneNumbers = [getenv("USER_NUMBER")]; - $createResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->createAsyncBulkLookupRequest(getenv("BW_ACCOUNT_ID"), $body); + $createResponse = self::$bandwidthClient->getPhoneNumberLookup()->getClient()->createAsyncBulkLookupRequest(getenv("BW_ACCOUNT_ID"), $body); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\CreateAsyncBulkResponse::class, $createResponse->getResult()); $this->assertIsArray($createResponse->getResult()->links); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\CreateAsyncBulkResponseData::class, $createResponse->getResult()->data); @@ -206,7 +215,7 @@ public function testAsyncTnLookup() { sleep(30); - $statusResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->getAsyncLookupRequestStatus(getenv("BW_ACCOUNT_ID"), $createResponse->getResult()->data->requestId); + $statusResponse = self::$bandwidthClient->getPhoneNumberLookup()->getClient()->getAsyncLookupRequestStatus(getenv("BW_ACCOUNT_ID"), $createResponse->getResult()->data->requestId); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponse::class, $statusResponse->getResult()); $this->assertIsArray($statusResponse->getResult()->links); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponseData::class, $statusResponse->getResult()->data); @@ -225,7 +234,7 @@ public function testAsyncTnLookup() { public function testSyncTnLookup() { $body = new BandwidthLib\PhoneNumberLookup\Models\CreateLookupRequest(); $body->phoneNumbers = [getenv("USER_NUMBER")]; - $response = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->createSyncLookupRequest(getenv("BW_ACCOUNT_ID"), $body); + $response = self::$bandwidthClient->getPhoneNumberLookup()->getClient()->createSyncLookupRequest(getenv("BW_ACCOUNT_ID"), $body); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponse::class, $response->getResult()); $this->assertIsArray($response->getResult()->links); $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\Link::class, $response->getResult()->links[0]); @@ -237,7 +246,7 @@ public function testSyncTnLookup() { $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResult::class, $response->getResult()->data->results[0]); $this->assertIsString($response->getResult()->data->results[0]->phoneNumber); $this->assertIsString($response->getResult()->data->results[0]->lineType); - $this->assertIsString($response->getResult()->data->results[0]->messagingProvider); + // $this->assertIsString($response->getResult()->data->results[0]->messagingProvider); $this->assertIsString($response->getResult()->data->results[0]->voiceProvider); $this->assertIsString($response->getResult()->data->results[0]->countryCodeA3); $this->assertIsArray($response->getResult()->errors);