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.
WebSockets Next: security integration
- fixes quarkusio#40312 - also create a new Vertx duplicated context for error handler invocation
- Loading branch information
Showing
13 changed files
with
418 additions
and
90 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
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
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
102 changes: 102 additions & 0 deletions
102
.../deployment/src/test/java/io/quarkus/websockets/next/test/security/EagerSecurityTest.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,102 @@ | ||
package io.quarkus.websockets.next.test.security; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.CompletionException; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.runtime.util.ExceptionUtil; | ||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.UpgradeRejectedException; | ||
import io.vertx.core.http.WebSocketConnectOptions; | ||
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials; | ||
|
||
public class EagerSecurityTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset("quarkus.http.auth.proactive=true\n" + | ||
"quarkus.http.auth.permission.secured.paths=/end\n" + | ||
"quarkus.http.auth.permission.secured.policy=authenticated\n"), "application.properties") | ||
.addClasses(WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("end") | ||
URI endUri; | ||
|
||
@BeforeAll | ||
public static void setupUsers() { | ||
TestIdentityController.resetRoles() | ||
.add("admin", "admin", "admin") | ||
.add("user", "user", "user"); | ||
} | ||
|
||
@Test | ||
public void testEndpoint() { | ||
try (WSClient client = new WSClient(vertx)) { | ||
CompletionException ce = assertThrows(CompletionException.class, () -> client.connect(endUri)); | ||
Throwable root = ExceptionUtil.getRootCause(ce); | ||
assertTrue(root instanceof UpgradeRejectedException); | ||
assertTrue(root.getMessage().contains("401")); | ||
} | ||
try (WSClient client = new WSClient(vertx)) { | ||
client.connect(basicAuth("admin", "admin"), endUri); | ||
client.sendAndAwait("hello"); | ||
client.waitForMessages(1); | ||
assertEquals("hello", client.getMessages().get(0).toString()); | ||
} | ||
try (WSClient client = new WSClient(vertx)) { | ||
client.connect(basicAuth("user", "user"), endUri); | ||
client.sendAndAwait("hello"); | ||
client.waitForMessages(1); | ||
assertEquals("forbidden", client.getMessages().get(0).toString()); | ||
} | ||
} | ||
|
||
private WebSocketConnectOptions basicAuth(String username, String password) { | ||
return new WebSocketConnectOptions().addHeader(HttpHeaders.AUTHORIZATION.toString(), | ||
new UsernamePasswordCredentials(username, password).applyHttpChallenge(null).toHttpAuthorization()); | ||
} | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
String echo(String message) { | ||
return message; | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden"; | ||
} | ||
|
||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
...t/deployment/src/test/java/io/quarkus/websockets/next/test/security/LazySecurityTest.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,102 @@ | ||
package io.quarkus.websockets.next.test.security; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.CompletionException; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.runtime.util.ExceptionUtil; | ||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.UpgradeRejectedException; | ||
import io.vertx.core.http.WebSocketConnectOptions; | ||
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials; | ||
|
||
public class LazySecurityTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset("quarkus.http.auth.proactive=false\n" + | ||
"quarkus.http.auth.permission.secured.paths=/end\n" + | ||
"quarkus.http.auth.permission.secured.policy=authenticated\n"), "application.properties") | ||
.addClasses(WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("end") | ||
URI endUri; | ||
|
||
@BeforeAll | ||
public static void setupUsers() { | ||
TestIdentityController.resetRoles() | ||
.add("admin", "admin", "admin") | ||
.add("user", "user", "user"); | ||
} | ||
|
||
@Test | ||
public void testEndpoint() { | ||
try (WSClient client = new WSClient(vertx)) { | ||
CompletionException ce = assertThrows(CompletionException.class, () -> client.connect(endUri)); | ||
Throwable root = ExceptionUtil.getRootCause(ce); | ||
assertTrue(root instanceof UpgradeRejectedException); | ||
assertTrue(root.getMessage().contains("401")); | ||
} | ||
try (WSClient client = new WSClient(vertx)) { | ||
client.connect(basicAuth("admin", "admin"), endUri); | ||
client.sendAndAwait("hello"); | ||
client.waitForMessages(1); | ||
assertEquals("hello", client.getMessages().get(0).toString()); | ||
} | ||
try (WSClient client = new WSClient(vertx)) { | ||
client.connect(basicAuth("user", "user"), endUri); | ||
client.sendAndAwait("hello"); | ||
client.waitForMessages(1); | ||
assertEquals("forbidden", client.getMessages().get(0).toString()); | ||
} | ||
} | ||
|
||
private WebSocketConnectOptions basicAuth(String username, String password) { | ||
return new WebSocketConnectOptions().addHeader(HttpHeaders.AUTHORIZATION.toString(), | ||
new UsernamePasswordCredentials(username, password).applyHttpChallenge(null).toHttpAuthorization()); | ||
} | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
String echo(String message) { | ||
return message; | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden"; | ||
} | ||
|
||
} | ||
|
||
} |
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.