[Java][okhttp-gson] Fix bug when specifying grant-type (flow) for OAuth token retry (#1183)

* Fix bug when specifying grant-type for OAuth token retry

* Update samples

* Update security samples
This commit is contained in:
Kiran-Sivakumar 2018-10-08 02:39:10 -07:00 committed by William Cheng
parent 52a112d90f
commit 7d58f308d9
9 changed files with 93 additions and 17 deletions

View File

@ -54,6 +54,7 @@ import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
import {{invokerPackage}}.auth.RetryingOAuth; import {{invokerPackage}}.auth.RetryingOAuth;
import {{invokerPackage}}.auth.OAuthFlow;
public class ApiClient { public class ApiClient {
@ -102,7 +103,7 @@ public class ApiClient {
) { ) {
init(); init();
RetryingOAuth retryingOAuth = new RetryingOAuth("{{tokenUrl}}", clientId, GrantType.valueOf("{{flow}}"), clientSecret, parameters); RetryingOAuth retryingOAuth = new RetryingOAuth("{{tokenUrl}}", clientId, OAuthFlow.{{flow}}, clientSecret, parameters);
authentications.put( authentications.put(
"{{name}}", "{{name}}",
retryingOAuth retryingOAuth

View File

@ -35,15 +35,14 @@ public class RetryingOAuth extends OAuth implements Interceptor {
public RetryingOAuth( public RetryingOAuth(
String tokenUrl, String tokenUrl,
String clientId, String clientId,
GrantType grantType, OAuthFlow flow,
String clientSecret, String clientSecret,
Map<String, String> parameters Map<String, String> parameters
) { ) {
this(OAuthClientRequest.tokenLocation(tokenUrl) this(OAuthClientRequest.tokenLocation(tokenUrl)
.setClientId(clientId) .setClientId(clientId)
.setGrantType(grantType)
.setClientSecret(clientSecret)); .setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) { if (parameters != null) {
for (String paramName : parameters.keySet()) { for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName)); tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
@ -51,6 +50,25 @@ public class RetryingOAuth extends OAuth implements Interceptor {
} }
} }
public void setFlow(OAuthFlow flow) {
switch(flow) {
case accessCode:
tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE);
break;
case implicit:
tokenRequestBuilder.setGrantType(GrantType.IMPLICIT);
break;
case password:
tokenRequestBuilder.setGrantType(GrantType.PASSWORD);
break;
case application:
tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS);
break;
default:
break;
}
}
@Override @Override
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
return retryingIntercept(chain, true); return retryingIntercept(chain, true);

View File

@ -1 +1 @@
3.3.0-SNAPSHOT 3.3.1-SNAPSHOT

View File

@ -53,6 +53,7 @@ import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.RetryingOAuth; import org.openapitools.client.auth.RetryingOAuth;
import org.openapitools.client.auth.OAuthFlow;
public class ApiClient { public class ApiClient {
@ -100,7 +101,7 @@ public class ApiClient {
) { ) {
init(); init();
RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, GrantType.valueOf("implicit"), clientSecret, parameters); RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, OAuthFlow.implicit, clientSecret, parameters);
authentications.put( authentications.put(
"petstore_auth", "petstore_auth",
retryingOAuth retryingOAuth

View File

@ -35,15 +35,14 @@ public class RetryingOAuth extends OAuth implements Interceptor {
public RetryingOAuth( public RetryingOAuth(
String tokenUrl, String tokenUrl,
String clientId, String clientId,
GrantType grantType, OAuthFlow flow,
String clientSecret, String clientSecret,
Map<String, String> parameters Map<String, String> parameters
) { ) {
this(OAuthClientRequest.tokenLocation(tokenUrl) this(OAuthClientRequest.tokenLocation(tokenUrl)
.setClientId(clientId) .setClientId(clientId)
.setGrantType(grantType)
.setClientSecret(clientSecret)); .setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) { if (parameters != null) {
for (String paramName : parameters.keySet()) { for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName)); tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
@ -51,6 +50,25 @@ public class RetryingOAuth extends OAuth implements Interceptor {
} }
} }
public void setFlow(OAuthFlow flow) {
switch(flow) {
case accessCode:
tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE);
break;
case implicit:
tokenRequestBuilder.setGrantType(GrantType.IMPLICIT);
break;
case password:
tokenRequestBuilder.setGrantType(GrantType.PASSWORD);
break;
case application:
tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS);
break;
default:
break;
}
}
@Override @Override
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
return retryingIntercept(chain, true); return retryingIntercept(chain, true);

View File

@ -53,6 +53,7 @@ import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.RetryingOAuth; import org.openapitools.client.auth.RetryingOAuth;
import org.openapitools.client.auth.OAuthFlow;
public class ApiClient { public class ApiClient {
@ -102,7 +103,7 @@ public class ApiClient {
) { ) {
init(); init();
RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, GrantType.valueOf("implicit"), clientSecret, parameters); RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, OAuthFlow.implicit, clientSecret, parameters);
authentications.put( authentications.put(
"petstore_auth", "petstore_auth",
retryingOAuth retryingOAuth

View File

@ -35,15 +35,14 @@ public class RetryingOAuth extends OAuth implements Interceptor {
public RetryingOAuth( public RetryingOAuth(
String tokenUrl, String tokenUrl,
String clientId, String clientId,
GrantType grantType, OAuthFlow flow,
String clientSecret, String clientSecret,
Map<String, String> parameters Map<String, String> parameters
) { ) {
this(OAuthClientRequest.tokenLocation(tokenUrl) this(OAuthClientRequest.tokenLocation(tokenUrl)
.setClientId(clientId) .setClientId(clientId)
.setGrantType(grantType)
.setClientSecret(clientSecret)); .setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) { if (parameters != null) {
for (String paramName : parameters.keySet()) { for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName)); tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
@ -51,6 +50,25 @@ public class RetryingOAuth extends OAuth implements Interceptor {
} }
} }
public void setFlow(OAuthFlow flow) {
switch(flow) {
case accessCode:
tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE);
break;
case implicit:
tokenRequestBuilder.setGrantType(GrantType.IMPLICIT);
break;
case password:
tokenRequestBuilder.setGrantType(GrantType.PASSWORD);
break;
case application:
tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS);
break;
default:
break;
}
}
@Override @Override
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
return retryingIntercept(chain, true); return retryingIntercept(chain, true);

View File

@ -53,6 +53,7 @@ import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.RetryingOAuth; import org.openapitools.client.auth.RetryingOAuth;
import org.openapitools.client.auth.OAuthFlow;
public class ApiClient { public class ApiClient {
@ -102,7 +103,7 @@ public class ApiClient {
) { ) {
init(); init();
RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, GrantType.valueOf("implicit"), clientSecret, parameters); RetryingOAuth retryingOAuth = new RetryingOAuth("", clientId, OAuthFlow.implicit, clientSecret, parameters);
authentications.put( authentications.put(
"petstore_auth", "petstore_auth",
retryingOAuth retryingOAuth

View File

@ -35,15 +35,14 @@ public class RetryingOAuth extends OAuth implements Interceptor {
public RetryingOAuth( public RetryingOAuth(
String tokenUrl, String tokenUrl,
String clientId, String clientId,
GrantType grantType, OAuthFlow flow,
String clientSecret, String clientSecret,
Map<String, String> parameters Map<String, String> parameters
) { ) {
this(OAuthClientRequest.tokenLocation(tokenUrl) this(OAuthClientRequest.tokenLocation(tokenUrl)
.setClientId(clientId) .setClientId(clientId)
.setGrantType(grantType)
.setClientSecret(clientSecret)); .setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) { if (parameters != null) {
for (String paramName : parameters.keySet()) { for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName)); tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
@ -51,6 +50,25 @@ public class RetryingOAuth extends OAuth implements Interceptor {
} }
} }
public void setFlow(OAuthFlow flow) {
switch(flow) {
case accessCode:
tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE);
break;
case implicit:
tokenRequestBuilder.setGrantType(GrantType.IMPLICIT);
break;
case password:
tokenRequestBuilder.setGrantType(GrantType.PASSWORD);
break;
case application:
tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS);
break;
default:
break;
}
}
@Override @Override
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
return retryingIntercept(chain, true); return retryingIntercept(chain, true);