diff --git a/README.md b/README.md index a2de3cfb5..3804c5f87 100644 --- a/README.md +++ b/README.md @@ -468,8 +468,15 @@ authentication }); ``` -3. **Retrieve credentials** - Existing credentials will be returned if they are still valid, otherwise the `refresh_token` will be used to attempt to renew them. If the `expires_in` or both the `access_token` and `id_token` values are missing, the method will throw a `CredentialsManagerException`. The same will happen if the credentials have expired and there's no `refresh_token` available. +3. **Check credentials existence** +There are cases were you just want to check if a user session is still valid (i.e. to know if you should present the login screen or the main screen). For convenience we include a `hasValidCredentials` method that can let you know in advance if a non-expired token is available without making an additional network call. The same rules of the `getCredentials` method apply: + +```java +boolean authenticated = manager.hasValidCredentials(); +``` + +4. **Retrieve credentials** +Existing credentials will be returned if they are still valid, otherwise the `refresh_token` will be used to attempt to renew them. If the `expires_in` or both the `access_token` and `id_token` values are missing, the method will throw a `CredentialsManagerException`. The same will happen if the credentials have expired and there's no `refresh_token` available. ```java manager.getCredentials(new BaseCallback(){ @@ -484,6 +491,13 @@ manager.getCredentials(new BaseCallbackis(authenticationException)); assertThat(exception.getMessage(), is("An error occurred while trying to use the Refresh Token to renew the Credentials.")); } + + @Test + public void shouldClearCredentials() throws Exception { + manager.clearCredentials(); + + verify(storage).remove("com.auth0.id_token"); + verify(storage).remove("com.auth0.access_token"); + verify(storage).remove("com.auth0.refresh_token"); + verify(storage).remove("com.auth0.token_type"); + verify(storage).remove("com.auth0.expires_at"); + verify(storage).remove("com.auth0.scope"); + verifyNoMoreInteractions(storage); + } + + @Test + public void shouldHaveCredentialsWhenTokenHasNotExpired() throws Exception { + long expirationTime = CredentialsMock.CURRENT_TIME_MS + 123456L * 1000; + when(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken"); + when(storage.retrieveString("com.auth0.access_token")).thenReturn(null); + assertThat(manager.hasValidCredentials(), is(true)); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn(null); + when(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken"); + assertThat(manager.hasValidCredentials(), is(true)); + } + + @Test + public void shouldNotHaveCredentialsWhenTokenHasExpiredAndNoRefreshTokenIsAvailable() throws Exception { + long expirationTime = CredentialsMock.CURRENT_TIME_MS; //Same as current time --> expired + when(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime); + when(storage.retrieveString("com.auth0.refresh_token")).thenReturn(null); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken"); + when(storage.retrieveString("com.auth0.access_token")).thenReturn(null); + assertFalse(manager.hasValidCredentials()); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn(null); + when(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken"); + assertFalse(manager.hasValidCredentials()); + } + + @Test + public void shouldHaveCredentialsWhenTokenHasExpiredButRefreshTokenIsAvailable() throws Exception { + long expirationTime = CredentialsMock.CURRENT_TIME_MS; //Same as current time --> expired + when(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime); + when(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken"); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken"); + when(storage.retrieveString("com.auth0.access_token")).thenReturn(null); + assertThat(manager.hasValidCredentials(), is(true)); + + when(storage.retrieveString("com.auth0.id_token")).thenReturn(null); + when(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken"); + assertThat(manager.hasValidCredentials(), is(true)); + } + + @Test + public void shouldNotHaveCredentialsWhenAccessTokenAndIdTokenAreMissing() throws Exception { + when(storage.retrieveString("com.auth0.id_token")).thenReturn(null); + when(storage.retrieveString("com.auth0.access_token")).thenReturn(null); + + assertFalse(manager.hasValidCredentials()); + } } \ No newline at end of file diff --git a/auth0/src/test/java/com/auth0/android/authentication/storage/SharedPreferencesStorageTest.java b/auth0/src/test/java/com/auth0/android/authentication/storage/SharedPreferencesStorageTest.java index 727361f4e..04768be12 100644 --- a/auth0/src/test/java/com/auth0/android/authentication/storage/SharedPreferencesStorageTest.java +++ b/auth0/src/test/java/com/auth0/android/authentication/storage/SharedPreferencesStorageTest.java @@ -186,4 +186,16 @@ public void shouldRetrieveIntegerValueFromPreferences() throws Exception { assertThat(value, is(123)); } + + //Remove + + @SuppressWarnings("ConstantConditions") + @Test + public void shouldRemovePreferencesKey() throws Exception { + SharedPreferencesStorage storage = new SharedPreferencesStorage(context); + storage.remove("name"); + verify(sharedPreferencesEditor).remove("name"); + verify(sharedPreferencesEditor).apply(); + } + } \ No newline at end of file