Skip to content

Commit

Permalink
Merge pull request #26792 from sberyozkin/test_security_principal
Browse files Browse the repository at this point in the history
Add TestPrincipalProducer
  • Loading branch information
sberyozkin authored Jul 18, 2022
2 parents aadc299 + fdab669 commit c328e8e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.it.resteasy.elytron;

import java.security.Principal;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -21,6 +22,8 @@
public class RootResource {
@Inject
SecurityIdentity identity;
@Inject
Principal principal;

@POST
@Consumes(MediaType.TEXT_PLAIN)
Expand Down Expand Up @@ -54,7 +57,7 @@ public String getSecure() {
@Path("/user")
@RolesAllowed("user")
public String user(@Context SecurityContext sec) {
return sec.getUserPrincipal().getName();
return sec.getUserPrincipal().getName() + ":" + identity.getPrincipal().getName() + ":" + principal.getName();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void testTestUserCorrectRole() {
.get("/user")
.then()
.statusCode(200)
.body(is("testUser"));
.body(is("testUser:testUser:testUser"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.it.keycloak;

import java.security.Principal;
import java.util.stream.Collectors;

import javax.inject.Inject;
Expand Down Expand Up @@ -32,6 +33,9 @@ public class ProtectedResource {
@Inject
SecurityIdentity identity;

@Inject
Principal principal;

@Inject
OidcConfigurationMetadata configMetadata;

Expand Down Expand Up @@ -63,13 +67,15 @@ public class ProtectedResource {
@GET
@Path("test-security")
public String testSecurity() {
return securityContext.getUserPrincipal().getName();
return securityContext.getUserPrincipal().getName() + ":" + identity.getPrincipal().getName() + ":"
+ principal.getName();
}

@GET
@Path("test-security-oidc")
public String testSecurityJwt() {
return idToken.getName() + ":" + idToken.getGroups().iterator().next()
return idToken.getName() + ":" + identity.getPrincipal().getName() + ":" + principal.getName()
+ ":" + idToken.getGroups().iterator().next()
+ ":" + idToken.getClaim("email")
+ ":" + userInfo.getString("sub")
+ ":" + configMetadata.get("audience");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestSecurityLazyAuthTest {
@TestSecurity(user = "user1", roles = "viewer")
public void testWithDummyUser() {
RestAssured.when().get("test-security").then()
.body(is("user1"));
.body(is("user1:user1:user1"));
}

@Test
Expand All @@ -35,7 +35,7 @@ public void testWithDummyUser() {
})
public void testJwtWithDummyUser() {
RestAssured.when().get("test-security-oidc").then()
.body(is("userOidc:viewer:[email protected]:subject:aud"));
.body(is("userOidc:userOidc:userOidc:viewer:[email protected]:subject:aud"));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.it.keycloak;

import java.security.Principal;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand All @@ -21,6 +23,9 @@ public class ProtectedJwtResource {
@Inject
SecurityIdentity identity;

@Inject
Principal principal;

@Inject
JsonWebToken accessToken;

Expand All @@ -31,7 +36,8 @@ public class ProtectedJwtResource {
@Path("test-security")
@RolesAllowed("viewer")
public String testSecurity() {
return securityContext.getUserPrincipal().getName();
return securityContext.getUserPrincipal().getName() + ":" + identity.getPrincipal().getName() + ":"
+ principal.getName();
}

@POST
Expand All @@ -46,7 +52,7 @@ public String testSecurityJson(User user) {
@Path("test-security-jwt")
@RolesAllowed("viewer")
public String testSecurityJwt() {
return accessToken.getName() + ":" + accessToken.getGroups().iterator().next()
+ ":" + accessToken.getClaim("email");
return accessToken.getName() + ":" + identity.getPrincipal().getName() + ":" + principal.getName()
+ ":" + accessToken.getGroups().iterator().next() + ":" + accessToken.getClaim("email");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TestSecurityLazyAuthTest {
@TestSecurity(user = "user1", roles = "viewer")
public void testWithDummyUser() {
RestAssured.when().get("test-security").then()
.body(is("user1"));
.body(is("user1:user1:user1"));
}

@Test
Expand Down Expand Up @@ -50,7 +50,7 @@ public void testPostWithDummyUserForbidden() {
})
public void testJwtGetWithDummyUser() {
RestAssured.when().get("test-security-jwt").then()
.body(is("userJwt:viewer:[email protected]"));
.body(is("userJwt:userJwt:userJwt:viewer:[email protected]"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.test.security;

import java.security.Principal;

import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.interceptor.Interceptor;

import io.quarkus.security.identity.SecurityIdentity;

@Alternative
@Priority(Interceptor.Priority.LIBRARY_AFTER)
@ApplicationScoped
public class TestPrincipalProducer {

@Inject
SecurityIdentity testIdentity;

@Produces
@RequestScoped
public Principal getTestIdentity() {
return testIdentity.getPrincipal();
}
}

0 comments on commit c328e8e

Please sign in to comment.