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
- when quarkus-security is present and quarkus.http.auth.proactive=false, then we force the authentication before the HTTP upgrade so that it's possible to capture the SecurityIdentity and set it afterwards for all endpoint callbacks - fixes quarkusio#40312 - also create a new Vertx duplicated context for error handler invocation
- Loading branch information
Showing
19 changed files
with
636 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
14 changes: 14 additions & 0 deletions
14
...-next/deployment/src/test/java/io/quarkus/websockets/next/test/security/AdminService.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.websockets.next.test.security; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
@RolesAllowed("admin") | ||
@ApplicationScoped | ||
public class AdminService { | ||
|
||
public String ping() { | ||
return "" + 24; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
.../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,59 @@ | ||
package io.quarkus.websockets.next.test.security; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.identity.CurrentIdentityAssociation; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
|
||
public class EagerSecurityTest extends SecurityTestBase { | ||
|
||
@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(Endpoint.class, WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@Inject | ||
CurrentIdentityAssociation currentIdentity; | ||
|
||
@OnOpen | ||
String open() { | ||
return "ready"; | ||
} | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
String echo(String message) { | ||
if (!currentIdentity.getIdentity().hasRole("admin")) { | ||
throw new IllegalStateException(); | ||
} | ||
return message; | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden:" + currentIdentity.getIdentity().getPrincipal().getName(); | ||
} | ||
|
||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...ployment/src/test/java/io/quarkus/websockets/next/test/security/EagerSecurityUniTest.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.websockets.next.test.security; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.identity.CurrentIdentityAssociation; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class EagerSecurityUniTest extends SecurityTestBase { | ||
|
||
@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(Endpoint.class, WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@Inject | ||
CurrentIdentityAssociation currentIdentity; | ||
|
||
@OnOpen | ||
String open() { | ||
return "ready"; | ||
} | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
Uni<String> echo(String message) { | ||
if (!currentIdentity.getIdentity().hasRole("admin")) { | ||
throw new IllegalStateException(); | ||
} | ||
return Uni.createFrom().item(message); | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden:" + currentIdentity.getIdentity().getPrincipal().getName(); | ||
} | ||
|
||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...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,60 @@ | ||
package io.quarkus.websockets.next.test.security; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.identity.CurrentIdentityAssociation; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.security.EagerSecurityTest.Endpoint; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
|
||
public class LazySecurityTest extends SecurityTestBase { | ||
|
||
@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(Endpoint.class, WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@Inject | ||
CurrentIdentityAssociation currentIdentity; | ||
|
||
@OnOpen | ||
String open() { | ||
return "ready"; | ||
} | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
String echo(String message) { | ||
if (!currentIdentity.getIdentity().hasRole("admin")) { | ||
throw new IllegalStateException(); | ||
} | ||
return message; | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden:" + currentIdentity.getIdentity().getPrincipal().getName(); | ||
} | ||
|
||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...eployment/src/test/java/io/quarkus/websockets/next/test/security/LazySecurityUniTest.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.websockets.next.test.security; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.identity.CurrentIdentityAssociation; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class LazySecurityUniTest extends SecurityTestBase { | ||
|
||
@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(Endpoint.class, WSClient.class, TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Authenticated | ||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
@Inject | ||
CurrentIdentityAssociation currentIdentity; | ||
|
||
@OnOpen | ||
String open() { | ||
return "ready"; | ||
} | ||
|
||
@RolesAllowed("admin") | ||
@OnTextMessage | ||
Uni<String> echo(String message) { | ||
if (!currentIdentity.getIdentity().hasRole("admin")) { | ||
throw new IllegalStateException(); | ||
} | ||
return Uni.createFrom().item(message); | ||
} | ||
|
||
@OnError | ||
String error(ForbiddenException t) { | ||
return "forbidden:" + currentIdentity.getIdentity().getPrincipal().getName(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.