[Java] Fix exception when OAuth2 token URL is a relative URL (#5535)

* Add support for case when OAuth2 token URL is a relative URL

* Add support for case when OAuth2 token URL is a relative URL

* run scripts under bin
This commit is contained in:
Sebastien Rosset
2020-03-10 10:18:08 -07:00
committed by GitHub
parent 2ee4ad1038
commit 6034c09130
6 changed files with 89 additions and 5 deletions

View File

@@ -30,6 +30,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
@@ -122,10 +123,30 @@ public class ApiClient {
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID, secret, and additional parameters
*/
public ApiClient(String clientId, String clientSecret, Map<String, String> parameters) {
this(null, clientId, clientSecret, parameters);
}
/*
* Constructor for ApiClient to support access token retry on 401/403 configured with base path, client ID, secret, and additional parameters
*/
public ApiClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters) {
init();
if (basePath != null) {
this.basePath = basePath;
}
{{#hasOAuthMethods}}
RetryingOAuth retryingOAuth = new RetryingOAuth("{{tokenUrl}}", clientId, OAuthFlow.{{flow}}, clientSecret, parameters);
String tokenUrl = "{{tokenUrl}}";
if (!"".equals(tokenUrl) && !URI.create(tokenUrl).isAbsolute()) {
URI uri = URI.create(getBasePath());
tokenUrl = uri.getScheme() + ":" +
(uri.getAuthority() != null ? "//" + uri.getAuthority() : "") +
tokenUrl;
if (!URI.create(tokenUrl).isAbsolute()) {
throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL");
}
}
RetryingOAuth retryingOAuth = new RetryingOAuth(tokenUrl, clientId, OAuthFlow.{{flow}}, clientSecret, parameters);
authentications.put(
"{{name}}",
retryingOAuth

View File

@@ -36,6 +36,13 @@ public class RetryingOAuth extends OAuth implements Interceptor {
this(new OkHttpClient(), tokenRequestBuilder);
}
/**
@param tokenUrl The token URL to be used for this OAuth2 flow.
Applicable to the following OAuth2 flows: "password", "clientCredentials" and "authorizationCode".
The value must be an absolute URL.
@param clientId The OAuth2 client ID for the "clientCredentials" flow.
@param clientSecret The OAuth2 client secret for the "clientCredentials" flow.
*/
public RetryingOAuth(
String tokenUrl,
String clientId,

View File

@@ -32,6 +32,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
@@ -114,9 +115,29 @@ public class ApiClient {
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID, secret, and additional parameters
*/
public ApiClient(String clientId, String clientSecret, Map<String, String> parameters) {
init();
this(null, clientId, clientSecret, parameters);
}
RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, OAuthFlow.implicit, clientSecret, parameters);
/*
* Constructor for ApiClient to support access token retry on 401/403 configured with base path, client ID, secret, and additional parameters
*/
public ApiClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters) {
init();
if (basePath != null) {
this.basePath = basePath;
}
String tokenUrl = "";
if (!"".equals(tokenUrl) && !URI.create(tokenUrl).isAbsolute()) {
URI uri = URI.create(getBasePath());
tokenUrl = uri.getScheme() + ":" +
(uri.getAuthority() != null ? "//" + uri.getAuthority() : "") +
tokenUrl;
if (!URI.create(tokenUrl).isAbsolute()) {
throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL");
}
}
RetryingOAuth retryingOAuth = new RetryingOAuth(tokenUrl, clientId, OAuthFlow.implicit, clientSecret, parameters);
authentications.put(
"petstore_auth",
retryingOAuth

View File

@@ -35,6 +35,13 @@ public class RetryingOAuth extends OAuth implements Interceptor {
this(new OkHttpClient(), tokenRequestBuilder);
}
/**
@param tokenUrl The token URL to be used for this OAuth2 flow.
Applicable to the following OAuth2 flows: "password", "clientCredentials" and "authorizationCode".
The value must be an absolute URL.
@param clientId The OAuth2 client ID for the "clientCredentials" flow.
@param clientSecret The OAuth2 client secret for the "clientCredentials" flow.
*/
public RetryingOAuth(
String tokenUrl,
String clientId,

View File

@@ -32,6 +32,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
@@ -114,9 +115,29 @@ public class ApiClient {
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID, secret, and additional parameters
*/
public ApiClient(String clientId, String clientSecret, Map<String, String> parameters) {
init();
this(null, clientId, clientSecret, parameters);
}
RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, OAuthFlow.implicit, clientSecret, parameters);
/*
* Constructor for ApiClient to support access token retry on 401/403 configured with base path, client ID, secret, and additional parameters
*/
public ApiClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters) {
init();
if (basePath != null) {
this.basePath = basePath;
}
String tokenUrl = "";
if (!"".equals(tokenUrl) && !URI.create(tokenUrl).isAbsolute()) {
URI uri = URI.create(getBasePath());
tokenUrl = uri.getScheme() + ":" +
(uri.getAuthority() != null ? "//" + uri.getAuthority() : "") +
tokenUrl;
if (!URI.create(tokenUrl).isAbsolute()) {
throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL");
}
}
RetryingOAuth retryingOAuth = new RetryingOAuth(tokenUrl, clientId, OAuthFlow.implicit, clientSecret, parameters);
authentications.put(
"petstore_auth",
retryingOAuth

View File

@@ -35,6 +35,13 @@ public class RetryingOAuth extends OAuth implements Interceptor {
this(new OkHttpClient(), tokenRequestBuilder);
}
/**
@param tokenUrl The token URL to be used for this OAuth2 flow.
Applicable to the following OAuth2 flows: "password", "clientCredentials" and "authorizationCode".
The value must be an absolute URL.
@param clientId The OAuth2 client ID for the "clientCredentials" flow.
@param clientSecret The OAuth2 client secret for the "clientCredentials" flow.
*/
public RetryingOAuth(
String tokenUrl,
String clientId,