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
35 changed files with 488 additions and 90 deletions

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());