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 response_type and custom parameter setter for AuthorizeUrlBuilder #40

Merged
merged 1 commit into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 27 additions & 1 deletion src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ private AuthorizeUrlBuilder(String domain, String clientId, String redirectUri)
builder = HttpUrl.parse(domain).newBuilder()
.addPathSegment("authorize")
.addEncodedQueryParameter("redirect_uri", redirectUri)
.addQueryParameter("response_type", "code")
.addQueryParameter("client_id", clientId);
withParameter("response_type", "code");
}

/**
Expand Down Expand Up @@ -90,6 +90,32 @@ public AuthorizeUrlBuilder withScope(String scope) {
return this;
}

/**
* Sets the response type value.
*
* @param responseType response type to set
* @return the builder instance
*/
public AuthorizeUrlBuilder withResponseType(String responseType) {
assertNotNull(responseType, "response type");
parameters.put("response_type", responseType);
return this;
}

/**
* Sets an additional parameter.
*
* @param name name of the parameter
* @param value value of the parameter to set
* @return the builder instance
*/
public AuthorizeUrlBuilder withParameter(String name, String value) {
assertNotNull(name, "name");
assertNotNull(value, "value");
parameters.put(name, value);
return this;
}

/**
* Creates a string representation of the URL with the configured parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,50 @@ public void shouldSetScope() throws Exception {
}

@Test
public void shouldThrowWithStateIsNull() throws Exception {
public void shouldThrowWhenScopeIsNull() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'state' cannot be null!");
exception.expectMessage("'scope' cannot be null!");
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withState(null);
.withScope(null);
}

@Test
public void shouldSetResponseType() throws Exception {
String url = AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withResponseType("token id_token")
.build();
assertThat(url, hasQueryParameter("response_type", "token id_token"));
}

@Test
public void shouldThrowWhenResponseTypeIsNull() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'response type' cannot be null!");
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withResponseType(null);
}

@Test
public void shouldSetCustomParameter() throws Exception {
String url = AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withParameter("name", "value")
.build();
assertThat(url, hasQueryParameter("name", "value"));
}

@Test
public void shouldThrowWhenCustomParameterNameIsNull() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'name' cannot be null!");
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withParameter(null, "value");
}

@Test
public void shouldThrowWhenCustomParameterValueIsNull() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'value' cannot be null!");
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
.withParameter("name", null);
}
}