Skip to content

Commit

Permalink
Merge pull request #361 from auth0/add-connection-scopes
Browse files Browse the repository at this point in the history
Support connection_scope for OAuth Connections
  • Loading branch information
hzalaz authored Oct 24, 2016
2 parents 3240e67 + 9d2201a commit b13cc42
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
compile 'com.android.support:design:24.2.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup:otto:1.3.8'
compile 'com.auth0.android:auth0:1.0.0'
compile 'com.auth0.android:auth0:1.1.0'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.robolectric:robolectric:3.1.2'
Expand Down
19 changes: 19 additions & 0 deletions lib/src/main/java/com/auth0/android/lock/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,25 @@ public Builder setMustAcceptTerms(boolean mustAcceptTerms) {
return this;
}

/**
* Sets the Connection Scope to request when performing an Authentication with the given Connection.
*
* @param connectionName to which specify the scopes.
* @param scope recognized by this specific authentication provider.
* @return the current builder instance
*/
public Builder withConnectionScope(@NonNull String connectionName, @NonNull String... scope) {
StringBuilder sb = new StringBuilder();
for (String s : scope) {
sb.append(s.trim()).append(",");
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
options.withConnectionScope(connectionName, sb.toString());
}
return this;
}

private List<CustomField> removeDuplicatedKeys(List<CustomField> customFields) {
int originalSize = customFields.size();
final List<CustomField> withoutDuplicates = new ArrayList<>();
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/java/com/auth0/android/lock/LockActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ public void onOAuthAuthenticationRequest(OAuthLoginEvent event) {
Log.d(TAG, "Couldn't find an specific provider, using the default: " + WebAuthProvider.class.getSimpleName());
WebAuthProvider.init(options.getAccount())
.useBrowser(options.useBrowser())
.withConnectionScope(options.getConnectionsScope().get(connection))
.withParameters(options.getAuthenticationParameters())
.withConnection(connection)
.start(this, authProviderCallback, WEB_AUTH_REQUEST_CODE);
Expand Down
19 changes: 19 additions & 0 deletions lib/src/main/java/com/auth0/android/lock/PasswordlessLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,24 @@ public Builder withAuthHandlers(@NonNull AuthHandler... handlers) {
AuthResolver.setAuthHandlers(Arrays.asList(handlers));
return this;
}

/**
* Sets the Connection Scope to request when performing an Authentication with the given Connection.
*
* @param connectionName to which specify the scopes.
* @param scope recognized by this specific authentication provider.
* @return the current builder instance
*/
public Builder withConnectionScope(@NonNull String connectionName, @NonNull String... scope) {
StringBuilder sb = new StringBuilder();
for (String s : scope) {
sb.append(s.trim()).append(",");
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
options.withConnectionScope(connectionName, sb.toString());
}
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

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

public class PasswordlessLockActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
Expand Down Expand Up @@ -480,6 +481,7 @@ public void onOAuthAuthenticationRequest(OAuthLoginEvent event) {
Log.d(TAG, "Couldn't find an specific provider, using the default: " + WebAuthProvider.class.getSimpleName());
WebAuthProvider.init(options.getAccount())
.useBrowser(options.useBrowser())
.withConnectionScope(options.getConnectionsScope().get(event.getConnection()))
.withParameters(options.getAuthenticationParameters())
.withConnection(event.getConnection())
.start(this, authProviderCallback, WEB_AUTH_REQUEST_CODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Options implements Parcelable {
private static final int WITHOUT_DATA = 0x00;
private static final int HAS_DATA = 0x01;
private static final String KEY_AUTHENTICATION_PARAMETERS = "authenticationParameters";
private static final String KEY_CONNECTIONS_SCOPE = "connectionsScope";
private static final String SCOPE_KEY = "scope";
private static final String DEVICE_KEY = "device";
private static final String SCOPE_OFFLINE_ACCESS = "offline_access";
Expand All @@ -76,6 +77,7 @@ public class Options implements Parcelable {
private List<String> enterpriseConnectionsUsingWebForm;
private HashMap<String, Integer> authStyles;
private HashMap<String, Object> authenticationParameters;
private HashMap<String, String> connectionsScope;
private List<CustomField> customFields;
private int initialScreen;
private Theme theme;
Expand All @@ -94,6 +96,7 @@ public Options() {
usePKCE = true;
authenticationParameters = new HashMap<>();
authStyles = new HashMap<>();
connectionsScope = new HashMap<>();
customFields = new ArrayList<>();
theme = Theme.newBuilder().build();
}
Expand Down Expand Up @@ -149,6 +152,13 @@ protected Options(Parcel in) {
} else {
authStyles = null;
}
if (in.readByte() == HAS_DATA) {
// FIXME this is something to improve
Bundle mapBundle = in.readBundle();
connectionsScope = (HashMap<String, String>) mapBundle.getSerializable(KEY_CONNECTIONS_SCOPE);
} else {
connectionsScope = null;
}
if (in.readByte() == HAS_DATA) {
customFields = new ArrayList<>();
in.readList(customFields, CustomField.class.getClassLoader());
Expand Down Expand Up @@ -210,6 +220,15 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeList(new ArrayList<>(authStyles.keySet()));
dest.writeList(new ArrayList<>(authStyles.values()));
}
if (connectionsScope == null) {
dest.writeByte((byte) (WITHOUT_DATA));
} else {
dest.writeByte((byte) (HAS_DATA));
// FIXME this is something to improve
Bundle mapBundle = new Bundle();
mapBundle.putSerializable(KEY_CONNECTIONS_SCOPE, connectionsScope);
dest.writeBundle(mapBundle);
}
if (customFields == null) {
dest.writeByte((byte) (WITHOUT_DATA));
} else {
Expand Down Expand Up @@ -438,4 +457,13 @@ public void setUseLabeledSubmitButton(boolean useLabeledSubmitButton) {
public boolean useLabeledSubmitButton() {
return useLabeledSubmitButton;
}

public void withConnectionScope(@NonNull String connectionName, @NonNull String scope) {
connectionsScope.put(connectionName, scope);
}

@NonNull
public Map<String, String> getConnectionsScope() {
return connectionsScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.auth0.android.lock.utils.CustomField;
import com.auth0.android.lock.utils.CustomField.FieldType;

import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.collection.IsMapContaining;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -32,6 +33,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -476,6 +478,25 @@ public void shouldSetAuthenticationParameters() throws Exception {
assertThat(options.getAuthenticationParameters(), is(equalTo(parceledOptions.getAuthenticationParameters())));
}

@Test
public void shouldSetConnectionScope() throws Exception {
options.withConnectionScope("some_connection", "scope for some connection");
options.withConnectionScope("other_connection", "scope for other connection");

Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

Options parceledOptions = Options.CREATOR.createFromParcel(parcel);
assertThat(options.getConnectionsScope(), is(equalTo(parceledOptions.getConnectionsScope())));
assertThat(options.getConnectionsScope().size(), is(2));
assertThat(options.getConnectionsScope(), hasEntry("some_connection", "scope for some connection"));
assertThat(options.getConnectionsScope(), hasEntry("other_connection", "scope for other connection"));
assertThat(parceledOptions.getConnectionsScope().size(), is(2));
assertThat(parceledOptions.getConnectionsScope(), hasEntry("some_connection", "scope for some connection"));
assertThat(parceledOptions.getConnectionsScope(), hasEntry("other_connection", "scope for other connection"));
}

@SuppressWarnings("ResourceType")
@Test
public void shouldAddAuthStyles() throws Exception {
Expand All @@ -489,13 +510,13 @@ public void shouldAddAuthStyles() throws Exception {

Options parceledOptions = Options.CREATOR.createFromParcel(parcel);
assertThat(options.getAuthStyles().size(), is(3));
assertThat(options.getAuthStyles(), is(IsMapContaining.hasEntry("firstConnection", 1)));
assertThat(options.getAuthStyles(), is(IsMapContaining.hasEntry("secondConnection", 2)));
assertThat(options.getAuthStyles(), is(IsMapContaining.hasEntry("thirdConnection", 3)));
assertThat(options.getAuthStyles(), is(hasEntry("firstConnection", 1)));
assertThat(options.getAuthStyles(), is(hasEntry("secondConnection", 2)));
assertThat(options.getAuthStyles(), is(hasEntry("thirdConnection", 3)));
assertThat(parceledOptions.getAuthStyles().size(), is(3));
assertThat(parceledOptions.getAuthStyles(), is(IsMapContaining.hasEntry("firstConnection", 1)));
assertThat(parceledOptions.getAuthStyles(), is(IsMapContaining.hasEntry("secondConnection", 2)));
assertThat(parceledOptions.getAuthStyles(), is(IsMapContaining.hasEntry("thirdConnection", 3)));
assertThat(parceledOptions.getAuthStyles(), is(hasEntry("firstConnection", 1)));
assertThat(parceledOptions.getAuthStyles(), is(hasEntry("secondConnection", 2)));
assertThat(parceledOptions.getAuthStyles(), is(hasEntry("thirdConnection", 3)));
}

@Test
Expand Down

0 comments on commit b13cc42

Please sign in to comment.