diff --git a/src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java b/src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java index 6d6de578..cd6a0e8b 100644 --- a/src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java +++ b/src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java @@ -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"); } /** @@ -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. * diff --git a/src/test/java/com/auth0/client/auth/AuthorizeUrlBuilderTest.java b/src/test/java/com/auth0/client/auth/AuthorizeUrlBuilderTest.java index 9b8013c2..fed43035 100644 --- a/src/test/java/com/auth0/client/auth/AuthorizeUrlBuilderTest.java +++ b/src/test/java/com/auth0/client/auth/AuthorizeUrlBuilderTest.java @@ -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); + } } \ No newline at end of file