Skip to content

Commit

Permalink
allow to specify audience on the AuthenticationRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Nov 24, 2016
1 parent 239a4d0 commit eca69f4
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ParameterBuilder {
public static final String CLIENT_ID_KEY = "client_id";
public static final String GRANT_TYPE_KEY = "grant_type";
public static final String DEVICE_KEY = "device";
public static final String AUDIENCE_KEY = "audience";

private Map<String, Object> parameters;

Expand Down Expand Up @@ -111,6 +112,16 @@ public ParameterBuilder setScope(String scope) {
return set(SCOPE_KEY, scope);
}

/**
* Sets the 'audience' parameter.
*
* @param audience an audience value
* @return itself
*/
public ParameterBuilder setAudience(String audience) {
return set(AUDIENCE_KEY, audience);
}

/**
* Sets the 'device' parameter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public AuthenticationRequest setDevice(String device) {
return this;
}

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

@Override
public AuthenticationRequest setAccessToken(String accessToken) {
authenticationRequest.setAccessToken(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public interface AuthenticationRequest extends Request<Credentials, Authenticati
*/
AuthenticationRequest setDevice(String device);

/**
* Sets the 'audience' parameter.
*
* @param audience an audience value
* @return itself
*/
AuthenticationRequest setAudience(String audience);

/**
* Sets the 'access_token' parameter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

import static com.auth0.android.authentication.ParameterBuilder.ACCESS_TOKEN_KEY;
import static com.auth0.android.authentication.ParameterBuilder.AUDIENCE_KEY;
import static com.auth0.android.authentication.ParameterBuilder.CONNECTION_KEY;
import static com.auth0.android.authentication.ParameterBuilder.DEVICE_KEY;
import static com.auth0.android.authentication.ParameterBuilder.GRANT_TYPE_KEY;
Expand Down Expand Up @@ -67,6 +68,18 @@ public AuthenticationRequest setDevice(String device) {
return this;
}

/**
* Sets the 'audience' parameter.
*
* @param audience an audience value
* @return itself
*/
@Override
public AuthenticationRequest setAudience(String audience) {
addParameter(AUDIENCE_KEY, audience);
return this;
}

/**
* Sets the 'access_token' parameter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,46 @@ public void shouldLoginWithUserAndPasswordSync() throws Exception {
assertThat(body, hasEntry("connection", MY_CONNECTION));
}

@Test
public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpoint() throws Exception {
mockAPI.willReturnSuccessfulLogin();
final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();

client.loginWithToken(SUPPORT_AUTH0_COM, "some-password")
.start(callback);
assertThat(callback, hasPayloadOfType(Credentials.class));

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getHeader("Accept-Language"), is(getDefaultLocale()));
Map<String, String> body = bodyFromRequest(request);
assertThat(body, hasEntry("client_id", CLIENT_ID));
assertThat(body, hasEntry("grant_type", "password"));
assertThat(body, hasEntry("username", SUPPORT_AUTH0_COM));
assertThat(body, hasEntry("password", "some-password"));
assertThat(body, not(hasKey("scope")));
assertThat(body, not(hasKey("audience")));
}

@Test
public void shouldLoginWithUserAndPasswordSyncUsingOAuthTokenEndpoint() throws Exception {
mockAPI.willReturnSuccessfulLogin();

final Credentials credentials = client
.loginWithToken(SUPPORT_AUTH0_COM, "some-password")
.execute();
assertThat(credentials, is(notNullValue()));

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getHeader("Accept-Language"), is(getDefaultLocale()));
Map<String, String> body = bodyFromRequest(request);
assertThat(body, hasEntry("client_id", CLIENT_ID));
assertThat(body, hasEntry("grant_type", "password"));
assertThat(body, hasEntry("username", SUPPORT_AUTH0_COM));
assertThat(body, hasEntry("password", "some-password"));
assertThat(body, not(hasKey("scope")));
assertThat(body, not(hasKey("audience")));
}

@Test
public void shouldFetchTokenInfo() throws Exception {
mockAPI.willReturnTokenInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public void shouldSetScope() throws Exception {
assertThat(parameters, hasEntry("scope", ParameterBuilder.SCOPE_OFFLINE_ACCESS));
}

@Test
public void shouldSetAudience() throws Exception {
Map<String, Object> parameters = builder.setAudience("https://domain.auth0.com/api").asDictionary();
assertThat(parameters, hasEntry("audience", "https://domain.auth0.com/api"));
}

@Test
public void shouldSetDevice() throws Exception {
Map<String, Object> parameters = builder.setDevice(DEVICE).asDictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public AuthenticationRequest setDevice(String device) {
return this;
}

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

@Override
public AuthenticationRequest setAccessToken(String accessToken) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void shouldSetScope() throws Exception {
Assert.assertThat(req, is(signUpRequest));
}

@Test
public void shouldSetAudience() throws Exception {
final AuthenticationRequest req = signUpRequest.setAudience("https://domain.auth0.com/api");
verify(authenticationMockRequest).setAudience("https://domain.auth0.com/api");
Assert.assertThat(req, is(notNullValue()));
Assert.assertThat(req, Matchers.<AuthenticationRequest>is(signUpRequest));
}

@Test
public void shouldSetDevice() throws Exception {
final AuthenticationRequest req = signUpRequest.setDevice("nexus-5x");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public AuthenticationRequest setDevice(String device) {
return null;
}

@Override
public AuthenticationRequest setAudience(String audience) {
return null;
}

@Override
public AuthenticationRequest setAccessToken(String accessToken) {
return null;
Expand Down

0 comments on commit eca69f4

Please sign in to comment.