forked from loafle/openapi-generator-original
update authentication to include more parameters (#10858)
This commit is contained in:
@@ -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 "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user