forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#39945 from michalvavrik/feature/detect-i…
…mplicit-basic-auth-requirement Detect basic authentication is implicitly required when it can be safely determined and enable the basic auth by default for such scenarios
- Loading branch information
Showing
6 changed files
with
311 additions
and
6 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
77 changes: 77 additions & 0 deletions
77
...ent/src/test/java/io/quarkus/oidc/test/ImplicitBasicAuthAndBearerAuthCombinationTest.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,77 @@ | ||
package io.quarkus.oidc.test; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.jwt.JsonWebToken; | ||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.oidc.BearerTokenAuthentication; | ||
import io.quarkus.test.QuarkusDevModeTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.keycloak.server.KeycloakTestResourceLifecycleManager; | ||
import io.quarkus.vertx.http.runtime.security.annotation.BasicAuthentication; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTestResource(KeycloakTestResourceLifecycleManager.class) | ||
public class ImplicitBasicAuthAndBearerAuthCombinationTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusDevModeTest test = new QuarkusDevModeTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(BasicBearerResource.class) | ||
.addAsResource( | ||
new StringAsset(""" | ||
quarkus.security.users.embedded.enabled=true | ||
quarkus.security.users.embedded.plain-text=true | ||
quarkus.security.users.embedded.users.alice=alice | ||
quarkus.oidc.auth-server-url=${keycloak.url}/realms/quarkus | ||
quarkus.oidc.client-id=quarkus-service-app | ||
quarkus.oidc.credentials.secret=secret | ||
quarkus.http.auth.proactive=false | ||
"""), | ||
"application.properties")); | ||
|
||
@Test | ||
public void testBasicEnabledAsSelectedWithAnnotation() { | ||
// endpoint is annotated with 'BasicAuthentication', so basic auth must be enabled | ||
RestAssured.given().auth().oauth2(getAccessToken()).get("/basic-bearer/bearer") | ||
.then().statusCode(200).body(Matchers.is("alice")); | ||
RestAssured.given().auth().basic("alice", "alice").get("/basic-bearer/basic") | ||
.then().statusCode(204); | ||
RestAssured.given().auth().basic("alice", "alice").get("/basic-bearer/bearer") | ||
.then().statusCode(401); | ||
RestAssured.given().auth().oauth2(getAccessToken()).get("/basic-bearer/basic") | ||
.then().statusCode(401); | ||
} | ||
|
||
private static String getAccessToken() { | ||
return KeycloakTestResourceLifecycleManager.getAccessToken("alice"); | ||
} | ||
|
||
@BearerTokenAuthentication | ||
@Path("basic-bearer") | ||
public static class BasicBearerResource { | ||
|
||
@Inject | ||
JsonWebToken accessToken; | ||
|
||
@GET | ||
@BasicAuthentication | ||
@Path("basic") | ||
public String basic() { | ||
return accessToken.getName(); | ||
} | ||
|
||
@GET | ||
@Path("bearer") | ||
public String bearer() { | ||
return accessToken.getName(); | ||
} | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
...t/src/test/java/io/quarkus/oidc/test/ImplicitBasicAuthAndCodeFlowAuthCombinationTest.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,118 @@ | ||
package io.quarkus.oidc.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.jwt.JsonWebToken; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler; | ||
import com.gargoylesoftware.htmlunit.WebClient; | ||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
|
||
import io.quarkus.oidc.IdToken; | ||
import io.quarkus.test.QuarkusDevModeTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.keycloak.server.KeycloakTestResourceLifecycleManager; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTestResource(KeycloakTestResourceLifecycleManager.class) | ||
public class ImplicitBasicAuthAndCodeFlowAuthCombinationTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusDevModeTest test = new QuarkusDevModeTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(BasicCodeFlowResource.class) | ||
.addAsResource( | ||
new StringAsset(""" | ||
quarkus.security.users.embedded.enabled=true | ||
quarkus.security.users.embedded.plain-text=true | ||
quarkus.security.users.embedded.users.alice=alice | ||
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.http.auth.permission.code-flow.paths=/basic-code-flow/code-flow | ||
quarkus.http.auth.permission.code-flow.policy=authenticated | ||
quarkus.http.auth.permission.code-flow.auth-mechanism=code | ||
quarkus.http.auth.permission.basic.paths=/basic-code-flow/basic | ||
quarkus.http.auth.permission.basic.policy=authenticated | ||
quarkus.http.auth.permission.basic.auth-mechanism=basic | ||
"""), | ||
"application.properties")); | ||
|
||
@Test | ||
public void testBasicEnabledAsSelectedWithHttpPerm() throws IOException, InterruptedException { | ||
// endpoint is annotated with 'BasicAuthentication', so basic auth must be enabled | ||
RestAssured.given().auth().basic("alice", "alice").get("/basic-code-flow/basic") | ||
.then().statusCode(204); | ||
RestAssured.given().auth().basic("alice", "alice").redirects().follow(false) | ||
.get("/basic-code-flow/code-flow").then().statusCode(302); | ||
|
||
try (final WebClient webClient = createWebClient()) { | ||
|
||
try { | ||
webClient.getPage("http://localhost:8080/basic-code-flow/basic"); | ||
fail("Exception is expected because by the basic auth is required"); | ||
} catch (FailingHttpStatusCodeException ex) { | ||
// Reported by Quarkus | ||
assertEquals(401, ex.getStatusCode()); | ||
} | ||
HtmlPage page = webClient.getPage("http://localhost:8080/basic-code-flow/code-flow"); | ||
|
||
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("alice", page.getBody().asNormalizedText()); | ||
|
||
webClient.getCookieManager().clearCookies(); | ||
} | ||
} | ||
|
||
private WebClient createWebClient() { | ||
WebClient webClient = new WebClient(); | ||
webClient.setCssErrorHandler(new SilentCssErrorHandler()); | ||
return webClient; | ||
} | ||
|
||
private static String getAccessToken() { | ||
return KeycloakTestResourceLifecycleManager.getAccessToken("alice"); | ||
} | ||
|
||
@Path("basic-code-flow") | ||
public static class BasicCodeFlowResource { | ||
|
||
@Inject | ||
@IdToken | ||
JsonWebToken idToken; | ||
|
||
@GET | ||
@Path("basic") | ||
public String basic() { | ||
return idToken.getName(); | ||
} | ||
|
||
@GET | ||
@Path("code-flow") | ||
public String codeFlow() { | ||
return idToken.getName(); | ||
} | ||
} | ||
|
||
} |
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.