|
| 1 | +package io.quarkus.oidc.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.is; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | + |
| 7 | +import jakarta.inject.Inject; |
| 8 | +import jakarta.ws.rs.GET; |
| 9 | +import jakarta.ws.rs.Path; |
| 10 | + |
| 11 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 14 | + |
| 15 | +import io.quarkus.oidc.BearerTokenAuthentication; |
| 16 | +import io.quarkus.security.Authenticated; |
| 17 | +import io.quarkus.security.identity.SecurityIdentity; |
| 18 | +import io.quarkus.test.QuarkusDevModeTest; |
| 19 | +import io.quarkus.test.common.QuarkusTestResource; |
| 20 | +import io.quarkus.test.keycloak.server.KeycloakTestResourceLifecycleManager; |
| 21 | +import io.quarkus.vertx.http.runtime.security.annotation.MTLSAuthentication; |
| 22 | +import io.restassured.RestAssured; |
| 23 | +import io.restassured.specification.RequestSpecification; |
| 24 | +import io.smallrye.certs.Format; |
| 25 | +import io.smallrye.certs.junit5.Certificate; |
| 26 | +import io.smallrye.certs.junit5.Certificates; |
| 27 | + |
| 28 | +/** |
| 29 | + * This test ensures OIDC runs before mTLS authentication mechanism when inclusive authentication is not enabled. |
| 30 | + */ |
| 31 | +@QuarkusTestResource(KeycloakTestResourceLifecycleManager.class) |
| 32 | +@Certificates(baseDir = "target/certs", certificates = @Certificate(name = "mtls-test", password = "secret", formats = { |
| 33 | + Format.PKCS12, Format.PEM }, client = true)) |
| 34 | +public class OidcMtlsDisabledInclusiveAuthTest { |
| 35 | + |
| 36 | + private static final String BASE_URL = "https://localhost:8443/mtls-bearer/"; |
| 37 | + private static final String CONFIGURATION = """ |
| 38 | + quarkus.tls.key-store.pem.0.cert=server.crt |
| 39 | + quarkus.tls.key-store.pem.0.key=server.key |
| 40 | + quarkus.tls.trust-store.pem.certs=ca.crt |
| 41 | + quarkus.http.ssl.client-auth=REQUIRED |
| 42 | + quarkus.http.insecure-requests=disabled |
| 43 | + quarkus.oidc.auth-server-url=${keycloak.url}/realms/quarkus |
| 44 | + quarkus.oidc.client-id=quarkus-service-app |
| 45 | + quarkus.oidc.credentials.secret=secret |
| 46 | + quarkus.http.auth.proactive=false |
| 47 | + """; |
| 48 | + |
| 49 | + @RegisterExtension |
| 50 | + static final QuarkusDevModeTest config = new QuarkusDevModeTest() |
| 51 | + .withApplicationRoot((jar) -> jar |
| 52 | + .addClasses(MtlsBearerResource.class) |
| 53 | + .addAsResource(new StringAsset(CONFIGURATION), "application.properties") |
| 54 | + .addAsResource(new File("target/certs/mtls-test.key"), "server.key") |
| 55 | + .addAsResource(new File("target/certs/mtls-test.crt"), "server.crt") |
| 56 | + .addAsResource(new File("target/certs/mtls-test-server-ca.crt"), "ca.crt")); |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testOidcHasHighestPriority() { |
| 60 | + givenWithCerts().get(BASE_URL + "only-mtls").then().statusCode(200).body(is("CN=localhost")); |
| 61 | + givenWithCerts().auth().oauth2(getAccessToken()).get(BASE_URL + "only-bearer").then().statusCode(200).body(is("alice")); |
| 62 | + // this needs to be OIDC because when inclusive auth is disabled, OIDC has higher priority |
| 63 | + givenWithCerts().auth().oauth2(getAccessToken()).get(BASE_URL + "both").then().statusCode(200).body(is("alice")); |
| 64 | + // OIDC must run first and thus authentication fails over invalid credentials |
| 65 | + givenWithCerts().auth().oauth2("invalid-token").get(BASE_URL + "both").then().statusCode(401); |
| 66 | + // mTLS authentication mechanism still runs when OIDC doesn't produce the identity |
| 67 | + givenWithCerts().get(BASE_URL + "both").then().statusCode(200).body(is("CN=localhost")); |
| 68 | + } |
| 69 | + |
| 70 | + private static RequestSpecification givenWithCerts() { |
| 71 | + return RestAssured.given() |
| 72 | + .keyStore("target/certs/mtls-test-client-keystore.p12", "secret") |
| 73 | + .trustStore("target/certs/mtls-test-client-truststore.p12", "secret"); |
| 74 | + } |
| 75 | + |
| 76 | + private static String getAccessToken() { |
| 77 | + return KeycloakTestResourceLifecycleManager.getAccessToken("alice"); |
| 78 | + } |
| 79 | + |
| 80 | + @Path("mtls-bearer") |
| 81 | + public static class MtlsBearerResource { |
| 82 | + |
| 83 | + @Inject |
| 84 | + SecurityIdentity securityIdentity; |
| 85 | + |
| 86 | + @GET |
| 87 | + @Authenticated |
| 88 | + @Path("both") |
| 89 | + public String both() { |
| 90 | + return securityIdentity.getPrincipal().getName(); |
| 91 | + } |
| 92 | + |
| 93 | + @GET |
| 94 | + @MTLSAuthentication |
| 95 | + @Path("only-mtls") |
| 96 | + public String onlyMTLS() { |
| 97 | + return securityIdentity.getPrincipal().getName(); |
| 98 | + } |
| 99 | + |
| 100 | + @GET |
| 101 | + @BearerTokenAuthentication |
| 102 | + @Path("only-bearer") |
| 103 | + public String onlyBearer() { |
| 104 | + return securityIdentity.getPrincipal().getName(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments