-
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.
feat(security): include missing auth failure msg body in dev mode
- Loading branch information
1 parent
aec3f34
commit cb7ea96
Showing
14 changed files
with
348 additions
and
28 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
128 changes: 128 additions & 0 deletions
128
.../java/io/quarkus/resteasy/test/security/AuthenticationFailureResponseBodyDevModeTest.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,128 @@ | ||
package io.quarkus.resteasy.test.security; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
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.security.Authenticated; | ||
import io.quarkus.security.AuthenticationCompletionException; | ||
import io.quarkus.security.AuthenticationFailedException; | ||
import io.quarkus.security.identity.IdentityProviderManager; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.request.AuthenticationRequest; | ||
import io.quarkus.security.identity.request.CertificateAuthenticationRequest; | ||
import io.quarkus.test.QuarkusDevModeTest; | ||
import io.quarkus.vertx.http.runtime.security.ChallengeData; | ||
import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; | ||
import io.restassured.RestAssured; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class AuthenticationFailureResponseBodyDevModeTest { | ||
|
||
private static final String RESPONSE_BODY = "failure"; | ||
|
||
public enum AuthFailure { | ||
AUTH_FAILED_WITH_BODY(() -> new AuthenticationFailedException(RESPONSE_BODY), true), | ||
AUTH_COMPLETION_WITH_BODY(() -> new AuthenticationCompletionException(RESPONSE_BODY), true), | ||
AUTH_FAILED_WITHOUT_BODY(AuthenticationFailedException::new, false), | ||
AUTH_COMPLETION_WITHOUT_BODY(AuthenticationCompletionException::new, false); | ||
|
||
public final Supplier<Throwable> failureSupplier; | ||
private final boolean expectBody; | ||
|
||
AuthFailure(Supplier<Throwable> failureSupplier, boolean expectBody) { | ||
this.failureSupplier = failureSupplier; | ||
this.expectBody = expectBody; | ||
} | ||
} | ||
|
||
@RegisterExtension | ||
static QuarkusDevModeTest runner = new QuarkusDevModeTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(SecuredResource.class, FailingAuthenticator.class, AuthFailure.class) | ||
.addAsResource(new StringAsset(""" | ||
quarkus.http.auth.proactive=false | ||
"""), "application.properties")); | ||
|
||
@Test | ||
public void testAuthenticationFailedExceptionBody() { | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITHOUT_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITHOUT_BODY, true); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITH_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITH_BODY, true); | ||
} | ||
|
||
@Test | ||
public void testAuthenticationCompletionExceptionBody() { | ||
assertExceptionBody(AuthFailure.AUTH_COMPLETION_WITHOUT_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_COMPLETION_WITH_BODY, false); | ||
} | ||
|
||
private static void assertExceptionBody(AuthFailure failure, boolean challenge) { | ||
RestAssured | ||
.given() | ||
.redirects().follow(false) | ||
.header("auth-failure", failure.toString()) | ||
.header("challenge-data", challenge) | ||
.get("/secured") | ||
.then() | ||
.statusCode(challenge ? 302 : 401) | ||
.body(failure.expectBody ? Matchers.equalTo(RESPONSE_BODY) | ||
: Matchers.not(Matchers.containsString(RESPONSE_BODY))); | ||
} | ||
|
||
@Authenticated | ||
@Path("secured") | ||
public static class SecuredResource { | ||
|
||
@GET | ||
public String ignored() { | ||
return "ignored"; | ||
} | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
public static class FailingAuthenticator implements HttpAuthenticationMechanism { | ||
|
||
@Override | ||
public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { | ||
return Uni.createFrom().failure(getFailureProducer(context)); | ||
} | ||
|
||
private static Supplier<Throwable> getFailureProducer(RoutingContext context) { | ||
return getAuthFailure(context).failureSupplier; | ||
} | ||
|
||
private static AuthFailure getAuthFailure(RoutingContext context) { | ||
return AuthFailure.valueOf(context.request().getHeader("auth-failure")); | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends AuthenticationRequest>> getCredentialTypes() { | ||
// so that we don't need to implement an identity provider | ||
return Collections.singleton(CertificateAuthenticationRequest.class); | ||
} | ||
|
||
@Override | ||
public Uni<ChallengeData> getChallenge(RoutingContext context) { | ||
if (Boolean.parseBoolean(context.request().getHeader("challenge-data"))) { | ||
return Uni.createFrom().item(new ChallengeData(302, null, null)); | ||
} else { | ||
return Uni.createFrom().optional(Optional.empty()); | ||
} | ||
} | ||
|
||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
...us/resteasy/reactive/server/test/security/AbstractAuthFailureResponseBodyDevModeTest.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,117 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.AuthenticationCompletionException; | ||
import io.quarkus.security.AuthenticationFailedException; | ||
import io.quarkus.security.identity.IdentityProviderManager; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.request.AuthenticationRequest; | ||
import io.quarkus.security.identity.request.CertificateAuthenticationRequest; | ||
import io.quarkus.vertx.http.runtime.security.ChallengeData; | ||
import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; | ||
import io.restassured.RestAssured; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public abstract class AbstractAuthFailureResponseBodyDevModeTest { | ||
|
||
private static final String RESPONSE_BODY = "failure"; | ||
|
||
public enum AuthFailure { | ||
AUTH_FAILED_WITH_BODY(() -> new AuthenticationFailedException(RESPONSE_BODY), true), | ||
AUTH_COMPLETION_WITH_BODY(() -> new AuthenticationCompletionException(RESPONSE_BODY), true), | ||
AUTH_FAILED_WITHOUT_BODY(AuthenticationFailedException::new, false), | ||
AUTH_COMPLETION_WITHOUT_BODY(AuthenticationCompletionException::new, false); | ||
|
||
public final Supplier<Throwable> failureSupplier; | ||
private final boolean expectBody; | ||
|
||
AuthFailure(Supplier<Throwable> failureSupplier, boolean expectBody) { | ||
this.failureSupplier = failureSupplier; | ||
this.expectBody = expectBody; | ||
} | ||
} | ||
|
||
@Test | ||
public void testAuthenticationFailedExceptionBody() { | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITHOUT_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITHOUT_BODY, true); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITH_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_FAILED_WITH_BODY, true); | ||
} | ||
|
||
@Test | ||
public void testAuthenticationCompletionExceptionBody() { | ||
assertExceptionBody(AuthFailure.AUTH_COMPLETION_WITHOUT_BODY, false); | ||
assertExceptionBody(AuthFailure.AUTH_COMPLETION_WITH_BODY, false); | ||
} | ||
|
||
private static void assertExceptionBody(AuthFailure failure, boolean challenge) { | ||
RestAssured | ||
.given() | ||
.redirects().follow(false) | ||
.header("auth-failure", failure.toString()) | ||
.header("challenge-data", challenge) | ||
.get("/secured") | ||
.then() | ||
.statusCode(challenge ? 302 : 401) | ||
.body(failure.expectBody ? Matchers.equalTo(RESPONSE_BODY) | ||
: Matchers.not(Matchers.containsString(RESPONSE_BODY))); | ||
} | ||
|
||
@Authenticated | ||
@Path("secured") | ||
public static class SecuredResource { | ||
|
||
@GET | ||
public String ignored() { | ||
return "ignored"; | ||
} | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
public static class FailingAuthenticator implements HttpAuthenticationMechanism { | ||
|
||
@Override | ||
public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { | ||
return Uni.createFrom().failure(getFailureProducer(context)); | ||
} | ||
|
||
private static Supplier<Throwable> getFailureProducer(RoutingContext context) { | ||
return getAuthFailure(context).failureSupplier; | ||
} | ||
|
||
private static AuthFailure getAuthFailure(RoutingContext context) { | ||
return AuthFailure.valueOf(context.request().getHeader("auth-failure")); | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends AuthenticationRequest>> getCredentialTypes() { | ||
// so that we don't need to implement an identity provider | ||
return Collections.singleton(CertificateAuthenticationRequest.class); | ||
} | ||
|
||
@Override | ||
public Uni<ChallengeData> getChallenge(RoutingContext context) { | ||
if (Boolean.parseBoolean(context.request().getHeader("challenge-data"))) { | ||
return Uni.createFrom().item(new ChallengeData(302, null, null)); | ||
} else { | ||
return Uni.createFrom().optional(Optional.empty()); | ||
} | ||
} | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...uarkus/resteasy/reactive/server/test/security/LazyAuthFailureResponseBodyDevModeTest.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,18 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusDevModeTest; | ||
|
||
public class LazyAuthFailureResponseBodyDevModeTest extends AbstractAuthFailureResponseBodyDevModeTest { | ||
|
||
@RegisterExtension | ||
static QuarkusDevModeTest runner = new QuarkusDevModeTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(SecuredResource.class, FailingAuthenticator.class, AuthFailure.class) | ||
.addAsResource(new StringAsset(""" | ||
quarkus.http.auth.proactive=false | ||
"""), "application.properties")); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...s/resteasy/reactive/server/test/security/ProactiveAuthFailureResponseBodyDevModeTest.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,14 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusDevModeTest; | ||
|
||
public class ProactiveAuthFailureResponseBodyDevModeTest extends AbstractAuthFailureResponseBodyDevModeTest { | ||
|
||
@RegisterExtension | ||
static QuarkusDevModeTest runner = new QuarkusDevModeTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(SecuredResource.class, FailingAuthenticator.class, AuthFailure.class)); | ||
|
||
} |
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.