Skip to content

Commit

Permalink
allow to specify additional params. return false for authorize(intent…
Browse files Browse the repository at this point in the history
…) by default
  • Loading branch information
lbalmaceda committed Sep 28, 2016
1 parent d19e531 commit fd100a9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
31 changes: 29 additions & 2 deletions auth0/src/main/java/com/auth0/android/provider/AuthProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

import com.auth0.android.auth0.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
Expand All @@ -50,13 +52,15 @@ public abstract class AuthProvider {
private final PermissionHandler handler;
private AuthCallback callback;
private int authenticationRequestCode;
private Map<String, Object> parameters;

public AuthProvider() {
this(new PermissionHandler());
}

AuthProvider(@NonNull PermissionHandler handler) {
this.handler = handler;
this.parameters = new HashMap<>();
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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<String, Object> parameters) {
this.parameters = new HashMap<>(parameters);
}

/**
* Getter for the parameters to send with the authentication request.
*
* @return the parameters to use.
*/
@NonNull
protected Map<String, Object> getParameters() {
return parameters;
}

/**
* Checks if all the required Android Manifest.permissions have already been granted.
*
Expand All @@ -175,7 +202,7 @@ private void requestPermissions(Activity activity, int requestCode) {
}

@VisibleForTesting
PermissionHandler getPermissionHandler(){
PermissionHandler getPermissionHandler() {
return handler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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<String, Object> params = new HashMap<>();
params.put("key", "value");
provider.setParameters(params);

final Map<String, Object> 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
Expand Down

0 comments on commit fd100a9

Please sign in to comment.