diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java index 5d2f55ac047..5fc6a92bba7 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java @@ -221,6 +221,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { bundle.put("models", allModels); bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar)); bundle.put("modelPackage", config.modelPackage()); + bundle.put("authMethods", config.fromSecurity(swagger.getSecurityDefinitions())); if (swagger.getExternalDocs() != null) { bundle.put("externalDocs", swagger.getExternalDocs()); } diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java index 48d4a86b09d..55d358154f8 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java @@ -84,6 +84,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("list", "array"); 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("APIClientException.mustache", packagePath + "/lib", "APIClientException.php")); supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php")); diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index cf3d1fb9372..b8b8eff4295 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -38,13 +38,14 @@ class APIClient { protected $user_agent = "PHP-Swagger"; /** - * @param string $host the address of the API server - * @param string $headerName a header to pass on requests + * @param string $host Base url of the API server (optional) */ - function __construct($host, $headerName = null, $headerValue = null) { - $this->host = $host; - $this->headerName = $headerName; - $this->headerValue = $headerValue; + function __construct($host = null) { + if ($host == null) { + $this->host = '{{basePath}}'; + } else { + $this->host = $host; + } } /** @@ -100,6 +101,47 @@ class APIClient { $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 $method method to call @@ -109,26 +151,22 @@ class APIClient { * @return mixed */ public function callAPI($resourcePath, $method, $queryParams, $postData, - $headerParams) { + $headerParams, $authSettings) { $headers = array(); - # Allow API key from $headerParams to override default - $added_api_key = False; + # determine authentication setting + $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); + + # construct the http header if ($headerParams != null) { # add default header $headerParams = array_merge((array)self::$default_header, $headerParams); foreach ($headerParams as $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 if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) { diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index f0100defffe..98be8ff3a5c 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -91,18 +91,20 @@ class {{classname}} { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array({{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); {{#returnType}}if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - '{{returnType}}'); - return $responseObject;{{/returnType}} + $responseObject = $this->apiClient->deserialize($response,'{{returnType}}'); + return $responseObject;{{/returnType}} } {{/operation}} {{newline}} diff --git a/modules/swagger-codegen/src/main/resources/php/configuration.mustache b/modules/swagger-codegen/src/main/resources/php/configuration.mustache new file mode 100644 index 00000000000..9af553702ae --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/php/configuration.mustache @@ -0,0 +1,36 @@ +host = $host; - $this->headerName = $headerName; - $this->headerValue = $headerValue; + function __construct($host = null) { + if ($host == null) { + $this->host = 'http://petstore.swagger.io/v2'; + } else { + $this->host = $host; + } } /** @@ -100,6 +101,52 @@ class APIClient { $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 $method method to call @@ -109,26 +156,22 @@ class APIClient { * @return mixed */ public function callAPI($resourcePath, $method, $queryParams, $postData, - $headerParams) { + $headerParams, $authSettings) { $headers = array(); - # Allow API key from $headerParams to override default - $added_api_key = False; + # determine authentication setting + $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); + + # construct the http header if ($headerParams != null) { # add default header $headerParams = array_merge((array)self::$default_header, $headerParams); foreach ($headerParams as $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 if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php new file mode 100644 index 00000000000..9426efa9170 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php @@ -0,0 +1,36 @@ +apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -123,10 +126,13 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -173,18 +179,20 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'array[Pet]'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'array[Pet]'); + return $responseObject; } /** @@ -229,18 +237,20 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'array[Pet]'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'array[Pet]'); + return $responseObject; } /** @@ -291,18 +301,20 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('api_key', 'petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'Pet'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'Pet'); + return $responseObject; } /** @@ -361,10 +373,13 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -421,10 +436,13 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -485,10 +503,13 @@ class PetApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('petstore_auth'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php index ab43d730f6a..609e6568ed4 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php @@ -67,18 +67,20 @@ class StoreApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array('api_key'); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'map[string,int]'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'map[string,int]'); + return $responseObject; } /** @@ -124,18 +126,20 @@ class StoreApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'Order'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'Order'); + return $responseObject; } /** @@ -186,18 +190,20 @@ class StoreApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'Order'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'Order'); + return $responseObject; } /** @@ -248,10 +254,13 @@ class StoreApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php index 90e35bca4ad..985ed22a40c 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php @@ -72,10 +72,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -123,10 +126,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -174,10 +180,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -228,18 +237,20 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'string'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'string'); + return $responseObject; } /** @@ -280,10 +291,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -336,18 +350,20 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); if(! $response) { return null; } - $responseObject = $this->apiClient->deserialize($response, - 'User'); - return $responseObject; + $responseObject = $this->apiClient->deserialize($response,'User'); + return $responseObject; } /** @@ -403,10 +419,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } @@ -459,10 +478,13 @@ class UserApi { $httpBody = $formParams; } + // authentication setting, if any + $authSettings = array(); + // make the API Call $response = $this->apiClient->callAPI($resourcePath, $method, $queryParams, $httpBody, - $headerParams); + $headerParams, $authSettings); } diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 4751c29dd78..bf941756fa6 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -53,13 +53,15 @@ class PetApiTest extends \PHPUnit_Framework_TestCase # test deleteDefaultHeader SwaggerClient\APIClient::deleteDefaultHeader('test2'); $this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2'])); + } // test getPetById with a Pet object (id 10005) public function testGetPetById() { - // initialize the API client - $api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); + // initialize the API client without host + $api_client = new SwaggerClient\APIClient(); + SwaggerClient\Configuration::$apiKey['api_key'] = '111222333444555'; $pet_id = 10005; // ID of pet that needs to be fetched $pet_api = new SwaggerClient\PetAPI($api_client); // return Pet (model)