Merge branch 'wing328-php_auth_setting' into develop_2.0

This commit is contained in:
Tony Tam 2015-05-20 22:46:02 -07:00
commit dd70a13c02
11 changed files with 291 additions and 80 deletions

View File

@ -221,6 +221,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
bundle.put("models", allModels); bundle.put("models", allModels);
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar)); bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
bundle.put("modelPackage", config.modelPackage()); bundle.put("modelPackage", config.modelPackage());
bundle.put("authMethods", config.fromSecurity(swagger.getSecurityDefinitions()));
if (swagger.getExternalDocs() != null) { if (swagger.getExternalDocs() != null) {
bundle.put("externalDocs", swagger.getExternalDocs()); bundle.put("externalDocs", swagger.getExternalDocs());
} }

View File

@ -84,6 +84,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("list", "array"); typeMapping.put("list", "array");
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json")); supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php"));
supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php")); supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php"));
supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php")); supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php"));
supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php")); supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php"));

View File

@ -38,13 +38,14 @@ class APIClient {
protected $user_agent = "PHP-Swagger"; protected $user_agent = "PHP-Swagger";
/** /**
* @param string $host the address of the API server * @param string $host Base url of the API server (optional)
* @param string $headerName a header to pass on requests
*/ */
function __construct($host, $headerName = null, $headerValue = null) { function __construct($host = null) {
if ($host == null) {
$this->host = '{{basePath}}';
} else {
$this->host = $host; $this->host = $host;
$this->headerName = $headerName; }
$this->headerValue = $headerValue;
} }
/** /**
@ -100,6 +101,47 @@ class APIClient {
$this->curl_timeout = $seconds; $this->curl_timeout = $seconds;
} }
/**
* Get API key (with prefix if set)
* @param string key name
* @return string API key with the prefix
*/
public function getApiKeyWithPrefix($apiKey) {
if (Configuration::$apiKeyPrefix[$apiKey]) {
return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey];
} else {
return Configuration::$apiKey[$apiKey];
}
}
/**
* update hearder 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(Configuration::$username.":".Configuration::$password);{{/isBasic}}
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
break;
{{/authMethods}}
default:
//TODO show warning about security definition not found
}
}
}
/** /**
* @param string $resourcePath path to method endpoint * @param string $resourcePath path to method endpoint
* @param string $method method to call * @param string $method method to call
@ -109,26 +151,22 @@ class APIClient {
* @return mixed * @return mixed
*/ */
public function callAPI($resourcePath, $method, $queryParams, $postData, public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) { $headerParams, $authSettings) {
$headers = array(); $headers = array();
# Allow API key from $headerParams to override default # determine authentication setting
$added_api_key = False; $this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
# construct the http header
if ($headerParams != null) { if ($headerParams != null) {
# add default header # add default header
$headerParams = array_merge((array)self::$default_header, $headerParams); $headerParams = array_merge((array)self::$default_header, $headerParams);
foreach ($headerParams as $key => $val) { foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val"; $headers[] = "$key: $val";
if ($key == $this->headerName) {
$added_api_key = True;
} }
} }
}
if (! $added_api_key && $this->headerName != null) {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
// form data // form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) { if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {

View File

@ -91,17 +91,19 @@ class {{classname}} {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array({{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}});
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
{{#returnType}}if(! $response) { {{#returnType}}if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'{{returnType}}');
'{{returnType}}');
return $responseObject;{{/returnType}} return $responseObject;{{/returnType}}
} }
{{/operation}} {{/operation}}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace {{invokerPackage}};
class Configuration {
public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
public static $PUT = "PUT";
public static $DELETE = "DELETE";
// authentication setting
public static $apiKey = array();
public static $apiKeyPrefix = array();
public static $username = '';
public static $password = '';
}

View File

@ -38,13 +38,14 @@ class APIClient {
protected $user_agent = "PHP-Swagger"; protected $user_agent = "PHP-Swagger";
/** /**
* @param string $host the address of the API server * @param string $host Base url of the API server (optional)
* @param string $headerName a header to pass on requests
*/ */
function __construct($host, $headerName = null, $headerValue = null) { function __construct($host = null) {
if ($host == null) {
$this->host = 'http://petstore.swagger.io/v2';
} else {
$this->host = $host; $this->host = $host;
$this->headerName = $headerName; }
$this->headerValue = $headerValue;
} }
/** /**
@ -100,6 +101,52 @@ class APIClient {
$this->curl_timeout = $seconds; $this->curl_timeout = $seconds;
} }
/**
* Get API key (with prefix if set)
* @param string key name
* @return string API key with the prefix
*/
public function getApiKeyWithPrefix($apiKey) {
if (Configuration::$apiKeyPrefix[$apiKey]) {
return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey];
} else {
return Configuration::$apiKey[$apiKey];
}
}
/**
* update hearder 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) {
case 'api_key':
$headerParams['api_key'] = $this->getApiKeyWithPrefix('api_key');
break;
case 'petstore_auth':
//TODO support oauth
break;
default:
//TODO show warning about security definition not found
}
}
}
/** /**
* @param string $resourcePath path to method endpoint * @param string $resourcePath path to method endpoint
* @param string $method method to call * @param string $method method to call
@ -109,26 +156,22 @@ class APIClient {
* @return mixed * @return mixed
*/ */
public function callAPI($resourcePath, $method, $queryParams, $postData, public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) { $headerParams, $authSettings) {
$headers = array(); $headers = array();
# Allow API key from $headerParams to override default # determine authentication setting
$added_api_key = False; $this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
# construct the http header
if ($headerParams != null) { if ($headerParams != null) {
# add default header # add default header
$headerParams = array_merge((array)self::$default_header, $headerParams); $headerParams = array_merge((array)self::$default_header, $headerParams);
foreach ($headerParams as $key => $val) { foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val"; $headers[] = "$key: $val";
if ($key == $this->headerName) {
$added_api_key = True;
} }
} }
}
if (! $added_api_key && $this->headerName != null) {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
// form data // form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) { if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace SwaggerClient;
class Configuration {
public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
public static $PUT = "PUT";
public static $DELETE = "DELETE";
// authentication setting
public static $apiKey = array();
public static $apiKeyPrefix = array();
public static $username = '';
public static $password = '';
}

View File

@ -72,10 +72,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -123,10 +126,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -173,17 +179,19 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'array[Pet]');
'array[Pet]');
return $responseObject; return $responseObject;
} }
@ -229,17 +237,19 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'array[Pet]');
'array[Pet]');
return $responseObject; return $responseObject;
} }
@ -291,17 +301,19 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('api_key', 'petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'Pet');
'Pet');
return $responseObject; return $responseObject;
} }
@ -361,10 +373,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -421,10 +436,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -485,10 +503,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }

