move authentication to the API level, so that supporting classes aren't api-specific

This commit is contained in:
nmonterroso
2015-06-22 10:31:38 -07:00
parent 53bddae904
commit e598384d97
2 changed files with 19 additions and 40 deletions

View File

@@ -99,6 +99,14 @@ class ApiClient {
return $this->host;
}
/**
* get the config
* @return ApiConfiguration
*/
public function getConfig() {
return $this->config;
}
/**
* delete the default header based on header name
*
@@ -176,34 +184,6 @@ class ApiClient {
return $keyWithPrefix;
}
/**
* update header and query param based on authentication setting
*
* @param array $headerParams header parameters (by ref)
* @param array $queryParams query parameters (by ref)
* @param array $authSettings array of authentication scheme (e.g ['api_key'])
*/
public function updateParamsForAuth(&$headerParams, &$queryParams, $authSettings)
{
if (count($authSettings) == 0)
return;
// one endpoint can have more than 1 auth settings
foreach($authSettings as $auth) {
// determine which one to use
switch($auth) {
{{#authMethods}}
case '{{name}}':
{{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode($this->config->getUsername().":".$this->config->getPassword());{{/isBasic}}
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
break;
{{/authMethods}}
default:
//TODO show warning about security definition not found
}
}
}
/**
* @param string $resourcePath path to method endpoint
@@ -211,18 +191,13 @@ class ApiClient {
* @param array $queryParams parameters to be place in query URL
* @param array $postData parameters to be placed in POST body
* @param array $headerParams parameters to be place in request header
* @param array $authSettings parameters for authentication
* @throws \{{invokerNamespace}}\ApiException on a non 2xx response
* @return mixed
*/
public function callApi($resourcePath, $method, $queryParams, $postData,
$headerParams, $authSettings) {
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) {
$headers = array();
# determine authentication setting
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
# construct the http header
$headerParams = array_merge((array)$this->defaultHeaders, (array)$headerParams);

View File

@@ -126,15 +126,19 @@ class {{classname}} {
// for HTTP post (form)
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array({{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}});
{{#authMethods}}{{#isApiKey}}
$apiKey = $this->apiClient->getApiKeyWithPrefix('{{keyParamName}}');
if (isset($apiKey)) {
{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $apiKey;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $apiKey;{{/isKeyInQuery}}
}{{/isApiKey}}
{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode($this->apiClient->getConfig()->getUsername().":".$this->apiClient->getConfig()->getPassword());{{/isBasic}}
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
{{/authMethods}}
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams, $authSettings);
$headerParams);
} catch (ApiException $e) {
$throw = $e;
@@ -148,7 +152,7 @@ class {{classname}} {
throw $throw;
}
{{#returnType}}
if(!$response) {
if (!$response) {
return null;
}