Skip to content
Open
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
15 changes: 12 additions & 3 deletions duo-universal-sdk/src/main/java/com/duosecurity/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import static com.duosecurity.Validator.validateUsername;
import static java.lang.String.format;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.duosecurity.exception.DuoException;
import com.duosecurity.model.HealthCheckResponse;
Expand Down Expand Up @@ -275,9 +279,14 @@ public String createAuthUrl(String username, String state) throws DuoException {
validateState(state);
String request = createJwtForAuthUrl(clientId, clientSecret, redirectUri,
state, username, useDuoCodeAttribute);
String query = format(
"?scope=openid&response_type=code&redirect_uri=%s&client_id=%s&request=%s",
redirectUri, clientId, request);
String query;
try {
query = format(
"?scope=openid&response_type=code&redirect_uri=%s&client_id=%s&request=%s",
URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.toString()), clientId, request);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Could not find encoding: " + StandardCharsets.UTF_8.toString());
}
return getAndValidateUrl(apiHost, OAUTH_V_1_AUTHORIZE_ENDPOINT + query).toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import org.mockito.Mockito;
import org.mockito.internal.matchers.apachecommons.ReflectionEquals;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -79,12 +82,12 @@ void healthCheck_throws_exception_when_stat_not_OK() throws DuoException {
}

@Test
void createAuthUrl_success() throws DuoException {
void createAuthUrl_success() throws DuoException, UnsupportedEncodingException {
String urlString = client.createAuthUrl(USERNAME, STATE);
try {
URL authUrl = new URL(urlString);
assertEquals(authUrl.getHost(), API_HOST);
assertTrue(authUrl.getQuery().contains("redirect_uri=" + HTTPS_REDIRECT_URI));
assertTrue(authUrl.getQuery().contains("redirect_uri=" + URLEncoder.encode(HTTPS_REDIRECT_URI, StandardCharsets.UTF_8.toString())));
assertTrue(authUrl.getQuery().contains("client_id=" + CLIENT_ID));
assertTrue(authUrl.getProtocol().equals("https"));
} catch (MalformedURLException e) {
Expand Down