From c06a21e63806546562d82d9e5e15f0060d0d3c1c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 15 Nov 2021 16:34:27 +0800 Subject: [PATCH] update authentication to include more parameters (#10858) --- .../libraries/okhttp-gson/ApiClient.mustache | 45 +++++++++--- .../okhttp-gson/auth/ApiKeyAuth.mustache | 69 +++++++++++++++++++ .../okhttp-gson/auth/Authentication.mustache | 25 +++++++ .../okhttp-gson/auth/HttpBasicAuth.mustache | 5 +- .../okhttp-gson/auth/HttpBearerAuth.mustache | 52 ++++++++++++++ .../libraries/okhttp-gson/auth/OAuth.mustache | 31 +++++++++ .../okhttp-gson/auth/RetryingOAuth.mustache | 5 +- .../org/openapitools/client/ApiClient.java | 45 +++++++++--- .../openapitools/client/auth/ApiKeyAuth.java | 5 +- .../client/auth/Authentication.java | 8 ++- .../client/auth/HttpBasicAuth.java | 5 +- .../client/auth/HttpBearerAuth.java | 7 +- .../org/openapitools/client/ApiClient.java | 45 +++++++++--- .../openapitools/client/auth/ApiKeyAuth.java | 5 +- .../client/auth/Authentication.java | 8 ++- .../client/auth/HttpBasicAuth.java | 5 +- .../client/auth/HttpBearerAuth.java | 7 +- .../org/openapitools/client/auth/OAuth.java | 5 +- .../client/auth/RetryingOAuth.java | 5 +- .../org/openapitools/client/ApiClient.java | 45 +++++++++--- .../openapitools/client/auth/ApiKeyAuth.java | 5 +- .../client/auth/Authentication.java | 8 ++- .../client/auth/HttpBasicAuth.java | 5 +- .../client/auth/HttpBearerAuth.java | 7 +- .../org/openapitools/client/auth/OAuth.java | 5 +- .../client/auth/RetryingOAuth.java | 5 +- .../org/openapitools/client/ApiClient.java | 45 +++++++++--- .../openapitools/client/auth/ApiKeyAuth.java | 5 +- .../client/auth/Authentication.java | 8 ++- .../client/auth/HttpBasicAuth.java | 5 +- .../client/auth/HttpBearerAuth.java | 7 +- .../org/openapitools/client/auth/OAuth.java | 5 +- .../client/auth/RetryingOAuth.java | 5 +- .../client/auth/ApiKeyAuthTest.java | 25 +++---- .../client/auth/HttpBasicAuthTest.java | 11 +-- 35 files changed, 488 insertions(+), 90 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/ApiKeyAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/Authentication.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBearerAuth.mustache create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuth.mustache diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index 5b765e5501a..9ce74912ba2 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -15,6 +15,7 @@ import okhttp3.internal.http.HttpMethod; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import okio.Buffer; import okio.BufferedSink; import okio.Okio; {{#joda}} @@ -1326,16 +1327,12 @@ public class ApiClient { List allQueryParams = new ArrayList(queryParams); allQueryParams.addAll(collectionQueryParams); - // update parameters with authentication settings - updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams); - final String url = buildUrl(path, queryParams, collectionQueryParams); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(headerParams, reqBuilder); - processCookieParams(cookieParams, reqBuilder); - String contentType = (String) headerParams.get("Content-Type"); + // prepare HTTP request body RequestBody reqBody; + String contentType = headerParams.get("Content-Type"); + if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { @@ -1354,6 +1351,13 @@ public class ApiClient { reqBody = serialize(body, contentType); } + // update parameters with authentication settings + updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url)); + + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + processCookieParams(cookieParams, reqBuilder); + // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); @@ -1461,13 +1465,14 @@ public class ApiClient { * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters */ - public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, Map cookieParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, + Map cookieParams, String payload, String method, URI uri) throws ApiException { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RuntimeException("Authentication undefined: " + authName); } - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, payload, method, uri); } } @@ -1690,4 +1695,26 @@ public class ApiClient { return path; } {{/dynamicOperations}} + + /** + * Convert the HTTP request body to a string. + * + * @param request The HTTP request object + * @return The string representation of the HTTP request body + * @throws org.openapitools.client.ApiException If fail to serialize the request body object into a string + */ + private String requestBodyToString(RequestBody requestBody) throws ApiException { + if (requestBody != null) { + try { + final Buffer buffer = new Buffer(); + requestBody.writeTo(buffer); + return buffer.readUtf8(); + } catch (final IOException e) { + throw new ApiException(e); + } + } + + // empty http request body + return ""; + } } diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/ApiKeyAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/ApiKeyAuth.mustache new file mode 100644 index 00000000000..a0dda669a89 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/ApiKeyAuth.mustache @@ -0,0 +1,69 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.ApiException; +import {{invokerPackage}}.Pair; + +import java.net.URI; +import java.util.Map; +import java.util.List; + +{{>generatedAnnotation}} +public class ApiKeyAuth implements Authentication { + private final String location; + private final String paramName; + + private String apiKey; + private String apiKeyPrefix; + + public ApiKeyAuth(String location, String paramName) { + this.location = location; + this.paramName = paramName; + } + + public String getLocation() { + return location; + } + + public String getParamName() { + return paramName; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public String getApiKeyPrefix() { + return apiKeyPrefix; + } + + public void setApiKeyPrefix(String apiKeyPrefix) { + this.apiKeyPrefix = apiKeyPrefix; + } + + @Override + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (apiKey == null) { + return; + } + String value; + if (apiKeyPrefix != null) { + value = apiKeyPrefix + " " + apiKey; + } else { + value = apiKey; + } + if ("query".equals(location)) { + queryParams.add(new Pair(paramName, value)); + } else if ("header".equals(location)) { + headerParams.put(paramName, value); + } else if ("cookie".equals(location)) { + cookieParams.put(paramName, value); + } + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/Authentication.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/Authentication.mustache new file mode 100644 index 00000000000..c4ad338c793 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/Authentication.mustache @@ -0,0 +1,25 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; +import {{invokerPackage}}.ApiException; + +import java.net.URI; +import java.util.Map; +import java.util.List; + +public interface Authentication { + /** + * Apply authentication settings to header and query params. + * + * @param queryParams List of query parameters + * @param headerParams Map of header parameters + * @param cookieParams Map of cookie parameters + * @param payload HTTP request body + * @param method HTTP method + * @param uri URI + * @throws ApiException if failed to update the parameters + */ + void applyToParams(List queryParams, Map headerParams, Map cookieParams, String payload, String method, URI uri) throws ApiException; +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBasicAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBasicAuth.mustache index 9cff50a7475..417a89e34cc 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBasicAuth.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBasicAuth.mustache @@ -3,9 +3,11 @@ package {{invokerPackage}}.auth; import {{invokerPackage}}.Pair; +import {{invokerPackage}}.ApiException; import okhttp3.Credentials; +import java.net.URI; import java.util.Map; import java.util.List; @@ -32,7 +34,8 @@ public class HttpBasicAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (username == null && password == null) { return; } diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBearerAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBearerAuth.mustache new file mode 100644 index 00000000000..c8a9fce2965 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/HttpBearerAuth.mustache @@ -0,0 +1,52 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.ApiException; +import {{invokerPackage}}.Pair; + +import java.net.URI; +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. + * + * @return The bearer token + */ + public String getBearerToken() { + return bearerToken; + } + + /** + * Sets the token, which together with the scheme, will be sent as the value of the Authorization header. + * + * @param bearerToken The bearer token to send in the Authorization header + */ + public void setBearerToken(String bearerToken) { + this.bearerToken = bearerToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + 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/okhttp-gson/auth/OAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuth.mustache new file mode 100644 index 00000000000..34d35985718 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/OAuth.mustache @@ -0,0 +1,31 @@ +{{>licenseInfo}} + +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; +import {{invokerPackage}}.ApiException; + +import java.net.URI; +import java.util.Map; +import java.util.List; + +{{>generatedAnnotation}} +public class OAuth implements Authentication { + private String accessToken; + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + @Override + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (accessToken != null) { + headerParams.put("Authorization", "Bearer " + accessToken); + } + } +} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/RetryingOAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/RetryingOAuth.mustache index 6e6562448da..137e266b5a2 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/RetryingOAuth.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/auth/RetryingOAuth.mustache @@ -1,6 +1,7 @@ {{#hasOAuthMethods}} package {{invokerPackage}}.auth; +import {{invokerPackage}}.ApiException; import {{invokerPackage}}.Pair; import okhttp3.Interceptor; @@ -19,6 +20,7 @@ import org.apache.oltu.oauth2.common.message.types.GrantType; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; import java.util.Map; import java.util.List; @@ -174,7 +176,8 @@ public class RetryingOAuth extends OAuth implements Interceptor { // Applying authorization to parameters is performed in the retryingIntercept method @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { // No implementation necessary } } diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java index b00b2a63547..99ca36f391b 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java @@ -18,6 +18,7 @@ import okhttp3.internal.http.HttpMethod; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import okio.Buffer; import okio.BufferedSink; import okio.Okio; import org.threeten.bp.LocalDate; @@ -1128,16 +1129,12 @@ public class ApiClient { List allQueryParams = new ArrayList(queryParams); allQueryParams.addAll(collectionQueryParams); - // update parameters with authentication settings - updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams); - final String url = buildUrl(path, queryParams, collectionQueryParams); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(headerParams, reqBuilder); - processCookieParams(cookieParams, reqBuilder); - String contentType = (String) headerParams.get("Content-Type"); + // prepare HTTP request body RequestBody reqBody; + String contentType = headerParams.get("Content-Type"); + if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { @@ -1156,6 +1153,13 @@ public class ApiClient { reqBody = serialize(body, contentType); } + // update parameters with authentication settings + updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url)); + + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + processCookieParams(cookieParams, reqBuilder); + // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); @@ -1263,13 +1267,14 @@ public class ApiClient { * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters */ - public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, Map cookieParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, + Map cookieParams, String payload, String method, URI uri) throws ApiException { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RuntimeException("Authentication undefined: " + authName); } - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, payload, method, uri); } } @@ -1421,4 +1426,26 @@ public class ApiClient { throw new AssertionError(e); } } + + /** + * Convert the HTTP request body to a string. + * + * @param request The HTTP request object + * @return The string representation of the HTTP request body + * @throws org.openapitools.client.ApiException If fail to serialize the request body object into a string + */ + private String requestBodyToString(RequestBody requestBody) throws ApiException { + if (requestBody != null) { + try { + final Buffer buffer = new Buffer(); + requestBody.writeTo(buffer); + return buffer.readUtf8(); + } catch (final IOException e) { + throw new ApiException(e); + } + } + + // empty http request body + return ""; + } } diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java index fc45f354985..d41b8a58391 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -56,7 +58,8 @@ public class ApiKeyAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (apiKey == null) { return; } diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/Authentication.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/Authentication.java index 7cc686d91d5..c7e65fb9670 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/Authentication.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/Authentication.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -25,6 +27,10 @@ public interface Authentication { * @param queryParams List of query parameters * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters + * @param payload HTTP request body + * @param method HTTP method + * @param uri URI + * @throws ApiException if failed to update the parameters */ - void applyToParams(List queryParams, Map headerParams, Map cookieParams); + void applyToParams(List queryParams, Map headerParams, Map cookieParams, String payload, String method, URI uri) throws ApiException; } diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java index 709e0a7a8e1..ef15ff6a57d 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java @@ -14,9 +14,11 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; import okhttp3.Credentials; +import java.net.URI; import java.util.Map; import java.util.List; @@ -43,7 +45,8 @@ public class HttpBasicAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (username == null && password == null) { return; } diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java index 6a454f6b418..96f57427e9f 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -46,8 +48,9 @@ public class HttpBearerAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { - if(bearerToken == null) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (bearerToken == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java index 576d712a0b2..67ad397c602 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java @@ -24,6 +24,7 @@ import okhttp3.internal.http.HttpMethod; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import okio.Buffer; import okio.BufferedSink; import okio.Okio; import org.threeten.bp.LocalDate; @@ -1192,16 +1193,12 @@ public class ApiClient { List allQueryParams = new ArrayList(queryParams); allQueryParams.addAll(collectionQueryParams); - // update parameters with authentication settings - updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams); - final String url = buildUrl(path, queryParams, collectionQueryParams); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(headerParams, reqBuilder); - processCookieParams(cookieParams, reqBuilder); - String contentType = (String) headerParams.get("Content-Type"); + // prepare HTTP request body RequestBody reqBody; + String contentType = headerParams.get("Content-Type"); + if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { @@ -1220,6 +1217,13 @@ public class ApiClient { reqBody = serialize(body, contentType); } + // update parameters with authentication settings + updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url)); + + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + processCookieParams(cookieParams, reqBuilder); + // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); @@ -1327,13 +1331,14 @@ public class ApiClient { * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters */ - public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, Map cookieParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, + Map cookieParams, String payload, String method, URI uri) throws ApiException { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RuntimeException("Authentication undefined: " + authName); } - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, payload, method, uri); } } @@ -1554,4 +1559,26 @@ public class ApiClient { } return path; } + + /** + * Convert the HTTP request body to a string. + * + * @param request The HTTP request object + * @return The string representation of the HTTP request body + * @throws org.openapitools.client.ApiException If fail to serialize the request body object into a string + */ + private String requestBodyToString(RequestBody requestBody) throws ApiException { + if (requestBody != null) { + try { + final Buffer buffer = new Buffer(); + requestBody.writeTo(buffer); + return buffer.readUtf8(); + } catch (final IOException e) { + throw new ApiException(e); + } + } + + // empty http request body + return ""; + } } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java index 8e03da2a679..35aa7184d26 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -56,7 +58,8 @@ public class ApiKeyAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (apiKey == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/Authentication.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/Authentication.java index 5c558b1d5ab..c59d11e0ce9 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/Authentication.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/Authentication.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -25,6 +27,10 @@ public interface Authentication { * @param queryParams List of query parameters * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters + * @param payload HTTP request body + * @param method HTTP method + * @param uri URI + * @throws ApiException if failed to update the parameters */ - void applyToParams(List queryParams, Map headerParams, Map cookieParams); + void applyToParams(List queryParams, Map headerParams, Map cookieParams, String payload, String method, URI uri) throws ApiException; } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java index 959a3211653..b4641c4217c 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java @@ -14,9 +14,11 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; import okhttp3.Credentials; +import java.net.URI; import java.util.Map; import java.util.List; @@ -43,7 +45,8 @@ public class HttpBasicAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (username == null && password == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java index e9d4c678963..460ad224963 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -46,8 +48,9 @@ public class HttpBearerAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { - if(bearerToken == null) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (bearerToken == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/OAuth.java index 0f145f8b23c..08a855ec5f9 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/OAuth.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -31,7 +33,8 @@ public class OAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (accessToken != null) { headerParams.put("Authorization", "Bearer " + accessToken); } diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/RetryingOAuth.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/RetryingOAuth.java index cb79b34ca87..911df8d3a37 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/RetryingOAuth.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/auth/RetryingOAuth.java @@ -1,5 +1,6 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; import okhttp3.Interceptor; @@ -18,6 +19,7 @@ import org.apache.oltu.oauth2.common.message.types.GrantType; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; import java.util.Map; import java.util.List; @@ -173,7 +175,8 @@ public class RetryingOAuth extends OAuth implements Interceptor { // Applying authorization to parameters is performed in the retryingIntercept method @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { // No implementation necessary } } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java index 079878c2ae0..8ffa4003db5 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java @@ -18,6 +18,7 @@ import okhttp3.internal.http.HttpMethod; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import okio.Buffer; import okio.BufferedSink; import okio.Okio; import org.threeten.bp.LocalDate; @@ -1193,16 +1194,12 @@ public class ApiClient { List allQueryParams = new ArrayList(queryParams); allQueryParams.addAll(collectionQueryParams); - // update parameters with authentication settings - updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams); - final String url = buildUrl(path, queryParams, collectionQueryParams); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(headerParams, reqBuilder); - processCookieParams(cookieParams, reqBuilder); - String contentType = (String) headerParams.get("Content-Type"); + // prepare HTTP request body RequestBody reqBody; + String contentType = headerParams.get("Content-Type"); + if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { @@ -1221,6 +1218,13 @@ public class ApiClient { reqBody = serialize(body, contentType); } + // update parameters with authentication settings + updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url)); + + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + processCookieParams(cookieParams, reqBuilder); + // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); @@ -1328,13 +1332,14 @@ public class ApiClient { * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters */ - public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, Map cookieParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, + Map cookieParams, String payload, String method, URI uri) throws ApiException { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RuntimeException("Authentication undefined: " + authName); } - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, payload, method, uri); } } @@ -1486,4 +1491,26 @@ public class ApiClient { throw new AssertionError(e); } } + + /** + * Convert the HTTP request body to a string. + * + * @param request The HTTP request object + * @return The string representation of the HTTP request body + * @throws org.openapitools.client.ApiException If fail to serialize the request body object into a string + */ + private String requestBodyToString(RequestBody requestBody) throws ApiException { + if (requestBody != null) { + try { + final Buffer buffer = new Buffer(); + requestBody.writeTo(buffer); + return buffer.readUtf8(); + } catch (final IOException e) { + throw new ApiException(e); + } + } + + // empty http request body + return ""; + } } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java index 8e03da2a679..35aa7184d26 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -56,7 +58,8 @@ public class ApiKeyAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (apiKey == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/Authentication.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/Authentication.java index 5c558b1d5ab..c59d11e0ce9 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/Authentication.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/Authentication.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -25,6 +27,10 @@ public interface Authentication { * @param queryParams List of query parameters * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters + * @param payload HTTP request body + * @param method HTTP method + * @param uri URI + * @throws ApiException if failed to update the parameters */ - void applyToParams(List queryParams, Map headerParams, Map cookieParams); + void applyToParams(List queryParams, Map headerParams, Map cookieParams, String payload, String method, URI uri) throws ApiException; } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java index 959a3211653..b4641c4217c 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java @@ -14,9 +14,11 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; import okhttp3.Credentials; +import java.net.URI; import java.util.Map; import java.util.List; @@ -43,7 +45,8 @@ public class HttpBasicAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (username == null && password == null) { return; } 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 index e9d4c678963..460ad224963 100644 --- 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 @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -46,8 +48,9 @@ public class HttpBearerAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { - if(bearerToken == null) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (bearerToken == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/OAuth.java index 0f145f8b23c..08a855ec5f9 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/OAuth.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -31,7 +33,8 @@ public class OAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (accessToken != null) { headerParams.put("Authorization", "Bearer " + accessToken); } diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/RetryingOAuth.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/RetryingOAuth.java index cb79b34ca87..911df8d3a37 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/RetryingOAuth.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/auth/RetryingOAuth.java @@ -1,5 +1,6 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; import okhttp3.Interceptor; @@ -18,6 +19,7 @@ import org.apache.oltu.oauth2.common.message.types.GrantType; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; import java.util.Map; import java.util.List; @@ -173,7 +175,8 @@ public class RetryingOAuth extends OAuth implements Interceptor { // Applying authorization to parameters is performed in the retryingIntercept method @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { // No implementation necessary } } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java index 079878c2ae0..8ffa4003db5 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java @@ -18,6 +18,7 @@ import okhttp3.internal.http.HttpMethod; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import okio.Buffer; import okio.BufferedSink; import okio.Okio; import org.threeten.bp.LocalDate; @@ -1193,16 +1194,12 @@ public class ApiClient { List allQueryParams = new ArrayList(queryParams); allQueryParams.addAll(collectionQueryParams); - // update parameters with authentication settings - updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams); - final String url = buildUrl(path, queryParams, collectionQueryParams); - final Request.Builder reqBuilder = new Request.Builder().url(url); - processHeaderParams(headerParams, reqBuilder); - processCookieParams(cookieParams, reqBuilder); - String contentType = (String) headerParams.get("Content-Type"); + // prepare HTTP request body RequestBody reqBody; + String contentType = headerParams.get("Content-Type"); + if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { @@ -1221,6 +1218,13 @@ public class ApiClient { reqBody = serialize(body, contentType); } + // update parameters with authentication settings + updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url)); + + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + processCookieParams(cookieParams, reqBuilder); + // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); @@ -1328,13 +1332,14 @@ public class ApiClient { * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters */ - public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, Map cookieParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams, + Map cookieParams, String payload, String method, URI uri) throws ApiException { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RuntimeException("Authentication undefined: " + authName); } - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, payload, method, uri); } } @@ -1486,4 +1491,26 @@ public class ApiClient { throw new AssertionError(e); } } + + /** + * Convert the HTTP request body to a string. + * + * @param request The HTTP request object + * @return The string representation of the HTTP request body + * @throws org.openapitools.client.ApiException If fail to serialize the request body object into a string + */ + private String requestBodyToString(RequestBody requestBody) throws ApiException { + if (requestBody != null) { + try { + final Buffer buffer = new Buffer(); + requestBody.writeTo(buffer); + return buffer.readUtf8(); + } catch (final IOException e) { + throw new ApiException(e); + } + } + + // empty http request body + return ""; + } } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java index 8e03da2a679..35aa7184d26 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/ApiKeyAuth.java @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -56,7 +58,8 @@ public class ApiKeyAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (apiKey == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/Authentication.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/Authentication.java index 5c558b1d5ab..c59d11e0ce9 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/Authentication.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/Authentication.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -25,6 +27,10 @@ public interface Authentication { * @param queryParams List of query parameters * @param headerParams Map of header parameters * @param cookieParams Map of cookie parameters + * @param payload HTTP request body + * @param method HTTP method + * @param uri URI + * @throws ApiException if failed to update the parameters */ - void applyToParams(List queryParams, Map headerParams, Map cookieParams); + void applyToParams(List queryParams, Map headerParams, Map cookieParams, String payload, String method, URI uri) throws ApiException; } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java index 959a3211653..b4641c4217c 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/HttpBasicAuth.java @@ -14,9 +14,11 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; import okhttp3.Credentials; +import java.net.URI; import java.util.Map; import java.util.List; @@ -43,7 +45,8 @@ public class HttpBasicAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (username == null && password == null) { return; } 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 index e9d4c678963..460ad224963 100644 --- 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 @@ -13,8 +13,10 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; +import java.net.URI; import java.util.Map; import java.util.List; @@ -46,8 +48,9 @@ public class HttpBearerAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { - if(bearerToken == null) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { + if (bearerToken == null) { return; } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/OAuth.java index 0f145f8b23c..08a855ec5f9 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/OAuth.java @@ -14,7 +14,9 @@ package org.openapitools.client.auth; import org.openapitools.client.Pair; +import org.openapitools.client.ApiException; +import java.net.URI; import java.util.Map; import java.util.List; @@ -31,7 +33,8 @@ public class OAuth implements Authentication { } @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { if (accessToken != null) { headerParams.put("Authorization", "Bearer " + accessToken); } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/RetryingOAuth.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/RetryingOAuth.java index cb79b34ca87..911df8d3a37 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/RetryingOAuth.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/auth/RetryingOAuth.java @@ -1,5 +1,6 @@ package org.openapitools.client.auth; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; import okhttp3.Interceptor; @@ -18,6 +19,7 @@ import org.apache.oltu.oauth2.common.message.types.GrantType; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; import java.util.Map; import java.util.List; @@ -173,7 +175,8 @@ public class RetryingOAuth extends OAuth implements Interceptor { // Applying authorization to parameters is performed in the retryingIntercept method @Override - public void applyToParams(List queryParams, Map headerParams, Map cookieParams) { + public void applyToParams(List queryParams, Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { // No implementation necessary } } diff --git a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/ApiKeyAuthTest.java b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/ApiKeyAuthTest.java index c48579be513..f7c2ecec9e1 100644 --- a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/ApiKeyAuthTest.java +++ b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/ApiKeyAuthTest.java @@ -7,18 +7,19 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.*; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; public class ApiKeyAuthTest { @Test - public void testApplyToParamsInQuery() { + public void testApplyToParamsInQuery() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); ApiKeyAuth auth = new ApiKeyAuth("query", "api_key"); auth.setApiKey("my-api-key"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); assertEquals(1, queryParams.size()); for (Pair queryParam : queryParams) { @@ -31,14 +32,14 @@ public class ApiKeyAuthTest { } @Test - public void testApplyToParamsInQueryWithNullValue() { + public void testApplyToParamsInQueryWithNullValue() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); ApiKeyAuth auth = new ApiKeyAuth("query", "api_key"); auth.setApiKey(null); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to parameters assertEquals(0, queryParams.size()); @@ -47,7 +48,7 @@ public class ApiKeyAuthTest { } @Test - public void testApplyToParamsInHeaderWithPrefix() { + public void testApplyToParamsInHeaderWithPrefix() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); @@ -55,7 +56,7 @@ public class ApiKeyAuthTest { ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN"); auth.setApiKey("my-api-token"); auth.setApiKeyPrefix("Token"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to query or cookie parameters assertEquals(0, queryParams.size()); @@ -65,7 +66,7 @@ public class ApiKeyAuthTest { } @Test - public void testApplyToParamsInHeaderWithNullValue() { + public void testApplyToParamsInHeaderWithNullValue() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); @@ -73,7 +74,7 @@ public class ApiKeyAuthTest { ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN"); auth.setApiKey(null); auth.setApiKeyPrefix("Token"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to parameters assertEquals(0, queryParams.size()); @@ -82,7 +83,7 @@ public class ApiKeyAuthTest { } @Test - public void testApplyToParamsInCookieWithPrefix() { + public void testApplyToParamsInCookieWithPrefix() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); @@ -90,7 +91,7 @@ public class ApiKeyAuthTest { ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN"); auth.setApiKey("my-api-token"); auth.setApiKeyPrefix("Token"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to query or header parameters assertEquals(0, queryParams.size()); @@ -100,7 +101,7 @@ public class ApiKeyAuthTest { } @Test - public void testApplyToParamsInCookieWithNullValue() { + public void testApplyToParamsInCookieWithNullValue() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); @@ -108,7 +109,7 @@ public class ApiKeyAuthTest { ApiKeyAuth auth = new ApiKeyAuth("cookie", "X-API-TOKEN"); auth.setApiKey(null); auth.setApiKeyPrefix("Token"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to parameters assertEquals(0, queryParams.size()); diff --git a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/HttpBasicAuthTest.java b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/HttpBasicAuthTest.java index 630f85ad37c..a077d394ada 100644 --- a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/HttpBasicAuthTest.java +++ b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/HttpBasicAuthTest.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.*; +import org.openapitools.client.ApiException; import org.openapitools.client.Pair; public class HttpBasicAuthTest { @@ -18,14 +19,14 @@ public class HttpBasicAuthTest { } @Test - public void testApplyToParams() { + public void testApplyToParams() throws ApiException { List queryParams = new ArrayList(); Map headerParams = new HashMap(); Map cookieParams = new HashMap(); auth.setUsername("my-username"); auth.setPassword("my-password"); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to query or cookie parameters assertEquals(0, queryParams.size()); @@ -38,7 +39,7 @@ public class HttpBasicAuthTest { // null username should be treated as empty string auth.setUsername(null); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // the string below is base64-encoded result of ":my-password" with the "Basic " prefix expected = "Basic Om15LXBhc3N3b3Jk"; assertEquals(expected, headerParams.get("Authorization")); @@ -46,7 +47,7 @@ public class HttpBasicAuthTest { // null password should be treated as empty string auth.setUsername("my-username"); auth.setPassword(null); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // the string below is base64-encoded result of "my-username:" with the "Basic " prefix expected = "Basic bXktdXNlcm5hbWU6"; assertEquals(expected, headerParams.get("Authorization")); @@ -56,7 +57,7 @@ public class HttpBasicAuthTest { headerParams = new HashMap(); auth.setUsername(null); auth.setPassword(null); - auth.applyToParams(queryParams, headerParams, cookieParams); + auth.applyToParams(queryParams, headerParams, cookieParams, null, null, null); // no changes to parameters assertEquals(0, queryParams.size()); assertEquals(0, headerParams.size());