Search in sources :

Example 1 with SimpleGroup

use of org.apache.cxf.common.security.SimpleGroup 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);
    Group group = new SimpleGroup("Roles", p);
    group.addMember(new SimpleGroup("friend"));
    s.getPrincipals().add(group);
    assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend"));
}
Also used : Group(java.security.acl.Group) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Test(org.junit.Test)

Example 2 with SimpleGroup

use of org.apache.cxf.common.security.SimpleGroup in project cxf by apache.

the class DefaultSecurityContextTest method testMultipleRoles.

@Test
public void testMultipleRoles() {
    Subject s = new Subject();
    Principal p = new SimplePrincipal("Barry");
    s.getPrincipals().add(p);
    Set<Principal> roles = new HashSet<>();
    roles.add(new SimpleGroup("friend", p));
    roles.add(new SimpleGroup("admin", p));
    s.getPrincipals().addAll(roles);
    LoginSecurityContext context = new DefaultSecurityContext(p, s);
    assertTrue(context.isUserInRole("friend"));
    assertTrue(context.isUserInRole("admin"));
    assertFalse(context.isUserInRole("bar"));
    Set<Principal> roles2 = context.getUserRoles();
    assertEquals(roles2, roles);
}
Also used : LoginSecurityContext(org.apache.cxf.security.LoginSecurityContext) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with SimpleGroup

use of org.apache.cxf.common.security.SimpleGroup in project cxf by apache.

the class SAMLUtils method parseRolesFromClaims.

/**
 * Extract roles from the given Claims
 */
public static Set<Principal> parseRolesFromClaims(ClaimCollection claims, String name, String nameFormat) {
    String roleAttributeName = name;
    if (roleAttributeName == null) {
        roleAttributeName = SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT;
    }
    Set<Principal> roles = new HashSet<>();
    for (Claim claim : claims) {
        if (claim instanceof SAMLClaim && ((SAMLClaim) claim).getName().equals(name) && (nameFormat == null || nameFormat.equals(((SAMLClaim) claim).getNameFormat()))) {
            for (Object claimValue : claim.getValues()) {
                if (claimValue instanceof String) {
                    roles.add(new SimpleGroup((String) claimValue));
                }
            }
            if (claim.getValues().size() > 1) {
                // Don't search for other attributes with the same name if > 1 claim value
                break;
            }
        }
    }
    return roles;
}
Also used : SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) SimpleGroup(org.apache.cxf.common.security.SimpleGroup) XMLObject(org.opensaml.core.xml.XMLObject) Principal(java.security.Principal) SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) Claim(org.apache.cxf.rt.security.claims.Claim) HashSet(java.util.HashSet)

Example 4 with SimpleGroup

use of org.apache.cxf.common.security.SimpleGroup in project cxf by apache.

the class CustomUsernameTokenInterceptor method createSubject.

protected Subject createSubject(String name, String password, boolean isDigest, String nonce, String created) throws SecurityException {
    Subject subject = new Subject();
    // delegate to the external security system if possible
    // authenticate the user somehow
    subject.getPrincipals().add(new SimplePrincipal(name));
    // add roles this user is in
    String roleName = "Alice".equals(name) ? "developers" : "pms";
    String expectedPassword = "Alice".equals(name) ? "ecilA" : UsernameToken.doPasswordDigest(nonce, created, "invalid-password");
    if (!password.equals(expectedPassword)) {
        throw new SecurityException("Wrong Password");
    }
    subject.getPrincipals().add(new SimpleGroup(roleName, name));
    subject.setReadOnly();
    return subject;
}
Also used : SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Example 5 with SimpleGroup

use of org.apache.cxf.common.security.SimpleGroup in project cxf by apache.

the class SimpleSubjectCreatingInterceptor method createSubject.

@Override
protected Subject createSubject(String name, String password, boolean isDigest, String nonce, String created) throws SecurityException {
    Subject subject = new Subject();
    // delegate to the external security system if possible
    String roleName = "Alice".equals(name) ? "developers" : "pms";
    subject.getPrincipals().add(new SimplePrincipal(name));
    subject.getPrincipals().add(new SimpleGroup(roleName, name));
    subject.setReadOnly();
    return subject;
}
Also used : SimpleGroup(org.apache.cxf.common.security.SimpleGroup) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Aggregations

SimpleGroup (org.apache.cxf.common.security.SimpleGroup)13 Subject (javax.security.auth.Subject)10 SimplePrincipal (org.apache.cxf.common.security.SimplePrincipal)9 Principal (java.security.Principal)6 Test (org.junit.Test)4 Group (java.security.acl.Group)2 HashSet (java.util.HashSet)2 IOException (java.io.IOException)1 Callback (javax.security.auth.callback.Callback)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 LoginException (javax.security.auth.login.LoginException)1 Claim (org.apache.cxf.rt.security.claims.Claim)1 SAMLClaim (org.apache.cxf.rt.security.saml.claims.SAMLClaim)1 LoginSecurityContext (org.apache.cxf.security.LoginSecurityContext)1 WSUsernameTokenPrincipalImpl (org.apache.wss4j.common.principal.WSUsernameTokenPrincipalImpl)1 UsernameToken (org.apache.wss4j.dom.message.token.UsernameToken)1 Credential (org.apache.wss4j.dom.validate.Credential)1 UsernameSecurityTokenImpl (org.apache.wss4j.stax.impl.securityToken.UsernameSecurityTokenImpl)1