|
12 | 12 | use BandwidthLib\Http\HttpResponse; |
13 | 13 | use BandwidthLib\APIException; |
14 | 14 | use \apimatic\jsonmapper\JsonMapper; |
| 15 | +use Unirest\Request; |
15 | 16 |
|
16 | 17 | /** |
17 | 18 | * Base controller |
@@ -73,4 +74,68 @@ protected function validateResponse(HttpResponse $response, HttpContext $_httpCo |
73 | 74 | throw new APIException('HTTP Response Not OK', $_httpContext); |
74 | 75 | } |
75 | 76 | } |
| 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 | + } |
76 | 141 | } |
0 commit comments