mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-07 11:36:11 +00:00
add authentication
This commit is contained in:
@@ -218,6 +218,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());
|
||||
}
|
||||
|
||||
@@ -67,6 +67,30 @@ class APIClient {
|
||||
$this->curl_timeout = $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headerParams
|
||||
* @param array $queryParams
|
||||
* @param array $authSettings
|
||||
*/
|
||||
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}}'] = Configuration::$api_key;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = Configuration::$api_key;{{/isKeyInQuery}}{{#isBasic}}$headerParams['Authorization'] = base64_encode(Configuraiton::$username.":"Configuration::$password){{/isBasic}}{{/isApiKey}}
|
||||
break;
|
||||
{{/authMethods}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $resourcePath path to method endpoint
|
||||
* @param string $method method to call
|
||||
@@ -76,7 +100,7 @@ class APIClient {
|
||||
* @return mixed
|
||||
*/
|
||||
public function callAPI($resourcePath, $method, $queryParams, $postData,
|
||||
$headerParams) {
|
||||
$headerParams, $authSettings) {
|
||||
|
||||
$headers = array();
|
||||
|
||||
|
||||
@@ -92,18 +92,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}}
|
||||
|
||||
Reference in New Issue
Block a user