forked from loafle/openapi-generator-original
add a listener on new token reception
Add a listener on new token reception that can then be stored or logged for debug Add an helper method to add authorizations (otherwise if the swagger def is not complete or correct, it is hard to configure from the main app)
This commit is contained in:
parent
1f38ce724f
commit
8b0bf8ff72
@ -28,6 +28,7 @@ import com.squareup.okhttp.OkHttpClient;
|
||||
import {{invokerPackage}}.auth.HttpBasicAuth;
|
||||
import {{invokerPackage}}.auth.ApiKeyAuth;
|
||||
import {{invokerPackage}}.auth.OAuth;
|
||||
import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
|
||||
import {{invokerPackage}}.auth.OAuthFlow;
|
||||
|
||||
|
||||
@ -44,12 +45,7 @@ public class ApiClient {
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
okClient = new OkHttpClient();
|
||||
adapterBuilder.setClient(new OkClient(okClient));
|
||||
for(String authName : authNames) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}{{#hasAuthMethods}}
|
||||
for(String authName : authNames) { {{#hasAuthMethods}}
|
||||
Interceptor auth;
|
||||
{{#authMethods}}if (authName == "{{name}}") { {{#isBasic}}
|
||||
auth = new HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
|
||||
@ -58,10 +54,9 @@ public class ApiClient {
|
||||
} else {{/authMethods}}{
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
apiAuthorizations.put(authName, auth);{{/hasAuthMethods}}{{^hasAuthMethods}}
|
||||
addAuthorization(authName, auth);{{/hasAuthMethods}}{{^hasAuthMethods}}
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");{{/hasAuthMethods}}
|
||||
}
|
||||
addAuthsToOkClient(okClient);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,9 +110,12 @@ public class ApiClient {
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.create();
|
||||
|
||||
okClient = new OkHttpClient();
|
||||
|
||||
adapterBuilder = new RestAdapter
|
||||
.Builder()
|
||||
.setEndpoint("{{basePath}}")
|
||||
.setClient(new OkClient(okClient))
|
||||
.setConverter(new GsonConverterWrapper(gson));
|
||||
}
|
||||
|
||||
@ -224,6 +222,33 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName
|
||||
* @param authorization
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okClient.interceptors().add(authorization);
|
||||
}
|
||||
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
|
||||
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
|
||||
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
|
||||
import org.apache.oltu.oauth2.common.message.types.GrantType;
|
||||
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;
|
||||
|
||||
import com.squareup.okhttp.Interceptor;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
@ -23,12 +24,18 @@ import com.squareup.okhttp.Response;
|
||||
|
||||
public class OAuth implements Interceptor {
|
||||
|
||||
public interface AccessTokenListener {
|
||||
public void notify(BasicOAuthToken token);
|
||||
}
|
||||
|
||||
private volatile String accessToken;
|
||||
private OAuthClient oauthClient;
|
||||
|
||||
private TokenRequestBuilder tokenRequestBuilder;
|
||||
private AuthenticationRequestBuilder authenticationRequestBuilder;
|
||||
|
||||
private AccessTokenListener accessTokenListener;
|
||||
|
||||
public OAuth( OkHttpClient client, TokenRequestBuilder requestBuilder ) {
|
||||
this.oauthClient = new OAuthClient(new OAuthOkHttpClient(client));
|
||||
this.tokenRequestBuilder = requestBuilder;
|
||||
@ -110,9 +117,11 @@ public class OAuth implements Interceptor {
|
||||
public synchronized void updateAccessToken(String requestAccessToken) throws IOException {
|
||||
if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
|
||||
try {
|
||||
OAuthJSONAccessTokenResponse accessTokenResponse;
|
||||
accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||
OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||
setAccessToken(accessTokenResponse.getAccessToken());
|
||||
if (accessTokenListener != null) {
|
||||
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
||||
}
|
||||
} catch (OAuthSystemException e) {
|
||||
throw new IOException(e);
|
||||
} catch (OAuthProblemException e) {
|
||||
@ -121,6 +130,10 @@ public class OAuth implements Interceptor {
|
||||
}
|
||||
}
|
||||
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
this.accessTokenListener = accessTokenListener;
|
||||
}
|
||||
|
||||
public synchronized String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import com.squareup.okhttp.OkHttpClient;
|
||||
import io.swagger.client.auth.HttpBasicAuth;
|
||||
import io.swagger.client.auth.ApiKeyAuth;
|
||||
import io.swagger.client.auth.OAuth;
|
||||
import io.swagger.client.auth.OAuth.AccessTokenListener;
|
||||
import io.swagger.client.auth.OAuthFlow;
|
||||
|
||||
|
||||
@ -44,12 +45,7 @@ public class ApiClient {
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
okClient = new OkHttpClient();
|
||||
adapterBuilder.setClient(new OkClient(okClient));
|
||||
for(String authName : authNames) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
Interceptor auth;
|
||||
if (authName == "petstore_auth") {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
@ -58,9 +54,8 @@ public class ApiClient {
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
apiAuthorizations.put(authName, auth);
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
addAuthsToOkClient(okClient);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,9 +109,12 @@ public class ApiClient {
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.create();
|
||||
|
||||
okClient = new OkHttpClient();
|
||||
|
||||
adapterBuilder = new RestAdapter
|
||||
.Builder()
|
||||
.setEndpoint("http://petstore.swagger.io/v2")
|
||||
.setClient(new OkClient(okClient))
|
||||
.setConverter(new GsonConverterWrapper(gson));
|
||||
}
|
||||
|
||||
@ -223,6 +221,33 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName
|
||||
* @param authorization
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okClient.interceptors().add(authorization);
|
||||
}
|
||||
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
|
||||
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
|
||||
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
|
||||
import org.apache.oltu.oauth2.common.message.types.GrantType;
|
||||
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;
|
||||
|
||||
import com.squareup.okhttp.Interceptor;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
@ -23,12 +24,18 @@ import com.squareup.okhttp.Response;
|
||||
|
||||
public class OAuth implements Interceptor {
|
||||
|
||||
public interface AccessTokenListener {
|
||||
public void notify(BasicOAuthToken token);
|
||||
}
|
||||
|
||||
private volatile String accessToken;
|
||||
private OAuthClient oauthClient;
|
||||
|
||||
private TokenRequestBuilder tokenRequestBuilder;
|
||||
private AuthenticationRequestBuilder authenticationRequestBuilder;
|
||||
|
||||
private AccessTokenListener accessTokenListener;
|
||||
|
||||
public OAuth( OkHttpClient client, TokenRequestBuilder requestBuilder ) {
|
||||
this.oauthClient = new OAuthClient(new OAuthOkHttpClient(client));
|
||||
this.tokenRequestBuilder = requestBuilder;
|
||||
@ -110,9 +117,11 @@ public class OAuth implements Interceptor {
|
||||
public synchronized void updateAccessToken(String requestAccessToken) throws IOException {
|
||||
if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
|
||||
try {
|
||||
OAuthJSONAccessTokenResponse accessTokenResponse;
|
||||
accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||
OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||
setAccessToken(accessTokenResponse.getAccessToken());
|
||||
if (accessTokenListener != null) {
|
||||
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
||||
}
|
||||
} catch (OAuthSystemException e) {
|
||||
throw new IOException(e);
|
||||
} catch (OAuthProblemException e) {
|
||||
@ -121,6 +130,10 @@ public class OAuth implements Interceptor {
|
||||
}
|
||||
}
|
||||
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
this.accessTokenListener = accessTokenListener;
|
||||
}
|
||||
|
||||
public synchronized String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user