Skip to content

Commit

Permalink
Merge pull request #143 from charithag/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamidu Sachith Punchihewa committed Jan 30, 2018
2 parents 90a26a5 + 822f6f0 commit 322c59d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions client/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public void onReceiveAPIResult(Map<String, String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand All @@ -71,24 +71,27 @@ public void obtainNewAccessToken() {
return;
}

StringRequest request = new StringRequest(Request.Method.POST, IdentityProxy.getInstance().getAccessTokenURL(),
new Response.Listener<String>() {
@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<String>() {
@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<String> parseNetworkResponse(NetworkResponse response) {
Expand All @@ -102,8 +105,8 @@ protected Map<String, String> 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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 322c59d

Please sign in to comment.