Skip to content

Commit

Permalink
add connection scope setter
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Oct 14, 2016
1 parent 2aece80 commit 2a8bc26
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* WebAuthProvider.java
*
Expand Down Expand Up @@ -35,7 +34,6 @@
import android.util.Log;

import com.auth0.android.Auth0;
import com.auth0.android.auth0.R;
import com.auth0.android.authentication.AuthenticationAPIClient;
import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.result.Credentials;
Expand Down Expand Up @@ -66,6 +64,7 @@ public class WebAuthProvider {
private static final String KEY_CLIENT_ID = "client_id";
private static final String KEY_REDIRECT_URI = "redirect_uri";
private static final String KEY_SCOPE = "scope";
private static final String KEY_CONNECTION_SCOPE = "connection_scope";
private static final String KEY_TELEMETRY = "auth0Client";

private static final String ERROR_VALUE_ACCESS_DENIED = "access_denied";
Expand All @@ -87,6 +86,7 @@ public class WebAuthProvider {
private boolean useBrowser;
private String state;
private String scope;
private String connectionScope;
private boolean useCodeGrant;
private Map<String, Object> parameters;
private String connectionName;
Expand All @@ -105,6 +105,7 @@ public static class Builder {
private boolean useFullscreen;
private String state;
private String scope;
private String connectionScope;
private boolean useCodeGrant;
private Map<String, Object> parameters;
private String connectionName;
Expand All @@ -127,6 +128,7 @@ public static class Builder {
* If the class authenticates with an external browser or not.
*
* @param useBrowser if the authentication is handled in a Browser.
* @return the current builder instance
*/
public Builder useBrowser(boolean useBrowser) {
this.useBrowser = useBrowser;
Expand All @@ -138,6 +140,7 @@ public Builder useBrowser(boolean useBrowser) {
* Browser authentication.
*
* @param useFullscreen if the activity should be fullscreen or not.
* @return the current builder instance
*/
public Builder useFullscreen(boolean useFullscreen) {
this.useFullscreen = useFullscreen;
Expand All @@ -148,6 +151,7 @@ public Builder useFullscreen(boolean useFullscreen) {
* Use a custom state in the requests
*
* @param state to use in the requests
* @return the current builder instance
*/
public Builder withState(@NonNull String state) {
this.state = state;
Expand All @@ -158,16 +162,29 @@ public Builder withState(@NonNull String state) {
* Give a scope for this request.
*
* @param scope to request.
* @return the current builder instance
*/
public Builder withScope(@NonNull String scope) {
this.scope = scope;
return this;
}

/**
* Give a connection scope for this request.
*
* @param connectionScope to request.
* @return the current builder instance
*/
public Builder withConnectionScope(@NonNull String connectionScope) {
this.connectionScope = connectionScope;
return this;
}

/**
* Choose the grant type for this request.
*
* @param useCodeGrant whether use code or implicit grant type
* @return the current builder instance
*/
public Builder useCodeGrant(boolean useCodeGrant) {
this.useCodeGrant = useCodeGrant;
Expand All @@ -178,6 +195,7 @@ public Builder useCodeGrant(boolean useCodeGrant) {
* Use extra parameters on the request
*
* @param parameters to add
* @return the current builder instance
*/
public Builder withParameters(@Nullable Map<String, Object> parameters) {
this.parameters = parameters != null ? new HashMap<>(parameters) : new HashMap<String, Object>();
Expand All @@ -188,6 +206,7 @@ public Builder withParameters(@Nullable Map<String, Object> parameters) {
* Use the given connection instead of the default 'auth0'.
*
* @param connectionName to use
* @return the current builder instance
*/
public Builder withConnection(@NonNull String connectionName) {
this.connectionName = connectionName;
Expand Down Expand Up @@ -215,6 +234,7 @@ public void start(@NonNull Activity activity, @NonNull AuthCallback callback, in
webAuth.useFullscreen = useFullscreen;
webAuth.state = state;
webAuth.scope = scope;
webAuth.connectionScope = connectionScope;
webAuth.useCodeGrant = useCodeGrant;
webAuth.parameters = parameters;
webAuth.connectionName = connectionName;
Expand Down Expand Up @@ -370,6 +390,7 @@ private Uri buildAuthorizeUri() {

final Map<String, String> queryParameters = new HashMap<>();
queryParameters.put(KEY_SCOPE, scope);
queryParameters.put(KEY_CONNECTION_SCOPE, connectionScope);
queryParameters.put(KEY_RESPONSE_TYPE, RESPONSE_TYPE_TOKEN);

if (shouldUsePKCE()) {
Expand Down Expand Up @@ -436,6 +457,10 @@ String getScope() {
return scope;
}

String getConnectionScope() {
return connectionScope;
}

boolean useCodeGrant() {
return useCodeGrant;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class WebAuthProviderTest {
private static final int REQUEST_CODE = 11;
private static final String CONNECTION_NAME = "connection";
private static final String SCOPE = "scope";
private static final String CONNECTION_SCOPE = "connection_scope";
private static final String STATE = "state";
private static final String USERNAME_PASSWORD_AUTHENTICATION_CONNECTION = "Username-Password-Authentication";
private static final String SCOPE_OPEN_ID = "openid";
Expand Down Expand Up @@ -121,6 +122,7 @@ public void shouldHaveDefaultsOnInit() throws Exception {
assertThat(instance.useFullscreen(), is(false));
assertThat(instance.getConnection(), is(not(Matchers.isEmptyOrNullString())));
assertThat(instance.getScope(), is(not(Matchers.isEmptyOrNullString())));
assertThat(instance.getConnectionScope(), is(nullValue()));
assertThat(instance.getState(), is(not(Matchers.isEmptyOrNullString())));
assertThat(instance.getParameters(), is(notNullValue()));
assertThat(instance.getParameters().isEmpty(), is(true));
Expand Down Expand Up @@ -155,6 +157,7 @@ public void shouldConfigureAfterInit() throws Exception {
.useFullscreen(true)
.withConnection(CONNECTION_NAME)
.withScope(SCOPE)
.withConnectionScope(CONNECTION_SCOPE)
.withState(STATE)
.withParameters(parameters)
.start(activity, callback, REQUEST_CODE);
Expand All @@ -165,6 +168,7 @@ public void shouldConfigureAfterInit() throws Exception {
assertThat(instance.useFullscreen(), is(true));
assertThat(instance.getConnection(), is(CONNECTION_NAME));
assertThat(instance.getScope(), is(SCOPE));
assertThat(instance.getConnectionScope(), is(CONNECTION_SCOPE));
assertThat(instance.getState(), is(STATE));
assertThat(instance.getParameters(), is(notNullValue()));
assertThat(instance.getParameters(), Matchers.hasEntry("key", (Object) "value"));
Expand Down Expand Up @@ -194,6 +198,7 @@ public void shouldBuildAuthorizeURI() throws Exception {
.withConnection("my-connection")
.withState("a-state")
.withScope("super_scope")
.withConnectionScope("super_connection_scope")
.withParameters(parameters)
.start(activity, callback, REQUEST_CODE);

Expand All @@ -209,6 +214,7 @@ public void shouldBuildAuthorizeURI() throws Exception {
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("connection", "my-connection"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("state", "a-state"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("scope", "super_scope"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("connection_scope", "super_connection_scope"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("custom_param_1", "custom_value_1"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("custom_param_2", "custom_value_2"));
assertThat(intentCaptor.getValue().getData(), hasParamWithValue("client_id", account.getClientId()));
Expand Down

0 comments on commit 2a8bc26

Please sign in to comment.