-
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]> Added code & grant_type to login success redirect Signed-off-by: Cem Nura <[email protected]> Added token mock for code flow authentication Signed-off-by: Cem Nura <[email protected]> Corrected token mock for code flow authentication Signed-off-by: Cem Nura <[email protected]>
- Loading branch information
Showing
6 changed files
with
198 additions
and
21 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
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
|
||
@Path("/code-flow") | ||
@Authenticated | ||
public class CodeFlowResource { | ||
|
||
@Inject | ||
SecurityIdentity identity; | ||
|
||
@GET | ||
public String access() { | ||
return identity.getPrincipal().getName(); | ||
} | ||
} |
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; | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...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,60 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler; | ||
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.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
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(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