update authentication to include more parameters (#10858)

This commit is contained in:
William Cheng 2021-11-15 16:34:27 +08:00 committed by GitHub
parent f596b32316
commit c06a21e638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 488 additions and 90 deletions

View File

@ -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<Pair> allQueryParams = new ArrayList<Pair>(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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams,
Map<String, String> 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 "";
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> 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);
}
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (username == null && password == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> 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;
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
// No implementation necessary
}
}

View File

@ -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<Pair> allQueryParams = new ArrayList<Pair>(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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams,
Map<String, String> 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 "";
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (apiKey == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (username == null && password == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
if(bearerToken == null) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (bearerToken == null) {
return;
}

View File

@ -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<Pair> allQueryParams = new ArrayList<Pair>(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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams,
Map<String, String> 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 "";
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (apiKey == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (username == null && password == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
if(bearerToken == null) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (bearerToken == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
// No implementation necessary
}
}

View File

@ -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<Pair> allQueryParams = new ArrayList<Pair>(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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams,
Map<String, String> 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 "";
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (apiKey == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (username == null && password == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
if(bearerToken == null) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (bearerToken == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
// No implementation necessary
}
}

View File

@ -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<Pair> allQueryParams = new ArrayList<Pair>(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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams,
Map<String, String> 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 "";
}
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (apiKey == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (username == null && password == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
if(bearerToken == null) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (bearerToken == null) {
return;
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}

View File

@ -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<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
// No implementation necessary
}
}

View File

@ -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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
@ -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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
@ -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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
@ -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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
@ -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());

View File

@ -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<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
Map<String, String> cookieParams = new HashMap<String, String>();
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<String, String>();
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());