View File

@ -67,17 +67,19 @@ class StoreApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('api_key');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'map[string,int]');
'map[string,int]');
return $responseObject; return $responseObject;
} }
@ -124,17 +126,19 @@ class StoreApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'Order');
'Order');
return $responseObject; return $responseObject;
} }
@ -186,17 +190,19 @@ class StoreApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'Order');
'Order');
return $responseObject; return $responseObject;
} }
@ -248,10 +254,13 @@ class StoreApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }

View File

@ -72,10 +72,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -123,10 +126,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -174,10 +180,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -228,17 +237,19 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'string');
'string');
return $responseObject; return $responseObject;
} }
@ -280,10 +291,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -336,17 +350,19 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'User');
'User');
return $responseObject; return $responseObject;
} }
@ -403,10 +419,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -459,10 +478,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }

View File

@ -53,13 +53,15 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
# test deleteDefaultHeader # test deleteDefaultHeader
SwaggerClient\APIClient::deleteDefaultHeader('test2'); SwaggerClient\APIClient::deleteDefaultHeader('test2');
$this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2'])); $this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2']));
} }
// test getPetById with a Pet object (id 10005) // test getPetById with a Pet object (id 10005)
public function testGetPetById() public function testGetPetById()
{ {
// initialize the API client // initialize the API client without host
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); $api_client = new SwaggerClient\APIClient();
SwaggerClient\Configuration::$apiKey['api_key'] = '111222333444555';
$pet_id = 10005; // ID of pet that needs to be fetched $pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new SwaggerClient\PetAPI($api_client); $pet_api = new SwaggerClient\PetAPI($api_client);
// return Pet (model) // return Pet (model)