From c9c8b99bcd0658bae57fd415e2c7e04e7c42dbee Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 19 Apr 2020 15:26:57 +0800 Subject: [PATCH] add bearer auth support to C# 2.0 client --- bin/csharp-dotnet2-petstore.sh | 2 +- bin/windows/csharp-dotnet2-petstore.bat | 2 +- .../csharp-dotnet2/ApiClient.mustache | 21 +++++++++- .../csharp-dotnet2/Configuration.mustache | 6 +++ .../resources/csharp-dotnet2/README.mustache | 41 +++++++++++++++---- .../resources/csharp-dotnet2/api_doc.mustache | 24 ++++++++--- .../Lib/OpenAPIClient/README.md | 1 + .../Lib/OpenAPIClient/docs/PetApi.md | 8 ---- .../Lib/OpenAPIClient/docs/StoreApi.md | 4 -- .../Lib/OpenAPIClient/docs/UserApi.md | 8 ---- .../Org/OpenAPITools/Client/ApiClient.cs | 4 +- .../Org/OpenAPITools/Client/Configuration.cs | 6 +++ 12 files changed, 86 insertions(+), 41 deletions(-) diff --git a/bin/csharp-dotnet2-petstore.sh b/bin/csharp-dotnet2-petstore.sh index d3641ed1ce55..c953170bfaa0 100755 --- a/bin/csharp-dotnet2-petstore.sh +++ b/bin/csharp-dotnet2-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient --additional-properties hideGenerationTimestamp=true $@" +ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -t modules/openapi-generator/src/main/resources/csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient --additional-properties hideGenerationTimestamp=true $@" java $JAVA_OPTS -jar $executable $ags diff --git a/bin/windows/csharp-dotnet2-petstore.bat b/bin/windows/csharp-dotnet2-petstore.bat index 6951617bed9a..f5bc77332e65 100755 --- a/bin/windows/csharp-dotnet2-petstore.bat +++ b/bin/windows/csharp-dotnet2-petstore.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenApiClientTest/Lib/OpenApiClient --additional-properties hideGenerationTimestamp=true +set ags=generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -t modules/openapi-generator/src/main/resources/csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenApiClientTest/Lib/OpenApiClient --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/modules/openapi-generator/src/main/resources/csharp-dotnet2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-dotnet2/ApiClient.mustache index c382839d8984..1938e277989a 100644 --- a/modules/openapi-generator/src/main/resources/csharp-dotnet2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-dotnet2/ApiClient.mustache @@ -261,8 +261,25 @@ namespace {{clientPackage}} { {{#authMethods}} case "{{name}}": - {{#isApiKey}}{{#isKeyInHeader}}headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);{{/isBasic}} - {{#isOAuth}}//TODO support oauth{{/isOAuth}} + {{#isApiKey}} + {{#isKeyInHeader}} + headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}"); + {{/isKeyInHeader}} + {{#isKeyInQuery}} + queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}"); + {{/isKeyInQuery}} + {{/isApiKey}} + {{#isBasic}} + {{#isBasicBasic}} + headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password); + {{/isBasicBasic}} + {{#isBasicBearer}} + headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; + {{/isBasicBearer}} + {{/isBasic}} + {{#isOAuth}} + headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; + {{/isOAuth}} break; {{/authMethods}} default: diff --git a/modules/openapi-generator/src/main/resources/csharp-dotnet2/Configuration.mustache b/modules/openapi-generator/src/main/resources/csharp-dotnet2/Configuration.mustache index e09f39fc601e..77f755d61144 100644 --- a/modules/openapi-generator/src/main/resources/csharp-dotnet2/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-dotnet2/Configuration.mustache @@ -37,6 +37,12 @@ namespace {{clientPackage}} /// The password. public static String Password { get; set; } + /// + /// Gets or sets the access token (Bearer/OAuth authentication). + /// + /// The access token. + public static String AccessToken { get; set; } + /// /// Gets or sets the API key based on the authentication name. /// diff --git a/modules/openapi-generator/src/main/resources/csharp-dotnet2/README.mustache b/modules/openapi-generator/src/main/resources/csharp-dotnet2/README.mustache index 041dec963b05..0d8dfd814d44 100644 --- a/modules/openapi-generator/src/main/resources/csharp-dotnet2/README.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-dotnet2/README.mustache @@ -56,16 +56,31 @@ namespace Example { public void main() { - {{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} + {{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}} + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} + {{#isBasicBasic}} // Configure HTTP basic authorization: {{{name}}} Configuration.Default.Username = "YOUR_USERNAME"; - Configuration.Default.Password = "YOUR_PASSWORD";{{/isBasic}}{{#isApiKey}} + Configuration.Default.Password = "YOUR_PASSWORD"; + {{/isBasicBasic}} + {{#isBasicBearer}} + // Configure Bearer access token for authorization: {{{name}}} + Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; + {{/isBasicBearer}} + {{/isBasic}} + {{#isApiKey}} // Configure API key authorization: {{{name}}} Configuration.Default.ApiKey.Add("{{{keyParamName}}}", "YOUR_API_KEY"); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed - // Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");{{/isApiKey}}{{#isOAuth}} + // Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer"); + {{/isApiKey}} + {{#isOAuth}} // Configure OAuth2 access token for authorization: {{{name}}} - Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";{{/isOAuth}}{{/authMethods}} + Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; + {{/isOAuth}} + {{/authMethods}} {{/hasAuthMethods}} var apiInstance = new {{classname}}(); @@ -123,21 +138,29 @@ No model defined in this package All endpoints do not require authorization. {{/authMethods}} {{#authMethods}} -{{#last}} +{{#-last}} Authentication schemes defined for the API: -{{/last}} +{{/-last}} {{/authMethods}} {{#authMethods}} ### {{name}} -{{#isApiKey}}- **Type**: API key +{{#isApiKey}} +- **Type**: API key - **API key parameter name**: {{keyParamName}} - **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}} {{/isApiKey}} -{{#isBasic}}- **Type**: HTTP basic authentication +{{#isBasic}} +{{#isBasicBasic}} +- **Type**: HTTP basic authentication +{{/isBasicBasic}} +{{#isBasicBearer}} +- **Type**: HTTP bearer authentication +{{/isBasicBearer}} {{/isBasic}} -{{#isOAuth}}- **Type**: OAuth +{{#isOAuth}} +- **Type**: OAuth - **Flow**: {{flow}} - **Authorization URL**: {{authorizationUrl}} - **Scopes**: {{^scopes}}N/A{{/scopes}} diff --git a/modules/openapi-generator/src/main/resources/csharp-dotnet2/api_doc.mustache b/modules/openapi-generator/src/main/resources/csharp-dotnet2/api_doc.mustache index 1a7727618947..325e1426014a 100644 --- a/modules/openapi-generator/src/main/resources/csharp-dotnet2/api_doc.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-dotnet2/api_doc.mustache @@ -32,18 +32,32 @@ namespace Example { public void main() { - {{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} + {{#isBasicBasic}} // Configure HTTP basic authorization: {{{name}}} Configuration.Default.Username = "YOUR_USERNAME"; - Configuration.Default.Password = "YOUR_PASSWORD";{{/isBasic}}{{#isApiKey}} + Configuration.Default.Password = "YOUR_PASSWORD"; + {{/isBasicBasic}} + {{#isBasicBearer}} + // Configure Bearer access token: {{{name}}} + Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; + {{/isBasicBearer}} + {{/isBasic}} + {{#isApiKey}} // Configure API key authorization: {{{name}}} Configuration.Default.ApiKey.Add("{{{keyParamName}}}", "YOUR_API_KEY"); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed - // Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");{{/isApiKey}}{{#isOAuth}} + // Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer"); + {{/isApiKey}} + {{#isOAuth}} // Configure OAuth2 access token for authorization: {{{name}}} - Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";{{/isOAuth}}{{/authMethods}} - {{/hasAuthMethods}} + Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; + {{/isOAuth}} + {{/authMethods}} + {{/hasAuthMethods}} var apiInstance = new {{classname}}(); {{#allParams}} {{#isPrimitiveType}} diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/README.md b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/README.md index b68ca22592ad..5296503208aa 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/README.md +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/README.md @@ -112,6 +112,7 @@ Class | Method | HTTP request | Description ## Documentation for Authorization +Authentication schemes defined for the API: ### api_key diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/PetApi.md b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/PetApi.md index 3ce062d25aa1..373f0348fe7c 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/PetApi.md +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/PetApi.md @@ -34,7 +34,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -96,7 +95,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -162,7 +160,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -227,7 +224,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -292,7 +288,6 @@ namespace Example { public void main() { - // Configure API key authorization: api_key Configuration.Default.ApiKey.Add("api_key", "YOUR_API_KEY"); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed @@ -357,7 +352,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -419,7 +413,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; @@ -485,7 +478,6 @@ namespace Example { public void main() { - // Configure OAuth2 access token for authorization: petstore_auth Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN"; diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/StoreApi.md b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/StoreApi.md index 9ee43d09cc19..068cb5419f84 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/StoreApi.md +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/StoreApi.md @@ -32,7 +32,6 @@ namespace Example { public void main() { - var apiInstance = new StoreApi(); var orderId = orderId_example; // string | ID of the order that needs to be deleted @@ -93,7 +92,6 @@ namespace Example { public void main() { - // Configure API key authorization: api_key Configuration.Default.ApiKey.Add("api_key", "YOUR_API_KEY"); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed @@ -156,7 +154,6 @@ namespace Example { public void main() { - var apiInstance = new StoreApi(); var orderId = 789; // long? | ID of pet that needs to be fetched @@ -216,7 +213,6 @@ namespace Example { public void main() { - var apiInstance = new StoreApi(); var body = new Order(); // Order | order placed for purchasing the pet diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/UserApi.md b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/UserApi.md index b9909cad575b..74ca98353aa7 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/UserApi.md +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/UserApi.md @@ -36,7 +36,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var body = new User(); // User | Created user object @@ -95,7 +94,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var body = new List(); // List | List of user object @@ -154,7 +152,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var body = new List(); // List | List of user object @@ -215,7 +212,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var username = username_example; // string | The name that needs to be deleted @@ -274,7 +270,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var username = username_example; // string | The name that needs to be fetched. Use user1 for testing. @@ -334,7 +329,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var username = username_example; // string | The user name for login var password = password_example; // string | The password for login in clear text @@ -396,7 +390,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); try @@ -453,7 +446,6 @@ namespace Example { public void main() { - var apiInstance = new UserApi(); var username = username_example; // string | name that need to be deleted var body = new User(); // User | Updated user object diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/ApiClient.cs index 934ae82628d4..e1d91307dcfe 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/ApiClient.cs @@ -261,11 +261,9 @@ public void UpdateParamsForAuth(Dictionary queryParams, Dictiona { case "api_key": headerParams["api_key"] = GetApiKeyWithPrefix("api_key"); - break; case "petstore_auth": - - //TODO support oauth + headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; break; default: //TODO show warning about security definition not found diff --git a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/Configuration.cs b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/Configuration.cs index 7546607fa992..10bd0088bb74 100644 --- a/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/Configuration.cs +++ b/samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/Configuration.cs @@ -37,6 +37,12 @@ public class Configuration /// The password. public static String Password { get; set; } + /// + /// Gets or sets the access token (Bearer/OAuth authentication). + /// + /// The access token. + public static String AccessToken { get; set; } + /// /// Gets or sets the API key based on the authentication name. ///