Skip to content

Commit

Permalink
concurrency bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PierfrancescoSoffritti committed Jun 17, 2016
1 parent 8a8e6cf commit 96e138b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,25 @@ public Date getTokenAcquisitionTime() {
* Other than returning the auth status, this method is responsible for changing the status to expired if expiresIn - 600 seconds are passed
* @return the status of the authentication. A value from {@link AuthStatus}
*/
@AuthStatus int getAuthStatus() {
synchronized @AuthStatus int getAuthStatus() {
if(authStatus == NOT_AUTHENTICATED)
return authStatus;

// the access token is refreshed offset seconds before expiring
long currentTime = new Date().getTime() / 1000;
long tokenAcquiredTime = tokenAcquisitionTime.getTime() / 1000;
int offset = 600; // 10 minutes
long currentTimeMillisec = new Date().getTime() / 1000;
long tokenAcquisitionTimeMillisec = this.tokenAcquisitionTime.getTime() / 1000;
int offsetMillisec = 600; // 10 minutes

if(currentTime - tokenAcquiredTime >= expiresIn - offset) {
if(currentTimeMillisec - tokenAcquisitionTimeMillisec >= expiresIn - offsetMillisec)
authStatus = TOKEN_EXPIRED;

accessToken = null;
expiresIn = -1;
tokenAcquisitionTime = null;
}

return authStatus;
}

/**
* Use this method to store new credentials.
*/
void authenticate(@NonNull String accessToken, @NonNull String refreshToken, int expiresIn, @NonNull Date tokenAcquisitionTime) {
synchronized void authenticate(@NonNull String accessToken, @NonNull String refreshToken, int expiresIn, @NonNull Date tokenAcquisitionTime) {
if(accessToken.length() == 0 || refreshToken.length() == 0)
throw new IllegalArgumentException("accessToken.isEmpty() || refreshToken.isEmpty()");

Expand All @@ -126,14 +121,14 @@ void authenticate(@NonNull String accessToken, @NonNull String refreshToken, int
/**
* Use this method to store new credentials.
*/
void authenticate(String accessToken, String refreshToken, int expiresIn) {
synchronized void authenticate(String accessToken, String refreshToken, int expiresIn) {
authenticate(accessToken, refreshToken, expiresIn, new Date());
}

/**
* Use this method to set a new access token.
*/
void setNewAccessToken(@NonNull String accessToken, int expiresIn) {
synchronized void setNewAccessToken(@NonNull String accessToken, int expiresIn) {
if(accessToken.length() == 0)
throw new IllegalArgumentException("accessToken.isEmpty()");

Expand All @@ -150,7 +145,7 @@ void setNewAccessToken(@NonNull String accessToken, int expiresIn) {
/**
* Use this method to delete all the stored credentials.
*/
void clear() {
synchronized void clear() {
this.accessToken = null;
this.refreshToken = null;
this.expiresIn = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void persistUser(CredentialStore credentialStore) {
.putInt(EXPIRES_IN_TOKEN_KEY, credentialStore.getExpiresIn())
.putString(TOKEN_ACQUIRE_TIME_KEY, credentialStore.getTokenAcquisitionTime().getTime()+"")
.putInt(AUTH_STATUS_KEY, credentialStore.getAuthStatus())
.apply();
.commit();
}

@Override
Expand Down

0 comments on commit 96e138b

Please sign in to comment.