Skip to content

Commit

Permalink
Handle security exception inside the thread block
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj committed Aug 1, 2023
1 parent b9ad3f5 commit b24f2cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,8 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, final R
Log.v(TAG, "Custom Tab Context was no longer valid.");
return;
}
Thread.UncaughtExceptionHandler exceptionHandler = (th, ex) -> {
AuthenticationException e = new AuthenticationException(
"a0.browser_not_available", "Error launching browser for authentication", new Exception(ex));
CommonThreadSwitcher.getInstance().mainThread(() -> failureCallback.apply(e));
};

Thread thread = new Thread(() -> {
new Thread(() -> {
try {
if (launchAsTwa) {
this.launchedAsTwa = true;
Expand All @@ -139,10 +134,12 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, final R
}
} catch (ActivityNotFoundException ex) {
Log.e(TAG, "Could not find any Browser application installed in this device to handle the intent.");
} catch (SecurityException ex) {
AuthenticationException e = new AuthenticationException(
"a0.browser_not_available", "Error launching browser for authentication", ex);
CommonThreadSwitcher.getInstance().mainThread(() -> failureCallback.apply(e));
}
});
thread.setUncaughtExceptionHandler(exceptionHandler);
thread.start();
}).start();
}

private void launchAsDefault(Context context, Uri uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.Is.isA;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
Expand All @@ -50,6 +51,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.auth0.android.authentication.AuthenticationException;
import com.google.androidbrowserhelper.trusted.TwaLauncher;
import com.google.androidbrowserhelper.trusted.splashscreens.SplashScreenStrategy;

Expand Down Expand Up @@ -305,7 +307,10 @@ public void shouldThrowExceptionIfFailedToLaunchBecauseOfException() {
doThrow(e)
.when(context).startActivity(any(Intent.class));
controller.launchUri(uri, false, (ex) -> {
assertThat(ex, is(e));
assertThat(ex, isA(AuthenticationException.class));
assertThat(ex.getCause(), is(eq(e)));
assertThat(ex.getCode(), is("a0.browser_not_available"));
assertThat(ex.getDescription(), is("Error launching browser for authentication"));
assertThat(Looper.myLooper(), is(Looper.getMainLooper()));
});
}
Expand Down

0 comments on commit b24f2cd

Please sign in to comment.