Skip to content

Commit

Permalink
Merge pull request #383 from auth0/set-custom-audience
Browse files Browse the repository at this point in the history
Support custom audience for Web Authentication
  • Loading branch information
hzalaz authored Jan 2, 2017
2 parents 9d12294 + aacfcf1 commit 0265dfb
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 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.3.0'
compile 'com.auth0.android:auth0:1.4.0'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.robolectric:robolectric:3.1.2'
Expand Down
11 changes: 11 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 @@ -467,6 +467,17 @@ public Builder withScope(@NonNull String scope) {
return this;
}

/**
* Sets the Audience or API Identifier to request access to when performing the Authentication.
*
* @param audience to use in the Authentication.
* @return the current builder instance
*/
public Builder withAudience(@NonNull String audience) {
options.withAudience(audience);
return this;
}

/**
* Choose a custom Privacy Policy URL to access when the user clicks the link on the Sign Up form.
* The default value is 'https://auth0.com/privacy'
Expand Down
11 changes: 11 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 @@ -365,6 +365,17 @@ public Builder withScope(@NonNull String scope) {
return this;
}

/**
* Sets the Audience or API Identifier to request access to when performing the Authentication.
*
* @param audience to use in the Authentication.
* @return the current builder instance
*/
public Builder withAudience(@NonNull String audience) {
options.withAudience(audience);
return this;
}

/**
* Sets the Connection Scope to request when performing an Authentication with the given Connection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
lockBus = new Bus();
lockBus.register(this);
handler = new Handler(getMainLooper());
webProvider = new WebProvider(options);

setContentView(R.layout.com_auth0_lock_activity_lock_passwordless);
passwordlessSuccessCover = (LinearLayout) findViewById(R.id.com_auth0_lock_link_sent_cover);
Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/java/com/auth0/android/lock/WebProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Intent;
import android.content.UriMatcher;
import android.support.annotation.NonNull;

import com.auth0.android.lock.internal.configuration.Options;
Expand Down Expand Up @@ -48,6 +47,10 @@ public void start(Activity activity, String connection, AuthCallback callback, i
if (scope != null) {
builder.withScope(scope);
}
final String audience = options.getAudience();
if (audience != null) {
builder.withAudience(audience);
}
builder.start(activity, callback, requestCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class Options implements Parcelable {
private String privacyURL;
private String termsURL;
private String scope;
private String audience;

public Options() {
usernameStyle = UsernameStyle.DEFAULT;
Expand Down Expand Up @@ -126,6 +127,7 @@ protected Options(Parcel in) {
privacyURL = in.readString();
termsURL = in.readString();
scope = in.readString();
audience = in.readString();
if (in.readByte() == HAS_DATA) {
connections = new ArrayList<>();
in.readList(connections, String.class.getClassLoader());
Expand Down Expand Up @@ -199,6 +201,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(privacyURL);
dest.writeString(termsURL);
dest.writeString(scope);
dest.writeString(audience);
if (connections == null) {
dest.writeByte((byte) (WITHOUT_DATA));
} else {
Expand Down Expand Up @@ -492,4 +495,13 @@ public void withScope(@NonNull String scope) {
public String getScope() {
return scope;
}

public void withAudience(@NonNull String audience) {
this.audience = audience;
}

@Nullable
public String getAudience() {
return audience;
}
}
4 changes: 4 additions & 0 deletions lib/src/test/java/com/auth0/android/lock/WebProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void shouldStartBrowserWithOptions() throws Exception {
options.withScope("email profile photos");
options.withConnectionScope("my-connection", "the connection scope");
options.setUseBrowser(true);
options.withAudience("https://me.auth0.com/myapi");

AuthCallback callback = mock(AuthCallback.class);
WebProvider webProvider = new WebProvider(options);
Expand All @@ -88,6 +89,7 @@ public void shouldStartBrowserWithOptions() throws Exception {
assertThat(intent.getData(), hasParamWithValue("custom-param-2", "value-2"));
assertThat(intent.getData(), hasParamWithValue("scope", "email profile photos"));
assertThat(intent.getData(), hasParamWithValue("connection_scope", "the connection scope"));
assertThat(intent.getData(), hasParamWithValue("audience", "https://me.auth0.com/myapi"));
assertThat(intent, hasAction(Intent.ACTION_VIEW));
}

Expand All @@ -104,6 +106,7 @@ public void shouldStartWebViewWithOptions() throws Exception {
options.withScope("email profile photos");
options.withConnectionScope("my-connection", "the connection scope");
options.setUseBrowser(false);
options.withAudience("https://me.auth0.com/myapi");

AuthCallback callback = mock(AuthCallback.class);
WebProvider webProvider = new WebProvider(options);
Expand All @@ -126,6 +129,7 @@ public void shouldStartWebViewWithOptions() throws Exception {
assertThat(intent.getData(), hasParamWithValue("custom-param-2", "value-2"));
assertThat(intent.getData(), hasParamWithValue("scope", "email profile photos"));
assertThat(intent.getData(), hasParamWithValue("connection_scope", "the connection scope"));
assertThat(intent.getData(), hasParamWithValue("audience", "https://me.auth0.com/myapi"));
assertThat(intent, hasComponent(WebAuthActivity.class.getName()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ public void shouldSetScope() throws Exception {
assertThat(parceledOptions.getScope(), is("some connection scope"));
}

@Test
public void shouldSetAudience() throws Exception {
options.withAudience("https://domain.auth0.com/users");

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

Options parceledOptions = Options.CREATOR.createFromParcel(parcel);
assertThat(options.getAudience(), is(equalTo("https://domain.auth0.com/users")));
assertThat(parceledOptions.getAudience(), is("https://domain.auth0.com/users"));
}

@SuppressWarnings("ResourceType")
@Test
public void shouldAddAuthStyles() throws Exception {
Expand Down Expand Up @@ -629,6 +642,7 @@ public void shouldSetDefaultValues() throws Exception {
assertThat(options.useLabeledSubmitButton(), is(false));
assertThat(options.hideMainScreenTitle(), is(false));
assertThat(options.getScope(), is(nullValue()));
assertThat(options.getAudience(), is(nullValue()));
assertThat(options.usernameStyle(), is(equalTo(UsernameStyle.DEFAULT)));
assertThat(options.authButtonSize(), is(equalTo(AuthButtonSize.UNSPECIFIED)));
assertThat(options.getTheme(), is(notNullValue()));
Expand Down

0 comments on commit 0265dfb

Please sign in to comment.