From fb7aa580bf679b101a9d1ad1bc2ff436fb1327f4 Mon Sep 17 00:00:00 2001 From: wandi34 Date: Wed, 26 Feb 2025 10:05:50 +0100 Subject: [PATCH] [Java][RestClient] Enable access token refresh (#20733) --- README.md | 1 + .../Java/libraries/restclient/ApiClient.mustache | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ .../java/org/openapitools/client/ApiClient.java | 16 ++++++++++++++++ 8 files changed, 113 insertions(+) diff --git a/README.md b/README.md index e7007203571..0f48f825145 100644 --- a/README.md +++ b/README.md @@ -631,6 +631,7 @@ Here are some companies/projects (alphabetical order) using OpenAPI Generator in - [act coding](https://github.com/actcoding) - [Adaptant Solutions AG](https://www.adaptant.io/) - [adesso SE](https://www.adesso.de/) +- [adorsys GmbH & Co.KG](https://adorsys.com/) - [Adyen](https://www.adyen.com/) - [Agoda](https://www.agoda.com/) - [Airthings](https://www.airthings.com/) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache index 99ea8a86127..e5a4bd8fcae 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache @@ -44,6 +44,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import {{javaxPackage}}.annotation.Nullable; @@ -240,6 +241,21 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index c004d27d1b1..faa1b85b0ce 100644 --- a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -222,6 +223,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 81704b3342a..188b9cd1bdf 100644 --- a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -220,6 +221,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index ed7fa64a2b3..ea8ee766581 100644 --- a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -220,6 +221,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index 34bbdedcb2e..3bf43f86476 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -226,6 +227,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index 34bbdedcb2e..3bf43f86476 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -226,6 +227,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index 34bbdedcb2e..3bf43f86476 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; +import java.util.function.Supplier; import jakarta.annotation.Nullable; @@ -226,6 +227,21 @@ public class ApiClient extends JavaTimeFormatter { throw new RuntimeException("No Bearer authentication configured!"); } + /** + * Helper method to set the supplier of access tokens for Bearer authentication. + * + * @param tokenSupplier the token supplier function + */ + public void setBearerToken(Supplier tokenSupplier) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBearerAuth) { + ((HttpBearerAuth) auth).setBearerToken(tokenSupplier); + return; + } + } + throw new RuntimeException("No Bearer authentication configured!"); + } + /** * Helper method to set username for the first HTTP basic authentication. * @param username the username