Skip to content

Commit a3e6d75

Browse files
committed
add oauth function
1 parent 2cbb82c commit a3e6d75

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/Controllers/BaseController.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BandwidthLib\Http\HttpResponse;
1313
use BandwidthLib\APIException;
1414
use \apimatic\jsonmapper\JsonMapper;
15+
use Unirest\Request;
1516

1617
/**
1718
* Base controller
@@ -73,4 +74,68 @@ protected function validateResponse(HttpResponse $response, HttpContext $_httpCo
7374
throw new APIException('HTTP Response Not OK', $_httpContext);
7475
}
7576
}
77+
78+
/**
79+
* Update Auth for an HTTP request based on the current configuration
80+
* @param array $headers The headers for the request
81+
* @param string $authType The type of basic auth to use
82+
*/
83+
protected function configureAuth(&$headers, $authType)
84+
{
85+
if (!empty($this->config->getAccessToken()) &&
86+
(empty($this->config->getAccessTokenExpiration()) ||
87+
$this->config->getAccessTokenExpiration() > time() + 60)
88+
) {
89+
$headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
90+
return;
91+
}
92+
93+
if (!empty($this->config->getClientId()) && !empty($this->config->getClientSecret())) {
94+
$_tokenUrl = 'https://api.bandwidth.com/api/v1/oauth2/token';
95+
$_tokenHeaders = array (
96+
'User-Agent' => BaseController::USER_AGENT,
97+
'Content-Type' => 'application/x-www-form-urlencoded',
98+
'Authorization' => 'Basic ' . base64_encode(
99+
$this->config->getClientId() . ':' . $this->config->getClientSecret()
100+
)
101+
);
102+
$_tokenBody = Request\Body::Form([
103+
'grant_type' => 'client_credentials'
104+
]);
105+
$response = Request::post($_tokenUrl, $_tokenHeaders, $_tokenBody);
106+
$this->config->setAccessToken($response->body->access_token);
107+
$this->config->setAccessTokenExpiration(time() + $response->body->expires_in);
108+
$headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
109+
110+
return;
111+
}
112+
113+
$username = '';
114+
$password = '';
115+
116+
switch ($authType) {
117+
case 'messaging':
118+
$username = $this->config->getMessagingBasicAuthUserName();
119+
$password = $this->config->getMessagingBasicAuthPassword();
120+
break;
121+
case 'voice':
122+
$username = $this->config->getVoiceBasicAuthUserName();
123+
$password = $this->config->getVoiceBasicAuthPassword();
124+
break;
125+
case 'webrtc':
126+
$username = $this->config->getWebRtcBasicAuthUserName();
127+
$password = $this->config->getWebRtcBasicAuthPassword();
128+
break;
129+
case 'phoneNumberLookup':
130+
$username = $this->config->getPhoneNumberLookupBasicAuthUserName();
131+
$password = $this->config->getPhoneNumberLookupBasicAuthPassword();
132+
break;
133+
case 'multiFactorAuth':
134+
$username = $this->config->getMultiFactorAuthBasicAuthUserName();
135+
$password = $this->config->getMultiFactorAuthBasicAuthPassword();
136+
break;
137+
}
138+
139+
Request::auth($username, $password);
140+
}
76141
}

0 commit comments

Comments
 (0)