From 1d47ca69feaa49ab62d6fe422543bf2fccd883d8 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 20 Nov 2015 17:36:17 +0800 Subject: [PATCH] update perl auth to skip null apikey/username&password --- .../main/resources/perl/ApiClient.mustache | 20 ++++++++++++++++--- .../perl/lib/WWW/SwaggerClient/ApiClient.pm | 12 ++++++++--- .../perl/lib/WWW/SwaggerClient/Role.pm | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache index bf1220912d2..1623721cc70 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache @@ -317,14 +317,28 @@ sub update_params_for_auth { foreach my $auth (@$auth_settings) { # determine which one to use if (!defined($auth)) { + # TODO show warning about auth setting not defined } {{#authMethods}}elsif ($auth eq '{{name}}') { - {{#isApiKey}}{{#isKeyInHeader}}$header_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$query_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$header_params->{'Authorization'} = 'Basic '.encode_base64($WWW::{{moduleName}}::Configuration::username.":".$WWW::{{moduleName}}::Configuration::password);{{/isBasic}} - {{#isOAuth}}$header_params->{'Authorization'} = 'Bearer ' . $WWW::{{moduleName}}::Configuration::access_token;{{/isOAuth}} + {{#isApiKey}}{{#isKeyInHeader}} + my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}'); + if ($api_key) { + $header_params->{'{{keyParamName}}'} = $api_key; + }{{/isKeyInHeader}}{{#isKeyInQuery}} + my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}'); + if ($api_key) { + $query_params->{'{{keyParamName}}'} = $api_key; + }{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}} + if ($WWW::{{moduleName}}::Configuration::username || $WWW::{{moduleName}}::Configuration::password) { + $header_params->{'Authorization'} = 'Basic ' . encode_base64($WWW::{{moduleName}}::Configuration::username . ":" . $WWW::{{moduleName}}::Configuration::password); + }{{/isBasic}}{{#isOAuth}} + if ($WWW::{{moduleName}}::Configuration::access_token) { + $header_params->{'Authorization'} = 'Bearer ' . $WWW::{{moduleName}}::Configuration::access_token; + }{{/isOAuth}} } {{/authMethods}} else { - # TODO show warning about security definition not found + # TODO show warning about security definition not found } } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index dbd822cc1d4..11f2ab3f236 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -317,18 +317,24 @@ sub update_params_for_auth { foreach my $auth (@$auth_settings) { # determine which one to use if (!defined($auth)) { + # TODO show warning about auth setting not defined } elsif ($auth eq 'api_key') { - $header_params->{'api_key'} = $self->get_api_key_with_prefix('api_key'); + my $api_key = $self->get_api_key_with_prefix('api_key'); + if ($api_key) { + $header_params->{'api_key'} = $api_key; + } } elsif ($auth eq 'petstore_auth') { - $header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token; + if ($WWW::SwaggerClient::Configuration::access_token) { + $header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token; + } } else { - # TODO show warning about security definition not found + # TODO show warning about security definition not found } } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm index feb5228cb61..941d9e0ce24 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm @@ -37,7 +37,7 @@ has version_info => ( is => 'ro', default => sub { { app_name => 'Swagger Petstore', app_version => '1.0.0', - generated_date => '2015-11-13T20:46:43.271Z', + generated_date => '2015-11-20T17:35:18.902+08:00', generator_class => 'class io.swagger.codegen.languages.PerlClientCodegen', } }, documentation => 'Information about the application version and the codegen codebase version' @@ -103,7 +103,7 @@ Automatically generated by the Perl Swagger Codegen project: =over 4 -=item Build date: 2015-11-13T20:46:43.271Z +=item Build date: 2015-11-20T17:35:18.902+08:00 =item Build package: class io.swagger.codegen.languages.PerlClientCodegen