Search in sources :

Example 1 with GroupPrincipal

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"));
}
Also used : GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Test(org.junit.Test)

Example 2 with GroupPrincipal

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;
}
Also used : Enumeration(java.util.Enumeration) Method(java.lang.reflect.Method) Principal(java.security.Principal) GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal)

Example 3 with GroupPrincipal

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"));
}
Also used : GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Test(org.junit.Test)

Example 4 with GroupPrincipal

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;
}
Also used : Enumeration(java.util.Enumeration) Method(java.lang.reflect.Method) Principal(java.security.Principal) GroupPrincipal(org.apache.cxf.common.security.GroupPrincipal)

Aggregations

Principal (java.security.Principal)4 GroupPrincipal (org.apache.cxf.common.security.GroupPrincipal)4 Method (java.lang.reflect.Method)2 Enumeration (java.util.Enumeration)2 Subject (javax.security.auth.Subject)2 SimpleGroup (org.apache.cxf.common.security.SimpleGroup)2 SimplePrincipal (org.apache.cxf.common.security.SimplePrincipal)2 Test (org.junit.Test)2