From 4fd02b9f123ca7f531e9a83603afbca55d3d3900 Mon Sep 17 00:00:00 2001 From: davidwcarlson Date: Sat, 9 Feb 2019 06:51:44 -0800 Subject: [PATCH] fix 457 Support for Bearer Auth (#1930) * fix #457 by introducing an HttpBearerAuth object See https://github.com/OpenAPITools/openapi-generator/issues/457 Also https://github.com/OpenAPITools/openapi-generator/issues/1446 for typescript, https://github.com/OpenAPITools/openapi-generator/issues/1577 for python Specs defined as follows currently generate BasicAuth and send an "Authorization: Basic [base64Encode(username + ":" + password)]" header components: securitySchemes: bearer: type: http scheme: bearer This change will generate code which uses a new HttpBearerAuth class, which will send a "Authorization: [scheme] [accessToken]" header. This change is slightly larger and more impactful than simply using OAuth for bearerBearer, but it allows for scheme values other than bearer. This fix was enabled by the recent commit of https://github.com/OpenAPITools/openapi-generator/commit/80ca67cfdac0008282ef756cc069f275883a1c44 This PR is an alternative to https://github.com/OpenAPITools/openapi-generator/pull/1972 * update petstore samples * Update HttpBearerAuth mustache templates and samples * correct the expected number of generated java client files * update the retrofit2 HttpBearerAuth template and samples * Add resttemplate-specific HttpBearerAuth mustache and samples * add vertx-specific HttpBearerAuth template and samples * add java webclient-specific HttpBearerAuth template and samples --- .../codegen/languages/JavaClientCodegen.java | 1 + .../main/resources/Java/ApiClient.mustache | 20 ++++++- .../Java/auth/HttpBearerAuth.mustache | 45 +++++++++++++++ .../feign/auth/HttpBearerAuth.mustache | 43 ++++++++++++++ .../Java/libraries/jersey2/ApiClient.mustache | 20 ++++++- .../resttemplate/auth/HttpBearerAuth.mustache | 38 +++++++++++++ .../retrofit/auth/HttpBearerAuth.mustache | 42 ++++++++++++++ .../retrofit2/auth/HttpBearerAuth.mustache | 42 ++++++++++++++ .../vertx/auth/HttpBearerAuth.mustache | 39 +++++++++++++ .../webclient/auth/HttpBearerAuth.mustache | 39 +++++++++++++ .../codegen/java/JavaClientCodegenTest.java | 3 +- .../client/auth/HttpBearerAuth.java | 43 ++++++++++++++ .../client/auth/HttpBearerAuth.java | 43 ++++++++++++++ .../org/openapitools/client/ApiClient.java | 15 +++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 15 +++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 15 +++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 15 +++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../client/auth/HttpBearerAuth.java | 56 +++++++++++++++++++ .../client/auth/HttpBearerAuth.java | 38 +++++++++++++ .../client/auth/HttpBearerAuth.java | 38 +++++++++++++ .../client/auth/HttpBearerAuth.java | 42 ++++++++++++++ .../client/auth/HttpBearerAuth.java | 42 ++++++++++++++ .../client/auth/HttpBearerAuth.java | 42 ++++++++++++++ .../client/auth/HttpBearerAuth.java | 42 ++++++++++++++ .../client/auth/HttpBearerAuth.java | 50 +++++++++++++++++ .../client/auth/HttpBearerAuth.java | 39 +++++++++++++ 32 files changed, 1198 insertions(+), 5 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/retrofit/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/vertx/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/webclient/auth/HttpBearerAuth.mustache create mode 100644 samples/client/petstore/java/feign/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/feign10x/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/retrofit/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/retrofit2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/retrofit2rx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/retrofit2rx2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/vertx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java create mode 100644 samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index e26a541456aa..ea0154884f8c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -246,6 +246,7 @@ public void processOpts() { // google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer) if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) { supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java")); + supportingFiles.add(new SupportingFile("auth/HttpBearerAuth.mustache", authFolder, "HttpBearerAuth.java")); supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java")); // NOTE: below moved to postProcessOperationsWithModels //supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java")); diff --git a/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache index 722b51424f9b..91f479bd5819 100644 --- a/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache @@ -51,6 +51,7 @@ import java.text.DateFormat; import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.HttpBasicAuth; +import {{invokerPackage}}.auth.HttpBearerAuth; import {{invokerPackage}}.auth.ApiKeyAuth; {{#hasOAuthMethods}} import {{invokerPackage}}.auth.OAuth; @@ -102,8 +103,9 @@ public class ApiClient { setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}"); // Setup authentications (key: authentication name, value: authentication). - authentications = new HashMap();{{#authMethods}}{{#isBasic}} - authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} + authentications = new HashMap();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} + authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}} + authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} // Prevent the authentications from being modified. @@ -279,6 +281,20 @@ public class ApiClient { } {{/hasOAuthMethods}} + /** + * Helper method to set access token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Set the User-Agent header's value (by adding to the default header map). * @param userAgent User agent diff --git a/modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..fe30391d5474 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.mustache @@ -0,0 +1,45 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; + +import java.util.Map; +import java.util.List; + +{{>generatedAnnotation}} +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..2240a5518b55 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/HttpBearerAuth.mustache @@ -0,0 +1,43 @@ +package {{invokerPackage}}.auth; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +/** + * An interceptor that adds the request header needed to use HTTP bearer authentication. + */ +public class HttpBearerAuth implements RequestInterceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void apply(RequestTemplate template) { + if(bearerToken == null) { + return; + } + + template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 10e0ef6a0c1e..2de4af24edc0 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -53,6 +53,7 @@ import java.util.regex.Pattern; import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.HttpBasicAuth; +import {{invokerPackage}}.auth.HttpBearerAuth; import {{invokerPackage}}.auth.ApiKeyAuth; {{#hasOAuthMethods}} import {{invokerPackage}}.auth.OAuth; @@ -84,8 +85,9 @@ public class ApiClient { setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}"); // Setup authentications (key: authentication name, value: authentication). - authentications = new HashMap();{{#authMethods}}{{#isBasic}} - authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} + authentications = new HashMap();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} + authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}} + authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} // Prevent the authentications from being modified. @@ -193,6 +195,20 @@ public class ApiClient { } {{#hasOAuthMethods}} + /** + * Helper method to set bearer token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set access token for the first OAuth2 authentication. * @param accessToken Access token diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..0426b7a69841 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/auth/HttpBearerAuth.mustache @@ -0,0 +1,38 @@ +package {{invokerPackage}}.auth; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.util.Base64Utils; +import org.springframework.util.MultiValueMap; + +{{>generatedAnnotation}} +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/retrofit/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/retrofit/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..9b910c461cba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/retrofit/auth/HttpBearerAuth.mustache @@ -0,0 +1,42 @@ +package {{invokerPackage}}.auth; + +import java.io.IOException; + +import com.squareup.okhttp.Interceptor; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..da1bef57a529 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/auth/HttpBearerAuth.mustache @@ -0,0 +1,42 @@ +package {{invokerPackage}}.auth; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/vertx/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/vertx/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..3dfc23800a66 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/vertx/auth/HttpBearerAuth.mustache @@ -0,0 +1,39 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; +import io.vertx.core.MultiMap; +import java.util.Base64; +import java.nio.charset.StandardCharsets; +import java.util.List; + +{{>generatedAnnotation}} +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, MultiMap headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/auth/HttpBearerAuth.mustache new file mode 100644 index 000000000000..67d5c6c56d01 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/auth/HttpBearerAuth.mustache @@ -0,0 +1,39 @@ +package {{invokerPackage}}.auth; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.util.Base64Utils; +import org.springframework.util.MultiValueMap; + +{{>generatedAnnotation}} +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index 995f885cc19c..8debff2e7fb4 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -255,7 +255,7 @@ public void testGeneratePing() throws Exception { generator.opts(clientOptInput).generate(); Map generatedFiles = generator.getFiles(); - Assert.assertEquals(generatedFiles.size(), 33); + Assert.assertEquals(generatedFiles.size(), 34); ensureContainsFile(generatedFiles, output, ".gitignore"); ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore"); ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION"); @@ -281,6 +281,7 @@ public void testGeneratePing() throws Exception { ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/Authentication.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java"); + ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBearerAuth.java"); //ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuth.java"); //ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuthFlow.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Configuration.java"); diff --git a/samples/client/petstore/java/feign/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/feign/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..d4c9cbe6361e --- /dev/null +++ b/samples/client/petstore/java/feign/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,43 @@ +package org.openapitools.client.auth; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +/** + * An interceptor that adds the request header needed to use HTTP bearer authentication. + */ +public class HttpBearerAuth implements RequestInterceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void apply(RequestTemplate template) { + if(bearerToken == null) { + return; + } + + template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/feign10x/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/feign10x/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..d4c9cbe6361e --- /dev/null +++ b/samples/client/petstore/java/feign10x/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,43 @@ +package org.openapitools.client.auth; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +/** + * An interceptor that adds the request header needed to use HTTP bearer authentication. + */ +public class HttpBearerAuth implements RequestInterceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void apply(RequestTemplate template) { + if(bearerToken == null) { + return; + } + + template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/ApiClient.java index f6467f36e915..40c742bb921b 100644 --- a/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/ApiClient.java @@ -52,6 +52,7 @@ import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.HttpBasicAuth; +import org.openapitools.client.auth.HttpBearerAuth; import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.OAuth; @@ -269,6 +270,20 @@ public void setAccessToken(String accessToken) { throw new RuntimeException("No OAuth2 authentication configured!"); } + /** + * Helper method to set access token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Set the User-Agent header's value (by adding to the default header map). * @param userAgent User agent diff --git a/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/jersey1/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java index 5893b2b206ab..89c2a8f51dde 100644 --- a/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.HttpBasicAuth; +import org.openapitools.client.auth.HttpBearerAuth; import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.OAuth; @@ -184,6 +185,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to set bearer token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set access token for the first OAuth2 authentication. * @param accessToken Access token diff --git a/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 0b8adc59e8d1..81c14285e6b5 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -47,6 +47,7 @@ import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.HttpBasicAuth; +import org.openapitools.client.auth.HttpBearerAuth; import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.OAuth; @@ -185,6 +186,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to set bearer token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set access token for the first OAuth2 authentication. * @param accessToken Access token diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java index 0b8adc59e8d1..81c14285e6b5 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java @@ -47,6 +47,7 @@ import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.HttpBasicAuth; +import org.openapitools.client.auth.HttpBearerAuth; import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.OAuth; @@ -185,6 +186,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to set bearer token for the first Bearer authentication. + * @param bearerToken Bearer token + */ + public void setBearerToken(String bearerToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(bearerToken); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set access token for the first OAuth2 authentication. * @param accessToken Access token diff --git a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..22016b5bbeef --- /dev/null +++ b/samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,56 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; + +import java.util.Map; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + /** + * Gets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams) { + if(bearerToken == null) { + return; + } + + headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..5e6b713b8097 --- /dev/null +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,38 @@ +package org.openapitools.client.auth; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.util.Base64Utils; +import org.springframework.util.MultiValueMap; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..5e6b713b8097 --- /dev/null +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,38 @@ +package org.openapitools.client.auth; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.util.Base64Utils; +import org.springframework.util.MultiValueMap; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/retrofit/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/retrofit/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..308a6a688fda --- /dev/null +++ b/samples/client/petstore/java/retrofit/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,42 @@ +package org.openapitools.client.auth; + +import java.io.IOException; + +import com.squareup.okhttp.Interceptor; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/samples/client/petstore/java/retrofit2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/retrofit2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..9f8cfba44bff --- /dev/null +++ b/samples/client/petstore/java/retrofit2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,42 @@ +package org.openapitools.client.auth; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/samples/client/petstore/java/retrofit2rx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/retrofit2rx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..9f8cfba44bff --- /dev/null +++ b/samples/client/petstore/java/retrofit2rx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,42 @@ +package org.openapitools.client.auth; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/samples/client/petstore/java/retrofit2rx2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/retrofit2rx2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..9f8cfba44bff --- /dev/null +++ b/samples/client/petstore/java/retrofit2rx2/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,42 @@ +package org.openapitools.client.auth; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class HttpBearerAuth implements Interceptor { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + // If the request already have an authorization (eg. Basic auth), do nothing + if (request.header("Authorization") == null && bearerToken != null) { + request = request.newBuilder() + .addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) + .build(); + } + return chain.proceed(request); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +} diff --git a/samples/client/petstore/java/vertx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/vertx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..4f96a69ec648 --- /dev/null +++ b/samples/client/petstore/java/vertx/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,50 @@ +/* + * OpenAPI Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + +package org.openapitools.client.auth; + +import org.openapitools.client.Pair; +import io.vertx.core.MultiMap; +import java.util.Base64; +import java.nio.charset.StandardCharsets; +import java.util.List; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, MultiMap headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } +} diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java new file mode 100644 index 000000000000..0b829a1765a6 --- /dev/null +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -0,0 +1,39 @@ +package org.openapitools.client.auth; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.util.Base64Utils; +import org.springframework.util.MultiValueMap; + + +public class HttpBearerAuth implements Authentication { + private final String scheme; + private String bearerToken; + + public HttpBearerAuth(String scheme) { + this.scheme = scheme; + } + + public String getBearerToken() { + return bearerToken; + } + + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams) { + if (bearerToken == null) { + return; + } + headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); + } + + private static String upperCaseBearer(String scheme) { + return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; + } + +}