Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to prefer OAuth 2.0 endpoints for authentication #378

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1.2'
compile 'com.auth0.android:auth0:1.3.0'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.robolectric:robolectric:3.1.2'
Expand Down
12 changes: 12 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 @@ -264,6 +264,18 @@ public Builder useImplicitGrant(boolean useImplicitGrant) {
return this;
}

/**
* Use OAuth 2.0 Authorization API. You will need to enable this setting in the Dashboard first. Go to Account (top right), Account Settings, click Advanced and check the toggle at the bottom.
* Default is {@code false}
*
* @param use if Lock will use the OAuth 2.0 API or the previous implementation.
* @return the current Builder instance
*/
public Builder useOAuth2(boolean use) {
options.useOAuth2API(use);
return this;
}

/**
* Whether the LockActivity can be closed when pressing the Back key or not.
*
Expand Down
12 changes: 12 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 @@ -262,6 +262,18 @@ public Builder useImplicitGrant(boolean useImplicitGrant) {
return this;
}

/**
* Use OAuth 2.0 Authorization API. You will need to enable this setting in the Dashboard first. Go to Account (top right), Account Settings, click Advanced and check the toggle at the bottom.
* Default is {@code false}
*
* @param use if Lock will use the OAuth 2.0 API or the previous implementation.
* @return the current Builder instance
*/
public Builder useOAuth2(boolean use) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave passwordless out of this for now

options.useOAuth2API(use);
return this;
}

/**
* Whether the PasswordlessLockActivity can be closed when pressing the Back key or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class Options implements Parcelable {
private boolean loginAfterSignUp;
private boolean mustAcceptTerms;
private boolean useLabeledSubmitButton;
private boolean useOAuth2API;
private String defaultDatabaseConnection;
private List<String> connections;
private List<String> enterpriseConnectionsUsingWebForm;
Expand Down Expand Up @@ -116,6 +117,7 @@ protected Options(Parcel in) {
mustAcceptTerms = in.readByte() != WITHOUT_DATA;
useCodePasswordless = in.readByte() != WITHOUT_DATA;
useLabeledSubmitButton = in.readByte() != WITHOUT_DATA;
useOAuth2API = in.readByte() != WITHOUT_DATA;
defaultDatabaseConnection = in.readString();
usernameStyle = in.readInt();
initialScreen = in.readInt();
Expand Down Expand Up @@ -188,6 +190,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (mustAcceptTerms ? HAS_DATA : WITHOUT_DATA));
dest.writeByte((byte) (useCodePasswordless ? HAS_DATA : WITHOUT_DATA));
dest.writeByte((byte) (useLabeledSubmitButton ? HAS_DATA : WITHOUT_DATA));
dest.writeByte((byte) (useOAuth2API ? HAS_DATA : WITHOUT_DATA));
dest.writeString(defaultDatabaseConnection);
dest.writeInt(usernameStyle);
dest.writeInt(initialScreen);
Expand Down Expand Up @@ -388,7 +391,9 @@ public void setLoginAfterSignUp(boolean loginAfterSignUp) {
}

public AuthenticationAPIClient getAuthenticationAPIClient() {
return new AuthenticationAPIClient(account);
AuthenticationAPIClient client = new AuthenticationAPIClient(account);
client.setOAuth2Preferred(useOAuth2API);
return client;
}

public void setUseCodePasswordless(boolean useCode) {
Expand Down Expand Up @@ -481,4 +486,8 @@ public void withScope(@NonNull String scope) {
public String getScope() {
return scope;
}

public void setUseOAuth2API(boolean use) {
this.useOAuth2API = use;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class MockAuthenticationRequest implements AuthenticationRequest {
String grantType;
String connection;
String scope;
String audience;
String device;
String accessToken;
HashMap<String, Object> parameters;
Expand Down Expand Up @@ -45,6 +46,12 @@ public AuthenticationRequest setDevice(String device) {
return this;
}

@Override
public AuthenticationRequest setAudience(String audience) {
this.audience = audience;
return this;
}

@Override
public AuthenticationRequest setAccessToken(String accessToken) {
this.accessToken = accessToken;
Expand All @@ -60,7 +67,7 @@ public AuthenticationRequest addAuthenticationParameters(Map<String, Object> par
@Override
public void start(BaseCallback<Credentials, AuthenticationException> callback) {
this.callback = callback;
this.started=true;
this.started = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ public void shouldUseWebview() throws Exception {
assertThat(options.useBrowser(), is(equalTo(parceledOptions.useBrowser())));
}

@Test
public void shouldUseOAuth2API() throws Exception {
options.setUseOAuth2API(true);

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

Options parceledOptions = Options.CREATOR.createFromParcel(parcel);
assertThat(options.getAuthenticationAPIClient(), is(notNullValue()));
assertThat(options.getAuthenticationAPIClient().isOAuth2Preferred(), is(true));
assertThat(parceledOptions.getAuthenticationAPIClient(), is(notNullValue()));
assertThat(parceledOptions.getAuthenticationAPIClient().isOAuth2Preferred(), is(true));
}

@Test
public void shouldUseLabeledSubmitButton() throws Exception {
options.setUseLabeledSubmitButton(true);
Expand Down