Skip to content

Commit

Permalink
Merge pull request #12143 from sberyozkin/oidc_drop_query_with_ssl_te…
Browse files Browse the repository at this point in the history
…rminating_proxy

Set HTTPS scheme in the final OIDC redirect URI
  • Loading branch information
gsmet authored Sep 17, 2020
2 parents 7d1b2db + ff222c7 commit 3108690
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ public void accept(SecurityIdentity identity) {

if (configContext.oidcConfig.authentication.isRemoveRedirectParameters()
&& context.request().query() != null) {
String finalRedirectUri = buildUriWithoutQueryParams(context);
String finalRedirectUri = buildUriWithoutQueryParams(context,
isForceHttps(configContext));
if (finalUserQuery != null) {
finalRedirectUri += ("?" + finalUserQuery);
}
Expand Down Expand Up @@ -429,9 +430,10 @@ private String buildUri(RoutingContext context, boolean forceHttps, String path)
.toString();
}

private String buildUriWithoutQueryParams(RoutingContext context) {
private String buildUriWithoutQueryParams(RoutingContext context, boolean forceHttps) {
final String scheme = forceHttps ? "https" : context.request().scheme();
URI absoluteUri = URI.create(context.request().absoluteURI());
return new StringBuilder(context.request().scheme()).append("://")
return new StringBuilder(scheme).append("://")
.append(absoluteUri.getAuthority())
.append(absoluteUri.getRawPath())
.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.it.keycloak;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import io.quarkus.security.Authenticated;

@Path("/tenant-https")
public class TenantHttps {

@Authenticated
@GET
public String getTenant() {
return "tenant-https";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ quarkus.oidc.tenant-https.auth-server-url=${keycloak.url}/realms/quarkus
quarkus.oidc.tenant-https.client-id=quarkus-app
quarkus.oidc.tenant-https.credentials.secret=secret
quarkus.oidc.tenant-https.authentication.scopes=profile,email,phone
quarkus.oidc.tenant-https.authentication.redirect-path=/web-app
quarkus.oidc.tenant-https.authentication.cookie-path=/
quarkus.oidc.tenant-https.authentication.extra-params.max-age=60
quarkus.oidc.tenant-https.application-type=web-app
Expand All @@ -104,9 +103,6 @@ quarkus.http.auth.permission.logout.policy=authenticated
quarkus.http.auth.permission.logout.paths=/tenant-autorefresh
quarkus.http.auth.permission.logout.policy=authenticated

quarkus.http.auth.permission.https.paths=/tenant-https
quarkus.http.auth.permission.https.policy=authenticated

quarkus.http.auth.permission.xhr.paths=/tenant-xhr
quarkus.http.auth.permission.xhr.policy=authenticated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testCodeFlowNoConsent() throws IOException {
webClient.getOptions().setRedirectEnabled(false);
WebResponse webResponse = webClient
.loadWebResponse(new WebRequest(URI.create("http://localhost:8081/index.html").toURL()));
verifyLocationHeader(webClient, webResponse.getResponseHeaderValue("location"), null, false);
verifyLocationHeader(webClient, webResponse.getResponseHeaderValue("location"), null, "web-app", false);

webClient.getOptions().setRedirectEnabled(true);
HtmlPage page = webClient.getPage("http://localhost:8081/index.html");
Expand Down Expand Up @@ -75,15 +75,46 @@ public void testCodeFlowForceHttpsRedirectUri() throws IOException {
WebResponse webResponse = webClient
.loadWebResponse(
new WebRequest(URI.create("http://localhost:8081/tenant-https").toURL()));
verifyLocationHeader(webClient, webResponse.getResponseHeaderValue("location"), "tenant-https", true);
String keycloakUrl = webResponse.getResponseHeaderValue("location");
verifyLocationHeader(webClient, keycloakUrl, "tenant-https", "tenant-https",
true);
HtmlPage page = webClient.getPage(keycloakUrl);

assertEquals("Log in to quarkus", page.getTitleText());
HtmlForm loginForm = page.getForms().get(0);
loginForm.getInputByName("username").setValueAttribute("alice");
loginForm.getInputByName("password").setValueAttribute("alice");

webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webResponse = loginForm.getInputByName("login").click().getWebResponse();
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);

// This is a redirect from the OIDC server to the endpoint
String endpointLocation = webResponse.getResponseHeaderValue("location");
assertTrue(endpointLocation.startsWith("https"));
endpointLocation = "http" + endpointLocation.substring(5);
URI endpointLocationUri = URI.create(endpointLocation);
assertNotNull(endpointLocationUri.getRawQuery());
webResponse = webClient.loadWebResponse(new WebRequest(endpointLocationUri.toURL()));

// This is a redirect from quarkus-oidc which drops the query parameters
String endpointLocation2 = webResponse.getResponseHeaderValue("location");
assertTrue(endpointLocation2.startsWith("https"));

endpointLocation2 = "http" + endpointLocation2.substring(5);
URI endpointLocationUri2 = URI.create(endpointLocation2);
assertNull(endpointLocationUri2.getRawQuery());

page = webClient.getPage(endpointLocationUri2.toURL());
assertEquals("tenant-https", page.getBody().asText());
webClient.getCookieManager().clearCookies();
}
}

private void verifyLocationHeader(WebClient webClient, String loc, String tenant, boolean forceHttps) {
private void verifyLocationHeader(WebClient webClient, String loc, String tenant, String path, boolean forceHttps) {
assertTrue(loc.startsWith("http://localhost:8180/auth/realms/quarkus/protocol/openid-connect/auth"));
String scheme = forceHttps ? "https" : "http";
assertTrue(loc.contains("redirect_uri=" + scheme + "%3A%2F%2Flocalhost%3A8081%2Fweb-app"));
assertTrue(loc.contains("redirect_uri=" + scheme + "%3A%2F%2Flocalhost%3A8081%2F" + path));
assertTrue(loc.contains("state=" + getStateCookieStateParam(webClient, tenant)));
assertTrue(loc.contains("scope=openid+profile+email+phone"));
assertTrue(loc.contains("response_type=code"));
Expand Down

0 comments on commit 3108690

Please sign in to comment.