From d04aba57c903b1ad5fecaf488bae3ba80105f498 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 30 Jan 2018 14:57:42 +0530 Subject: [PATCH] Fix https://github.com/wso2/product-iots/issues/1641 --- client/client/build.gradle | 4 +- .../iot/agent/services/MessageProcessor.java | 2 +- .../wso2/iot/agent/proxy/APIController.java | 2 +- .../iot/agent/proxy/AccessTokenHandler.java | 2 +- .../wso2/iot/agent/proxy/IdentityProxy.java | 10 ++- .../iot/agent/proxy/RefreshTokenHandler.java | 62 ++++++++++--------- .../wso2/iot/agent/proxy/utils/Constants.java | 1 + 7 files changed, 47 insertions(+), 36 deletions(-) diff --git a/client/client/build.gradle b/client/client/build.gradle index f237fea4..e6272430 100644 --- a/client/client/build.gradle +++ b/client/client/build.gradle @@ -36,8 +36,8 @@ android { targetSdkVersion 25 multiDexEnabled true - versionCode 3010029 - versionName "3.1.29" + versionCode 3010030 + versionName "3.1.30" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/client/client/src/main/java/org/wso2/iot/agent/services/MessageProcessor.java b/client/client/src/main/java/org/wso2/iot/agent/services/MessageProcessor.java index 517c190c..81f2547e 100644 --- a/client/client/src/main/java/org/wso2/iot/agent/services/MessageProcessor.java +++ b/client/client/src/main/java/org/wso2/iot/agent/services/MessageProcessor.java @@ -508,7 +508,7 @@ public void onReceiveAPIResult(Map result, int requestCode) { } } else if (Constants.Status.AUTHENTICATION_FAILED.equals(responseStatus) && org.wso2.iot.agent.proxy.utils.Constants.REFRESH_TOKEN_EXPIRED.equals(response)) { - Log.d(TAG, "Requesting credentials to obtain new token pair."); + Log.i(TAG, "Requesting credentials to obtain new token pair."); LocalNotification.stopPolling(context); Preference.putBoolean(context, Constants.TOKEN_EXPIRED, true); CommonUtils.displayNotification(context, diff --git a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/APIController.java b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/APIController.java index 11210ad9..62c04faa 100644 --- a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/APIController.java +++ b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/APIController.java @@ -52,7 +52,7 @@ * return API results to the client. */ public class APIController implements TokenCallBack { - private static final String TAG = "APIController"; + private static final String TAG = APIController.class.getSimpleName(); private Token token; private String clientKey, clientSecret; private APIResultCallBack apiResultCallback; diff --git a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/AccessTokenHandler.java b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/AccessTokenHandler.java index 8327ab51..0661e3ec 100644 --- a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/AccessTokenHandler.java +++ b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/AccessTokenHandler.java @@ -182,7 +182,7 @@ private void processTokenResponse(String responseCode, String result) { timeToExpireSecond = Long.parseLong(response.getString(Constants.EXPIRE_LABEL)); Token token = new Token(); long expiresOn = new Date().getTime() - + (timeToExpireSecond - Constants.HttpClient.TOKEN_VALIDITY_PERCENTAGE * timeToExpireSecond / 100) * 1000; + + (timeToExpireSecond - (100 - Constants.HttpClient.TOKEN_VALIDITY_PERCENTAGE) * timeToExpireSecond / 100) * 1000; token.setExpiresOn(new Date(expiresOn)); token.setRefreshToken(refreshToken); token.setAccessToken(accessToken); diff --git a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/IdentityProxy.java b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/IdentityProxy.java index 85207337..d8c70973 100644 --- a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/IdentityProxy.java +++ b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/IdentityProxy.java @@ -83,10 +83,14 @@ public void receiveAccessToken(String status, String message, Token token) { @Override public void receiveNewAccessToken(String status, String message, Token token) { - if (Constants.DEBUG_ENABLED && token != null) { - Log.d(TAG, "Using Access Token: " + token.getAccessToken()); + if (token != null) { + if (Constants.DEBUG_ENABLED) { + Log.d(TAG, "Using Access Token: " + token.getAccessToken()); + } + IdentityProxy.token = token; + } else { + Log.w(TAG, "Token is not renewed. Status: " + status); } - IdentityProxy.token = token; tokenCallBack.onReceiveTokenResult(token, status, message); } diff --git a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/RefreshTokenHandler.java b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/RefreshTokenHandler.java index 4c4aa8d3..03239769 100644 --- a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/RefreshTokenHandler.java +++ b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/RefreshTokenHandler.java @@ -44,7 +44,7 @@ import java.util.Map; /** - * This class handles the entire functionality of OAuth token expiration and + * This class handles the entire functionality of OAuth token expiration and * refresh process. */ @@ -71,24 +71,27 @@ public void obtainNewAccessToken() { return; } - StringRequest request = new StringRequest(Request.Method.POST, IdentityProxy.getInstance().getAccessTokenURL(), - new Response.Listener() { - @Override - public void onResponse(String response) { - Log.d(TAG, response); - } - }, - new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, error.toString()); - if (error.networkResponse != null) { - Log.w(TAG, error.toString() + " Status code: " + error.networkResponse.statusCode); - processTokenResponse(String.valueOf(error.networkResponse.statusCode), - new String(error.networkResponse.data)); - } - } - }) + StringRequest request = new StringRequest(Request.Method.POST, + IdentityProxy.getInstance().getAccessTokenURL(), + new Response.Listener() { + @Override + public void onResponse(String response) { + if (Constants.DEBUG_ENABLED) { + Log.d(TAG, "Token renewal response: " + response); + } + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Log.e(TAG, "Error in token renewal. " + error.toString()); + if (error.networkResponse != null) { + Log.w(TAG, error.toString() + " Status code: " + error.networkResponse.statusCode); + processTokenResponse(String.valueOf(error.networkResponse.statusCode), + new String(error.networkResponse.data)); + } + } + }) { @Override protected Response parseNetworkResponse(NetworkResponse response) { @@ -102,8 +105,8 @@ protected Map getParams() throws AuthFailureError { requestParams.put(Constants.GRANT_TYPE, Constants.REFRESH_TOKEN); requestParams.put(Constants.REFRESH_TOKEN, token.getRefreshToken()); requestParams.put(SCOPE_LABEL, PRODUCTION_LABEL); - if(Constants.DEBUG_ENABLED && token.getRefreshToken() == null) { - Log.d(TAG, "Refresh token is null."); + if(token.getRefreshToken() == null) { + Log.w(TAG, "Refresh token is null."); } return requestParams; } @@ -148,7 +151,7 @@ private void processTokenResponse(String responseCode, String result) { timeToExpireSecond = Integer.parseInt(response.getString(Constants.EXPIRE_LABEL)); Token token = new Token(); long expiresOn = new Date().getTime() - + (timeToExpireSecond - Constants.HttpClient.TOKEN_VALIDITY_PERCENTAGE * timeToExpireSecond / 100) * 1000; + + (timeToExpireSecond - (100 - Constants.HttpClient.TOKEN_VALIDITY_PERCENTAGE) * timeToExpireSecond / 100) * 1000; token.setExpiresOn(new Date(expiresOn)); token.setRefreshToken(refreshToken); token.setAccessToken(accessToken); @@ -168,18 +171,21 @@ private void processTokenResponse(String responseCode, String result) { identityProxy .receiveNewAccessToken(responseCode, Constants.SUCCESS_RESPONSE, token); - + } else if (Constants.ACCESS_FAILURE.equals(responseCode)){ + identityProxy.receiveNewAccessToken(responseCode, Constants.REFRESH_TOKEN_EXPIRED, null); } else if (responseCode != null) { + String errorDescription = Constants.ERROR_LABEL; if (result != null) { JSONObject responseBody = new JSONObject(result); - String errorDescription = - responseBody.getString(Constants.ERROR_DESCRIPTION_LABEL); - identityProxy.receiveNewAccessToken(responseCode, errorDescription, token); + errorDescription = responseBody.getString(Constants.ERROR_DESCRIPTION_LABEL); } + identityProxy.receiveNewAccessToken(responseCode, errorDescription, null); + } else { + identityProxy.receiveNewAccessToken(Constants.CLIENT_ERROR, Constants.ERROR_LABEL, null); } } catch (JSONException e) { - identityProxy.receiveNewAccessToken(responseCode, null, token); - Log.e(TAG, "Invalid JSON." + e); + identityProxy.receiveNewAccessToken(responseCode, e.getMessage(), null); + Log.e(TAG, "Invalid JSON. " + e); } } } diff --git a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/utils/Constants.java b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/utils/Constants.java index 0ac635b0..2867b166 100644 --- a/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/utils/Constants.java +++ b/client/iDPProxy/src/main/java/org/wso2/iot/agent/proxy/utils/Constants.java @@ -98,6 +98,7 @@ private HttpClient(){ public final static String CLIENT_SECRET = "client_secret"; public final static String TOKEN_ENDPOINT = "token_endpoint"; public static enum HTTP_METHODS{GET, POST, DELETE, PUT}; + public static final String CLIENT_ERROR = "-1"; public static final String INTERNAL_SERVER_ERROR = "500"; public static final String ACCESS_FAILURE = "400"; public static final String REQUEST_SUCCESSFUL = "200";