Skip to content

Commit

Permalink
Copy attributes from Elytron SecurityIdentity to Quarkus SecurityIden…
Browse files Browse the repository at this point in the history
…tity
  • Loading branch information
sberyozkin committed Dec 3, 2024
1 parent 94e600e commit 3d7c659
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ public void testJaxrsUserRoleSuccess() {
setupAuth("standardUser", "standardUserPassword")
.when().get("/jaxrs-secured/subject/secured").then()
.statusCode(200)
.body(equalTo("standardUser"));
.body(equalTo(expectedStandardUserName()));
}

@Test
public void testJaxrsInjectedPrincipalSuccess() {
setupAuth("standardUser", "standardUserPassword")
.when().get("/jaxrs-secured/subject/principal-secured").then()
.statusCode(200)
.body(equalTo("standardUser"));
.body(equalTo(expectedStandardUserName()));
}

protected String expectedStandardUserName() {
return "standardUser";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public class MinimalConfigurationTest extends LdapSecurityRealmTest {
.addClasses(testClasses)
.addAsResource("minimal-config/application.properties", "application.properties"));

protected String expectedStandardUserName() {
return "standardUser:Standard User";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.SecurityContext;

import org.wildfly.security.authz.Attributes;

import io.quarkus.security.identity.SecurityIdentity;

@Path("subject")
public class SubjectExposingResource {

@Inject
Principal principal;

@Inject
SecurityIdentity identity;

@GET
@RolesAllowed("standardRole")
@Path("secured")
public String getSubjectSecured(@Context SecurityContext sec) {
Principal user = sec.getUserPrincipal();
public String getSubjectSecured() {
Principal user = identity.getPrincipal();
String name = user != null ? user.getName() : "anonymous";
return name;
Attributes.Entry attributeEntry = (Attributes.Entry) identity.getAttributes().get("displayName");
return attributeEntry == null ? name : name + ":" + attributeEntry.get(0);
}

@GET
Expand All @@ -34,7 +42,8 @@ public String getPrincipalSecured(@Context SecurityContext sec) {
throw new IllegalStateException("No injected principal");
}
String name = principal.getName();
return name;
Attributes.Entry attributeEntry = (Attributes.Entry) identity.getAttributes().get("displayName");
return attributeEntry == null ? name : name + ":" + attributeEntry.get(0);
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ quarkus.security.ldap.identity-mapping.search-base-dn=ou=Users,dc=quarkus,dc=io
quarkus.security.ldap.identity-mapping.attribute-mappings."0".from=cn
quarkus.security.ldap.identity-mapping.attribute-mappings."0".filter=(member=uid={0},ou=Users,dc=quarkus,dc=io)
quarkus.security.ldap.identity-mapping.attribute-mappings."0".filter-base-dn=ou=Roles,dc=quarkus,dc=io

quarkus.security.ldap.identity-mapping.attribute-mappings."1".from=displayName
quarkus.security.ldap.identity-mapping.attribute-mappings."1".to=displayName
quarkus.security.ldap.identity-mapping.attribute-mappings."1".filter=(uid={0})
quarkus.security.ldap.identity-mapping.attribute-mappings."1".filter-base-dn=ou=Users,dc=quarkus,dc=io
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jboss.logging.Logger;
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.authz.Attributes;
import org.wildfly.security.evidence.PasswordGuessEvidence;

import io.quarkus.security.AuthenticationFailedException;
Expand Down Expand Up @@ -52,6 +53,10 @@ public SecurityIdentity get() {
throw new AuthenticationFailedException();
}
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
for (Attributes.Entry entry : result.getAttributes().entries()) {
builder.addAttribute(entry.getKey(), entry);
}

builder.setPrincipal(result.getPrincipal());
for (String i : result.getRoles()) {
builder.addRole(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jboss.logging.Logger;
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.authz.Attributes;
import org.wildfly.security.evidence.BearerTokenEvidence;

import io.quarkus.security.AuthenticationFailedException;
Expand Down Expand Up @@ -51,6 +52,9 @@ public SecurityIdentity get() {
throw new AuthenticationFailedException();
}
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
for (Attributes.Entry entry : result.getAttributes().entries()) {
builder.addAttribute(entry.getKey(), entry);
}
builder.setPrincipal(result.getPrincipal());
for (String i : result.getRoles()) {
builder.addRole(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.ServerAuthenticationContext;
import org.wildfly.security.authz.Attributes;
import org.wildfly.security.credential.PasswordCredential;

import io.quarkus.security.AuthenticationFailedException;
Expand Down Expand Up @@ -62,6 +63,9 @@ public SecurityIdentity get() {
throw new AuthenticationFailedException();
}
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
for (Attributes.Entry entry : result.getAttributes().entries()) {
builder.addAttribute(entry.getKey(), entry);
}
builder.setPrincipal(result.getPrincipal());
for (String i : result.getRoles()) {
builder.addRole(i);
Expand Down
2 changes: 2 additions & 0 deletions test-framework/ldap/src/main/resources/quarkus-io.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ cn: StandardUser
sn: standardUser
uid: standardUser
userPassword: standardUserPassword
displayName: Standard User


dn: uid=adminUser,ou=Users,dc=quarkus,dc=io
objectClass: top
Expand Down

0 comments on commit 3d7c659

Please sign in to comment.