From fd100a977eb17ad5b3dc3c42f2466c3e6be4c648 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 28 Sep 2016 18:01:11 -0300 Subject: [PATCH] allow to specify additional params. return false for authorize(intent) by default --- .../auth0/android/provider/AuthProvider.java | 31 +++++++++++++++- .../android/provider/AuthProviderTest.java | 37 +++++++++++++------ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/AuthProvider.java b/auth0/src/main/java/com/auth0/android/provider/AuthProvider.java index 1554000ff..351b28abc 100644 --- a/auth0/src/main/java/com/auth0/android/provider/AuthProvider.java +++ b/auth0/src/main/java/com/auth0/android/provider/AuthProvider.java @@ -36,7 +36,9 @@ import com.auth0.android.auth0.R; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** @@ -50,6 +52,7 @@ public abstract class AuthProvider { private final PermissionHandler handler; private AuthCallback callback; private int authenticationRequestCode; + private Map parameters; public AuthProvider() { this(new PermissionHandler()); @@ -57,6 +60,7 @@ public AuthProvider() { AuthProvider(@NonNull PermissionHandler handler) { this.handler = handler; + this.parameters = new HashMap<>(); } /** @@ -135,11 +139,14 @@ public void clearSession() { /** * Finishes the authentication flow by passing the data received in the activity's onNewIntent() callback. * The final authentication result will be delivered to the callback specified when calling start(). + * The default implementation will return false, you need to override it if you want to customize the logic. * * @param intent the data received on the onNewIntent() call * @return true if a result was expected and has a valid format, or false if not. */ - public abstract boolean authorize(@Nullable Intent intent); + public boolean authorize(@Nullable Intent intent) { + return false; + } /** * Defines which Android Manifest Permissions are required by this Identity Provider to work. @@ -149,6 +156,26 @@ public void clearSession() { */ public abstract String[] getRequiredAndroidPermissions(); + /** + * Sets the parameters to send with the authentication request. The user is responsible of calling getParameters() and attaching them in the request. + * By default, this is an empty map. + * + * @param parameters the parameters to use. + */ + public void setParameters(@NonNull Map parameters) { + this.parameters = new HashMap<>(parameters); + } + + /** + * Getter for the parameters to send with the authentication request. + * + * @return the parameters to use. + */ + @NonNull + protected Map getParameters() { + return parameters; + } + /** * Checks if all the required Android Manifest.permissions have already been granted. * @@ -175,7 +202,7 @@ private void requestPermissions(Activity activity, int requestCode) { } @VisibleForTesting - PermissionHandler getPermissionHandler(){ + PermissionHandler getPermissionHandler() { return handler; } diff --git a/auth0/src/test/java/com/auth0/android/provider/AuthProviderTest.java b/auth0/src/test/java/com/auth0/android/provider/AuthProviderTest.java index 1745b9dc1..ecb86ed4c 100644 --- a/auth0/src/test/java/com/auth0/android/provider/AuthProviderTest.java +++ b/auth0/src/test/java/com/auth0/android/provider/AuthProviderTest.java @@ -31,6 +31,8 @@ import android.support.v4.content.PermissionChecker; import android.widget.TextView; +import org.hamcrest.Matcher; +import org.hamcrest.collection.IsMapContaining; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -44,14 +46,19 @@ import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 21, manifest = Config.NONE) @@ -75,7 +82,7 @@ public class AuthProviderTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); processAuthenticationCalled = false; - provider = new AuthProvider(handler){ + provider = new AuthProvider(handler) { @Override protected void requestAuth(Activity activity, int requestCode) { @@ -87,11 +94,6 @@ public boolean authorize(int requestCode, int resultCode, @Nullable Intent inten return false; } - @Override - public boolean authorize(@Nullable Intent intent) { - return false; - } - @Override public String[] getRequiredAndroidPermissions() { return PROVIDER_PERMISSIONS; @@ -111,11 +113,6 @@ public boolean authorize(int requestCode, int resultCode, @Nullable Intent inten return false; } - @Override - public boolean authorize(@Nullable Intent intent) { - return false; - } - @Override public String[] getRequiredAndroidPermissions() { return new String[0]; @@ -196,7 +193,23 @@ public void shouldFailWithDialogWhenPermissionsAreNotGranted() throws Exception TextView messageTV = (TextView) dialog.findViewById(android.R.id.message); assertThat(messageTV.getText().toString(), containsString("Some permissions required by this provider were not granted. You can try to authenticate again or go to " + "the application's permission screen in the phone settings and grant them. The missing permissions are:\n" + "[some, values]")); + } + + @Test + public void shouldSetParameters() throws Exception { + Map params = new HashMap<>(); + params.put("key", "value"); + provider.setParameters(params); + + final Map parameters = provider.getParameters(); + assertThat(parameters, is(notNullValue())); + assertThat(parameters, hasEntry("key", (Object) "value")); + } + @Test + public void shouldReturnFalseWhenCalledWithIntentByDefault() throws Exception { + boolean authorizeResult = provider.authorize(new Intent()); + assertFalse(authorizeResult); } @Test