Skip to content

Commit 8007ddf

Browse files
committed
Do not make OIDC state cookie name unique if multiple code flows are not allowed
1 parent de112b9 commit 8007ddf

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/CodeAuthenticationMechanism.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ private Uni<SecurityIdentity> processRedirectFromOidc(RoutingContext context, Oi
154154
return stateParamIsMissing(oidcTenantConfig, context, cookies, stateQueryParam.size() > 1);
155155
}
156156

157+
String stateCookieNameSuffix = oidcTenantConfig.authentication.allowMultipleCodeFlows ? "_" + stateQueryParam.get(0)
158+
: "";
157159
final Cookie stateCookie = context.request().getCookie(
158-
getStateCookieName(oidcTenantConfig) + "_" + stateQueryParam.get(0));
160+
getStateCookieName(oidcTenantConfig) + stateCookieNameSuffix);
159161

160162
if (stateCookie == null) {
161163
return stateCookieIsMissing(oidcTenantConfig, context, cookies);
@@ -971,8 +973,9 @@ private String generateCodeFlowState(RoutingContext context, TenantConfigContext
971973
extraStateValue.setRestorePath("?" + context.request().query());
972974
cookieValue += (COOKIE_DELIM + encodeExtraStateValue(extraStateValue, configContext));
973975
}
976+
String stateCookieNameSuffix = configContext.oidcConfig.authentication.allowMultipleCodeFlows ? "_" + uuid : "";
974977
createCookie(context, configContext.oidcConfig,
975-
getStateCookieName(configContext.oidcConfig) + "_" + uuid, cookieValue, 60 * 30);
978+
getStateCookieName(configContext.oidcConfig) + stateCookieNameSuffix, cookieValue, 60 * 30);
976979
return uuid;
977980
}
978981

integration-tests/oidc-code-flow/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ quarkus.oidc.tenant-jwt.client-id=quarkus-app-jwt
4343
quarkus.oidc.tenant-jwt.credentials.jwt.secret=AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow
4444
quarkus.oidc.tenant-jwt.token.issuer=${quarkus.oidc.auth-server-url}
4545
quarkus.oidc.tenant-jwt.authentication.redirect-path=/web-app/callback-jwt-after-redirect
46+
quarkus.oidc.tenant-jwt.authentication.allow-multiple-code-flows=false
4647
quarkus.oidc.tenant-jwt.application-type=web-app
4748

4849
# Tenant with client which needs to use client_secret_jwt but uses client_secret_post

integration-tests/oidc-code-flow/src/test/java/io/quarkus/it/keycloak/CodeFlowTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void testCodeFlowNoConsent() throws IOException {
5959

6060
Cookie stateCookie = getStateCookie(webClient, null);
6161
assertNotNull(stateCookie);
62+
assertEquals(stateCookie.getName(), "q_auth_Default_test_" + getStateCookieStateParam(stateCookie));
6263
assertNull(stateCookie.getSameSite());
6364

6465
webClient.getCookieManager().clearCookies();
@@ -673,9 +674,10 @@ public void testIdTokenInjectionJwtMethod() throws IOException, InterruptedExcep
673674
WebResponse webResponse = webClient
674675
.loadWebResponse(
675676
new WebRequest(URI.create("http://localhost:8081/web-app/callback-jwt-before-redirect").toURL()));
676-
assertNotNull(getStateCookie(webClient, "tenant-jwt"));
677-
assertNotNull(getStateCookieStateParam(webClient, "tenant-jwt"));
678-
assertNull(getStateCookieSavedPath(webClient, "tenant-jwt"));
677+
Cookie stateCookie = getNonUniqueStateCookie(webClient, "tenant-jwt");
678+
assertEquals(stateCookie.getName(), "q_auth_tenant-jwt");
679+
assertNotNull(getStateCookieStateParam(stateCookie));
680+
assertNull(getStateCookieSavedPath(stateCookie));
679681

680682
HtmlPage page = webClient.getPage(webResponse.getResponseHeaderValue("location"));
681683
assertEquals("Sign in to quarkus", page.getTitleText());
@@ -1265,15 +1267,29 @@ private Cookie getStateCookie(WebClient webClient, String tenantId) {
12651267
return null;
12661268
}
12671269

1270+
private Cookie getNonUniqueStateCookie(WebClient webClient, String tenantId) {
1271+
String cookieName = "q_auth" + (tenantId == null ? "_Default_test" : "_" + tenantId);
1272+
return webClient.getCookieManager().getCookie(cookieName);
1273+
}
1274+
12681275
private String getStateCookieStateParam(WebClient webClient, String tenantId) {
12691276
return getStateCookie(webClient, tenantId).getValue().split("\\|")[0];
12701277
}
12711278

1279+
private String getStateCookieStateParam(Cookie stateCookie) {
1280+
return stateCookie.getValue().split("\\|")[0];
1281+
}
1282+
12721283
private String getStateCookieSavedPath(WebClient webClient, String tenantId) {
12731284
String[] parts = getStateCookie(webClient, tenantId).getValue().split("\\|");
12741285
return parts.length == 2 ? parts[1] : null;
12751286
}
12761287

1288+
private String getStateCookieSavedPath(Cookie stateCookie) {
1289+
String[] parts = stateCookie.getValue().split("\\|");
1290+
return parts.length == 2 ? parts[1] : null;
1291+
}
1292+
12771293
private Cookie getSessionCookie(WebClient webClient, String tenantId) {
12781294
return webClient.getCookieManager().getCookie("q_session" + (tenantId == null ? "_Default_test" : "_" + tenantId));
12791295
}

0 commit comments

Comments
 (0)