-
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.
Added code flow authorization it test with wiremock stubbing
Signed-off-by: Cem Nura <[email protected]>
- Loading branch information
Showing
7 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
integration-tests/oidc-wiremock/src/main/java/io/quarkus/it/keycloak/CodeFlowResource.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,15 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import io.quarkus.security.Authenticated; | ||
|
||
@Path("/code-flow") | ||
@Authenticated | ||
public class CodeFlowResource { | ||
|
||
@GET | ||
public void access() { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ration-tests/oidc-wiremock/src/main/java/io/quarkus/it/keycloak/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.it.keycloak; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.oidc.TenantResolver; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@ApplicationScoped | ||
public class CustomTenantResolver implements TenantResolver { | ||
|
||
@Override | ||
public String resolve(RoutingContext context) { | ||
String path = context.normalisedPath(); | ||
if (path.endsWith("code-flow")) { | ||
return "code-flow"; | ||
} | ||
return null; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-tests/oidc-wiremock/src/main/resources/META-INF/resources/index.html
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,8 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Welcome to Test App</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
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
68 changes: 68 additions & 0 deletions
68
...n-tests/oidc-wiremock/src/test/java/io/quarkus/it/keycloak/CodeFlowAuthorizationTest.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,68 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler; | ||
import com.gargoylesoftware.htmlunit.WebClient; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import com.gargoylesoftware.htmlunit.WebResponse; | ||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.util.Cookie; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(KeycloakTestResource.class) | ||
public class CodeFlowAuthorizationTest { | ||
|
||
@Test | ||
public void testCodeFlow() throws IOException { | ||
try (final WebClient webClient = createWebClient()) { | ||
webClient.getOptions().setRedirectEnabled(false); | ||
WebResponse webResponse = webClient | ||
.loadWebResponse(new WebRequest(URI.create("http://localhost:8081/code-flow").toURL())); | ||
verifyLocationHeader(webClient, webResponse.getResponseHeaderValue("location"), "code-flow", "index.html", false); | ||
|
||
webClient.getOptions().setRedirectEnabled(true); | ||
HtmlPage page = webClient.getPage("http://localhost:8081/code-flow"); | ||
|
||
HtmlForm form = page.getFormByName("form"); | ||
form.getInputByName("username").type("alice"); | ||
form.getInputByName("password").type("alice"); | ||
|
||
page = form.getInputByValue("login").click(); | ||
|
||
assertEquals("Welcome to Test App", page.getTitleText()); | ||
} | ||
} | ||
|
||
private WebClient createWebClient() { | ||
WebClient webClient = new WebClient(); | ||
webClient.setCssErrorHandler(new SilentCssErrorHandler()); | ||
return webClient; | ||
} | ||
|
||
private void verifyLocationHeader(WebClient webClient, String loc, String tenant, String path, boolean forceHttps) { | ||
assertTrue(loc.contains("/auth")); | ||
String scheme = forceHttps ? "https" : "http"; | ||
assertTrue(loc.contains("redirect_uri=" + scheme + "%3A%2F%2Flocalhost%3A8081%2F" + path)); | ||
assertTrue(loc.contains("state=" + getStateCookieStateParam(webClient, tenant))); | ||
assertTrue(loc.contains("scope=openid")); | ||
assertTrue(loc.contains("response_type=code")); | ||
assertTrue(loc.contains("client_id=quarkus-web-app")); | ||
} | ||
|
||
private Cookie getStateCookie(WebClient webClient, String tenantId) { | ||
return webClient.getCookieManager().getCookie("q_auth" + (tenantId == null ? "" : "_" + tenantId)); | ||
} | ||
|
||
private String getStateCookieStateParam(WebClient webClient, String tenantId) { | ||
return getStateCookie(webClient, tenantId).getValue().split("\\|")[0]; | ||
} | ||
} |
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