Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve consistency around Expires At in CredentialsManager #295

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,13 @@ private void continueGetCredentials(final BaseCallback<Credentials, CredentialsM
return;
}
final Credentials credentials = gson.fromJson(json, Credentials.class);
if (isEmpty(credentials.getAccessToken()) && isEmpty(credentials.getIdToken()) || credentials.getExpiresAt() == null) {
Long expiresAt = storage.retrieveLong(KEY_EXPIRES_AT);
if (isEmpty(credentials.getAccessToken()) && isEmpty(credentials.getIdToken()) || expiresAt == null) {
callback.onFailure(new CredentialsManagerException("No Credentials were previously set."));
decryptCallback = null;
return;
}
if (credentials.getExpiresAt().getTime() > getCurrentTimeInMillis()) {
if (expiresAt > getCurrentTimeInMillis()) {
callback.onSuccess(credentials);
decryptCallback = null;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ public void shouldRequireAuthenticationIfAPI23AndLockScreenEnabled() {
public void shouldGetCredentialsAfterAuthentication() {
Date expiresAt = new Date(CredentialsMock.CURRENT_TIME_MS + 123456L * 1000);
insertTestCredentials(true, true, false, expiresAt);
when(storage.retrieveLong("com.auth0.credentials_expires_at")).thenReturn(expiresAt.getTime());

//Require authentication
Activity activity = spy(Robolectric.buildActivity(Activity.class).create().start().resume().get());
Expand Down Expand Up @@ -750,6 +751,36 @@ public void shouldGetCredentialsAfterAuthentication() {
assertThat(retryCheck, is(false));
}

@Test
public void shouldNotGetCredentialsWhenCredentialsHaveExpired() {
Date credentialsExpiresAt = new Date(CredentialsMock.CURRENT_TIME_MS + 123456L * 1000);
Date storedExpiresAt = new Date(CredentialsMock.CURRENT_TIME_MS - 60L * 1000);
insertTestCredentials(true, true, false, credentialsExpiresAt);
when(storage.retrieveLong("com.auth0.credentials_expires_at")).thenReturn(storedExpiresAt.getTime());

//Require authentication
Activity activity = spy(Robolectric.buildActivity(Activity.class).create().start().resume().get());
KeyguardManager kService = mock(KeyguardManager.class);
when(activity.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
when(kService.isKeyguardSecure()).thenReturn(true);
Intent confirmCredentialsIntent = mock(Intent.class);
when(kService.createConfirmDeviceCredentialIntent("theTitle", "theDescription")).thenReturn(confirmCredentialsIntent);
boolean willRequireAuthentication = manager.requireAuthentication(activity, 123, "theTitle", "theDescription");
assertThat(willRequireAuthentication, is(true));

manager.getCredentials(callback);

//Should fail because of expired credentials
verify(callback).onFailure(exceptionCaptor.capture());
CredentialsManagerException exception = exceptionCaptor.getValue();
assertThat(exception, is(notNullValue()));
assertThat(exception.getMessage(), is("No Credentials were previously set."));
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved

//A second call to checkAuthenticationResult should fail as callback is set to null
final boolean retryCheck = manager.checkAuthenticationResult(123, Activity.RESULT_OK);
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
assertThat(retryCheck, is(false));
}

@Test
public void shouldNotGetCredentialsAfterCanceledAuthentication() {
Date expiresAt = new Date(CredentialsMock.CURRENT_TIME_MS + 123456L * 1000);
Expand Down