From fd4baed436f684bd567c8f6e335f65bc7c2e8495 Mon Sep 17 00:00:00 2001
From: Luciano Balmaceda <balmacedaluciano@gmail.com>
Date: Thu, 1 Feb 2018 17:20:06 -0300
Subject: [PATCH 1/2] prevent NPE when browser re-triggers authentication

---
 .../android/provider/AuthenticationActivity.java |  6 +++++-
 .../provider/AuthenticationActivityTest.java     | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.java b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.java
index c725df668..242075cd8 100644
--- a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.java
+++ b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.java
@@ -71,7 +71,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
     @Override
     protected void onResume() {
         super.onResume();
-        if (!intentLaunched) {
+        if (!intentLaunched && getIntent().getExtras() == null) {
+            //Activity was launched in an unexpected way
+            finish();
+            return;
+        } else if (!intentLaunched) {
             intentLaunched = true;
             launchAuthenticationIntent();
             return;
diff --git a/auth0/src/test/java/com/auth0/android/provider/AuthenticationActivityTest.java b/auth0/src/test/java/com/auth0/android/provider/AuthenticationActivityTest.java
index f6e066e7c..4a2a73da7 100644
--- a/auth0/src/test/java/com/auth0/android/provider/AuthenticationActivityTest.java
+++ b/auth0/src/test/java/com/auth0/android/provider/AuthenticationActivityTest.java
@@ -70,6 +70,22 @@ private void createActivity(Intent configurationIntent) {
         activityShadow = shadowOf(activity);
     }
 
+    @SuppressWarnings("deprecation")
+    @Test
+    public void shouldFinishGracefullyWhenCalledByError() throws Exception {
+        Intent intent = new Intent(callerActivity, AuthenticationActivity.class);
+        //An invalid call will not pass any expected extras
+        createActivity(intent);
+
+        activityController.create().newIntent(intent).start().resume();
+
+        verifyNoMoreInteractions(customTabsController);
+        assertThat(activity.getDeliveredIntent(), is(nullValue()));
+        assertThat(activity.isFinishing(), is(true));
+
+        activityController.destroy();
+    }
+
     @SuppressWarnings("deprecation")
     @Test
     public void shouldAuthenticateUsingBrowser() throws Exception {

From 4736ef2ec7c642b2aee44f242715827caa4dba16 Mon Sep 17 00:00:00 2001
From: Luciano Balmaceda <balmacedaluciano@gmail.com>
Date: Thu, 1 Feb 2018 17:31:25 -0300
Subject: [PATCH 2/2] remove unwanted NO_HISTORY flag

---
 .../java/com/auth0/android/provider/CustomTabsController.java   | 1 -
 .../com/auth0/android/provider/CustomTabsControllerTest.java    | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java b/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java
index f0c33e09f..22c1c04b0 100644
--- a/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java
+++ b/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java
@@ -144,7 +144,6 @@ public void run() {
                     context.startActivity(intent);
                 } catch (ActivityNotFoundException ignored) {
                     Intent fallbackIntent = new Intent(Intent.ACTION_VIEW, uri);
-                    fallbackIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                     context.startActivity(fallbackIntent);
                 }
             }
diff --git a/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java b/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java
index dad35d8fe..3256ffc19 100644
--- a/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java
+++ b/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java
@@ -256,7 +256,7 @@ public void shouldLaunchUriWithFallbackIfCustomTabIntentFails() throws Exception
         Intent fallbackIntent = intents.get(1);
         assertThat(fallbackIntent.getAction(), is(Intent.ACTION_VIEW));
         assertThat(fallbackIntent.getData(), is(uri));
-        assertThat(fallbackIntent, hasFlag(Intent.FLAG_ACTIVITY_NO_HISTORY));
+        assertThat(fallbackIntent, not(hasFlag(Intent.FLAG_ACTIVITY_NO_HISTORY)));
         assertThat(fallbackIntent.hasExtra(CustomTabsIntent.EXTRA_SESSION), is(false));
         assertThat(fallbackIntent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(false));
     }