From 8b0bf8ff726a31c2fbdb38676065c38e86e7181e Mon Sep 17 00:00:00 2001 From: cbornet Date: Thu, 15 Oct 2015 15:50:29 +0200 Subject: [PATCH] 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) --- .../libraries/retrofit/ApiClient.mustache | 41 +++++++++++++++---- .../libraries/retrofit/auth/OAuth.mustache | 17 +++++++- .../java/io/swagger/client/ApiClient.java | 41 +++++++++++++++---- .../java/io/swagger/client/auth/OAuth.java | 17 +++++++- 4 files changed, 96 insertions(+), 20 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache index eaca662c583..183fb5f27ce 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache @@ -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 getApiAuthorizations() { return apiAuthorizations; } diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache index e6179d6c690..68ee918601e 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache @@ -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; } diff --git a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/ApiClient.java index 343e545e83d..6540997c319 100644 --- a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/ApiClient.java @@ -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"); - } + for(String authName : authNames) { 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 getApiAuthorizations() { return apiAuthorizations; } diff --git a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java index f7e488e0499..80614f0f56a 100644 --- a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java +++ b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java @@ -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; }