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.
RESTEasy classic servlets - add RoutingContext to active request context
fixes: quarkusio#30405
- Loading branch information
1 parent
24bae58
commit 963e373
Showing
3 changed files
with
97 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,22 @@ | |
|
||
import javax.annotation.security.PermitAll; | ||
import javax.annotation.security.RolesAllowed; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import io.quarkus.security.identity.CurrentIdentityAssociation; | ||
|
||
/** | ||
* @author Michal Szynkiewicz, [email protected] | ||
*/ | ||
@Path("/roles") | ||
@PermitAll | ||
public class RolesAllowedResource { | ||
|
||
@Inject | ||
CurrentIdentityAssociation currentIdentityAssociation; | ||
|
||
@GET | ||
@RolesAllowed({ "user", "admin" }) | ||
public String defaultSecurity() { | ||
|
@@ -24,4 +31,10 @@ public String admin() { | |
return "admin"; | ||
} | ||
|
||
@Path("/admin/security-identity") | ||
@RolesAllowed("admin") | ||
@GET | ||
public String getSecurityIdentity() { | ||
return currentIdentityAssociation.getIdentity().getPrincipal().getName(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...oyment/src/test/java/io/quarkus/resteasy/test/security/SecurityIdentityAugmentorTest.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,67 @@ | ||
package io.quarkus.resteasy.test.security; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.control.ActivateRequestContext; | ||
|
||
import org.hamcrest.Matchers; | ||
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.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.SecurityIdentityAugmentor; | ||
import io.quarkus.security.runtime.QuarkusSecurityIdentity; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class SecurityIdentityAugmentorTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset("quarkus.http.auth.basic=true\n"), "application.properties") | ||
.addClasses(TestIdentityProvider.class, RolesAllowedResource.class, TestIdentityController.class)) | ||
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-undertow", Version.getVersion()))); | ||
|
||
@BeforeAll | ||
public static void setupUsers() { | ||
TestIdentityController.resetRoles().add("admin", "admin"); | ||
} | ||
|
||
@Test | ||
public void testSecurityIdentityAugmentor() { | ||
RestAssured.given().auth().basic("admin", "admin").get("/roles/admin/security-identity").then().statusCode(200) | ||
.body(Matchers.is("admin")); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class CustomAugmentor implements SecurityIdentityAugmentor { | ||
|
||
@Override | ||
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) { | ||
if (identity.isAnonymous()) { | ||
return Uni.createFrom().item(identity); | ||
} | ||
return context.runBlocking(build(identity)); | ||
} | ||
|
||
@ActivateRequestContext | ||
Supplier<SecurityIdentity> build(SecurityIdentity identity) { | ||
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity); | ||
builder.addRole("admin"); | ||
return builder::build; | ||
} | ||
|
||
} | ||
|
||
} |
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