use of org.apache.cxf.common.security.GroupPrincipal in project cxf by apache.
the class DefaultSecurityContextTest method testUserInRole2.
@Test
public void testUserInRole2() {
Subject s = new Subject();
Principal p = new SimplePrincipal("Barry");
s.getPrincipals().add(p);
GroupPrincipal group = new SimpleGroup("Roles", p);
group.addMember(new SimpleGroup("friend"));
s.getPrincipals().add(group);
assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend"));
}
use of org.apache.cxf.common.security.GroupPrincipal in project tomee by apache.
the class DefaultSecurityContext method checkGroup.
protected boolean checkGroup(Principal principal, String role) {
if (principal.getName().equals(role)) {
return true;
}
Enumeration<? extends Principal> members;
try {
Method m = ReflectionUtil.getMethod(principal.getClass(), "members");
m.setAccessible(true);
@SuppressWarnings("unchecked") Enumeration<? extends Principal> ms = (Enumeration<? extends Principal>) m.invoke(principal);
members = ms;
} catch (Exception e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Unable to invoke memebers in " + principal.getName() + ":" + e.getMessage());
}
return false;
}
while (members.hasMoreElements()) {
// this might be a plain role but could represent a group consisting of other groups/roles
Principal member = members.nextElement();
if (member.getName().equals(role) || isGroupPrincipal(member) && checkGroup((GroupPrincipal) member, role)) {
return true;
}
}
return false;
}
use of org.apache.cxf.common.security.GroupPrincipal in project cxf by apache.
the class DefaultSecurityContextTest method testUserInRole3.
@Test
public void testUserInRole3() {
Subject s = new Subject();
Principal p = new SimplePrincipal("Barry");
s.getPrincipals().add(p);
GroupPrincipal group = new SimpleGroup("Roles", p);
GroupPrincipal subgroup = new SimpleGroup("subgroup");
subgroup.addMember(new SimpleGroup("friend"));
group.addMember(subgroup);
s.getPrincipals().add(group);
assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend"));
}
use of org.apache.cxf.common.security.GroupPrincipal in project cxf by apache.
the class DefaultSecurityContext method checkGroup.
protected boolean checkGroup(Principal principal, String role) {
if (principal.getName().equals(role)) {
return true;
}
Enumeration<? extends Principal> members;
try {
Method m = ReflectionUtil.getMethod(principal.getClass(), "members");
m.setAccessible(true);
@SuppressWarnings("unchecked") Enumeration<? extends Principal> ms = (Enumeration<? extends Principal>) m.invoke(principal);
members = ms;
} catch (Exception e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Unable to invoke memebers in " + principal.getName() + ":" + e.getMessage());
}
return false;
}
while (members.hasMoreElements()) {
// this might be a plain role but could represent a group consisting of other groups/roles
Principal member = members.nextElement();
if (member.getName().equals(role) || isGroupPrincipal(member) && checkGroup((GroupPrincipal) member, role)) {
return true;
}
}
return false;
}
Aggregations