-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix OIDC cookie related tenant id and chunk calculation issues
- Loading branch information
1 parent
07c9712
commit 61529c2
Showing
14 changed files
with
671 additions
and
153 deletions.
There are no files selected for viewing
414 changes: 292 additions & 122 deletions
414
docs/src/main/asciidoc/security-openid-connect-multitenancy.adoc
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
.../oidc/deployment/src/test/java/io/quarkus/oidc/test/CodeTenantReauthenticateTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package io.quarkus.oidc.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler; | ||
import com.gargoylesoftware.htmlunit.TextPage; | ||
import com.gargoylesoftware.htmlunit.WebClient; | ||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.util.Cookie; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.keycloak.server.KeycloakTestResourceLifecycleManager; | ||
|
||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@QuarkusTestResource(KeycloakTestResourceLifecycleManager.class) | ||
public class CodeTenantReauthenticateTestCase { | ||
private static Class<?>[] testClasses = { | ||
TenantReauthentication.class, | ||
CustomTenantResolver.class, | ||
CustomTenantConfigResolver.class | ||
}; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(testClasses) | ||
.addAsResource("application-tenant-reauthenticate.properties", "application.properties")); | ||
|
||
@Test | ||
public void testDefaultTenant() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, null, "/protected", "alice"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTenantResolver() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-resolver", "/protected/tenant/tenant-resolver", "tenant-resolver:alice"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTenantConfigResolver() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-config-resolver", "/protected/tenant/tenant-config-resolver", | ||
"tenant-config-resolver:alice"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromTenantResolverToDefaultTenant() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-resolver", "/protected/tenant/tenant-resolver", "tenant-resolver:alice"); | ||
expectReauthentication(webClient, "/protected", "tenant-resolver"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromDefaultTenantToTenantResover() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, null, "/protected", "alice"); | ||
expectReauthentication(webClient, "/protected/tenant/tenant-resolver", null); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromTenantConfigResolverToDefaultTenant() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-config-resolver", "/protected/tenant/tenant-config-resolver", | ||
"tenant-config-resolver:alice"); | ||
expectReauthentication(webClient, "/protected", "tenant-config-resolver"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromDefaultTenantToTenantConfigResolver() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, null, "/protected", "alice"); | ||
expectReauthentication(webClient, "/protected/tenant/tenant-config-resolver", null); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromTenantResolverToTenantConfigResolver() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-resolver", "/protected/tenant/tenant-resolver", "tenant-resolver:alice"); | ||
expectReauthentication(webClient, "/protected/tenant/tenant-config-resolver", "tenant-resolver"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitchFromTenantConfigResolverToTenantResolver() throws Exception { | ||
try (final WebClient webClient = createWebClient()) { | ||
|
||
callTenant(webClient, "tenant-config-resolver", "/protected/tenant/tenant-config-resolver", | ||
"tenant-config-resolver:alice"); | ||
expectReauthentication(webClient, "/protected/tenant/tenant-resolver", "tenant-config-resolver"); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
private static void callTenant(WebClient webClient, String tenant, String relativePath, String expectedResponse) | ||
throws Exception { | ||
HtmlPage page = webClient.getPage("http://localhost:8081" + relativePath); | ||
|
||
assertEquals("Sign in to quarkus", page.getTitleText()); | ||
|
||
HtmlForm loginForm = page.getForms().get(0); | ||
|
||
loginForm.getInputByName("username").setValueAttribute("alice"); | ||
loginForm.getInputByName("password").setValueAttribute("alice"); | ||
|
||
page = loginForm.getInputByName("login").click(); | ||
|
||
assertEquals(expectedResponse, page.getBody().asNormalizedText()); | ||
assertNotNull(getSessionCookie(webClient, tenant)); | ||
} | ||
|
||
private static void expectReauthentication(WebClient webClient, String relativePath, | ||
String oldTenant) throws Exception { | ||
webClient.getOptions().setRedirectEnabled(false); | ||
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); | ||
|
||
TextPage textPage = webClient.getPage("http://localhost:8081" + relativePath); | ||
assertEquals(302, textPage.getWebResponse().getStatusCode()); | ||
assertNull(getSessionCookie(webClient, oldTenant)); | ||
} | ||
|
||
private static WebClient createWebClient() { | ||
WebClient webClient = new WebClient(); | ||
webClient.setCssErrorHandler(new SilentCssErrorHandler()); | ||
return webClient; | ||
} | ||
|
||
private static Cookie getSessionCookie(WebClient webClient, String tenantId) { | ||
return webClient.getCookieManager().getCookie("q_session" + (tenantId == null ? "" : "_" + tenantId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
extensions/oidc/deployment/src/test/java/io/quarkus/oidc/test/CustomTenantResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.oidc.test; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.oidc.TenantResolver; | ||
import io.quarkus.oidc.runtime.OidcUtils; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@ApplicationScoped | ||
public class CustomTenantResolver implements TenantResolver { | ||
@Override | ||
public String resolve(RoutingContext context) { | ||
if (context.request().path().endsWith("/tenant-resolver")) { | ||
return "tenant-resolver"; | ||
} | ||
context.put(OidcUtils.TENANT_ID_ATTRIBUTE, OidcUtils.DEFAULT_TENANT_ID); | ||
return null; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
extensions/oidc/deployment/src/test/java/io/quarkus/oidc/test/TenantReauthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.oidc.test; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
|
||
import org.eclipse.microprofile.jwt.JsonWebToken; | ||
|
||
import io.quarkus.oidc.IdToken; | ||
import io.quarkus.oidc.OidcSession; | ||
import io.quarkus.security.Authenticated; | ||
|
||
@Path("/protected") | ||
@Authenticated | ||
public class TenantReauthentication { | ||
|
||
@Inject | ||
@IdToken | ||
JsonWebToken idToken; | ||
|
||
@Inject | ||
OidcSession session; | ||
|
||
@GET | ||
public String getName() { | ||
return idToken.getName(); | ||
} | ||
|
||
@GET | ||
@Path("tenant/{id}") | ||
public String getTenantName(@PathParam("id") String tenantId) { | ||
return tenantId + ":" + idToken.getName(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
extensions/oidc/deployment/src/test/resources/application-tenant-reauthenticate.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
quarkus.oidc.auth-server-url=${keycloak.url}/realms/quarkus | ||
quarkus.oidc.client-id=quarkus-web-app | ||
quarkus.oidc.credentials.secret=secret | ||
quarkus.oidc.application-type=web-app | ||
|
||
quarkus.oidc.tenant-resolver.auth-server-url=${keycloak.url}/realms/quarkus | ||
quarkus.oidc.tenant-resolver.client-id=quarkus-web-app | ||
quarkus.oidc.tenant-resolver.credentials.secret=secret | ||
quarkus.oidc.tenant-resolver.application-type=web-app | ||
|
||
quarkus.log.category."com.gargoylesoftware.htmlunit".level=ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.