Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
{
Expand Down
65 changes: 65 additions & 0 deletions src/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use BandwidthLib\Http\HttpResponse;
use BandwidthLib\APIException;
use \apimatic\jsonmapper\JsonMapper;
use Unirest\Request;

/**
* Base controller
Expand Down Expand Up @@ -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);
}
}
12 changes: 6 additions & 6 deletions src/Messaging/Controllers/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/MultiFactorAuth/Controllers/MFAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions src/PhoneNumberLookup/Controllers/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Loading