Search in sources :

Example 6 with ProcessedClaim

use of org.apache.cxf.sts.claims.ProcessedClaim in project ddf by codice.

the class RoleClaimsHandlerTest method testRetrieveClaimsValuesNotNullPrincipal.

@Test
public void testRetrieveClaimsValuesNotNullPrincipal() throws LdapException, SearchResultReferenceIOException {
    BindResult bindResult = mock(BindResult.class);
    ClaimsParameters claimsParameters;
    Connection connection = mock(Connection.class);
    ConnectionEntryReader membershipReader = mock(ConnectionEntryReader.class);
    ConnectionEntryReader groupNameReader = mock(ConnectionEntryReader.class);
    LDAPConnectionFactory connectionFactory = PowerMockito.mock(LDAPConnectionFactory.class);
    LinkedAttribute membershipAttribute = new LinkedAttribute("uid");
    LinkedAttribute groupNameAttribute = new LinkedAttribute("cn");
    ProcessedClaimCollection processedClaims;
    RoleClaimsHandler claimsHandler;
    SearchResultEntry membershipSearchResult = mock(SearchResultEntry.class);
    SearchResultEntry groupNameSearchResult = mock(SearchResultEntry.class);
    String groupName = "avengers";
    when(bindResult.isSuccess()).thenReturn(true);
    membershipAttribute.add("tstark");
    when(membershipSearchResult.getAttribute(anyString())).thenReturn(membershipAttribute);
    // hasNext() returns 'true' the first time, then 'false' every time after.
    when(membershipReader.hasNext()).thenReturn(true, false);
    when(membershipReader.readEntry()).thenReturn(membershipSearchResult);
    groupNameAttribute.add(groupName);
    when(groupNameSearchResult.getAttribute(anyString())).thenReturn(groupNameAttribute);
    when(groupNameReader.hasNext()).thenReturn(true, false);
    when(groupNameReader.readEntry()).thenReturn(groupNameSearchResult);
    when(connection.bind(anyObject())).thenReturn(bindResult);
    when(connection.search(anyObject(), anyObject(), eq("(&(objectClass=groupOfNames)(member=uid=tstark,))"), anyVararg())).thenReturn(groupNameReader);
    when(connection.search(anyString(), anyObject(), anyString(), matches("uid"))).thenReturn(membershipReader);
    when(connectionFactory.getConnection()).thenReturn(connection);
    claimsHandler = new RoleClaimsHandler();
    claimsHandler.setLdapConnectionFactory(connectionFactory);
    claimsHandler.setBindMethod("Simple");
    claimsHandler.setBindUserCredentials("foo");
    claimsHandler.setBindUserDN("bar");
    claimsParameters = new ClaimsParameters();
    claimsParameters.setPrincipal(new UserPrincipal(USER_CN));
    ClaimCollection claimCollection = new ClaimCollection();
    processedClaims = claimsHandler.retrieveClaimValues(claimCollection, claimsParameters);
    assertThat(processedClaims, hasSize(1));
    ProcessedClaim claim = processedClaims.get(0);
    assertThat(claim.getPrincipal(), equalTo(new UserPrincipal(USER_CN)));
    assertThat(claim.getValues(), hasSize(1));
    assertThat(claim.getValues().get(0), equalTo(groupName));
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) Connection(org.forgerock.opendj.ldap.Connection) Matchers.anyString(org.mockito.Matchers.anyString) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) BindResult(org.forgerock.opendj.ldap.responses.BindResult) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with ProcessedClaim

use of org.apache.cxf.sts.claims.ProcessedClaim in project ddf by codice.

the class PropertyFileClaimsHandler method retrieveClaimValues.

@Override
public ProcessedClaimCollection retrieveClaimValues(ClaimCollection claims, ClaimsParameters parameters) {
    ProcessedClaimCollection claimsColl = new ProcessedClaimCollection();
    Principal principal = parameters.getPrincipal();
    boolean needsRoleClaim = false;
    for (Claim claim : claims) {
        if (roleClaimType.equals(claim.getClaimType().toString())) {
            needsRoleClaim = true;
        } else {
            LOGGER.debug("Unsupported claim: {}", claim.getClaimType());
        }
    }
    String user = getUser(principal);
    if (user == null) {
        LOGGER.info("Could not determine user name, possible authentication error. Returning no claims.");
        return claimsColl;
    }
    if (needsRoleClaim) {
        String userAttributes = userMapping.get(user);
        if (userAttributes != null) {
            String[] attributes = userAttributes.split(",");
            ProcessedClaim c = new ProcessedClaim();
            c.setClaimType(getSupportedClaimTypes().get(0));
            c.setPrincipal(principal);
            for (int i = 1; i < attributes.length; i++) {
                c.addValue(attributes[i]);
            }
            claimsColl.add(c);
        }
    }
    return claimsColl;
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) X500Principal(javax.security.auth.x500.X500Principal) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) Claim(org.apache.cxf.rt.security.claims.Claim)

Example 8 with ProcessedClaim

use of org.apache.cxf.sts.claims.ProcessedClaim in project ddf by codice.

the class TestAttributeFileClaimsHandler method testRetrieveClaimValuesAdmin.

@Test
public void testRetrieveClaimValuesAdmin() {
    ProcessedClaimCollection processedClaims = attributeFileClaimsHandler.retrieveClaimValues(claimCollection, adminClaimsParameters);
    assertThat(processedClaims.size(), is(2));
    for (ProcessedClaim processedClaim : processedClaims) {
        if (processedClaim.getClaimType().toString().equals("test")) {
            assertThat(processedClaim.getValues().size(), is(1));
            assertThat(processedClaim.getValues().get(0), is("testValue"));
        } else if (processedClaim.getClaimType().toString().equals("test1")) {
            assertThat(processedClaim.getValues().size(), is(3));
            assertTrue(processedClaim.getValues().contains("testing1"));
            assertTrue(processedClaim.getValues().contains("testing2"));
            assertTrue(processedClaim.getValues().contains("testing3"));
        }
    }
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) Test(org.junit.Test)

Example 9 with ProcessedClaim

use of org.apache.cxf.sts.claims.ProcessedClaim in project ddf by codice.

the class GuestClaimsHandler method retrieveClaimValues.

@Override
public ProcessedClaimCollection retrieveClaimValues(ClaimCollection claims, ClaimsParameters parameters) {
    ProcessedClaimCollection claimsColl = new ProcessedClaimCollection();
    Principal principal = parameters.getPrincipal();
    for (Claim claim : claims) {
        URI claimType = claim.getClaimType();
        List<String> value = claimsMap.get(claimType);
        if (value != null) {
            ProcessedClaim c = new ProcessedClaim();
            c.setClaimType(claimType);
            c.setPrincipal(principal);
            for (String val : value) {
                c.addValue(val);
            }
            claimsColl.add(c);
        }
    }
    if (principal != null && principal instanceof GuestPrincipal) {
        String ipAddress = ((GuestPrincipal) principal).getAddress();
        if (ipAddress != null) {
            try {
                ProcessedClaim ipClaim = new ProcessedClaim();
                ipClaim.setClaimType(new URI(IP_ADDRESS_CLAIMS_KEY));
                ipClaim.setPrincipal(principal);
                ipClaim.addValue(ipAddress);
                claimsColl.add(ipClaim);
            } catch (URISyntaxException e) {
                LOGGER.info("Claims mapping cannot be converted to a URI. Ip claim will be excluded", e);
            }
        }
    }
    return claimsColl;
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) GuestPrincipal(ddf.security.principal.GuestPrincipal) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GuestPrincipal(ddf.security.principal.GuestPrincipal) Principal(java.security.Principal) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) Claim(org.apache.cxf.rt.security.claims.Claim)

Example 10 with ProcessedClaim

use of org.apache.cxf.sts.claims.ProcessedClaim in project ddf by codice.

the class GuestClaimsHandlerTest method testRetrieveClaims.

@Test
public void testRetrieveClaims() throws URISyntaxException {
    GuestClaimsHandler claimsHandler = new GuestClaimsHandler();
    claimsHandler.setAttributes(Arrays.asList("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier=Guest", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress=Guest@guest.com|someguy@somesite.com|somedude@cool.com", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname=Guest"));
    ClaimCollection requestClaims = new ClaimCollection();
    Claim requestClaim = new Claim();
    URI nameURI = new URI("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
    requestClaim.setClaimType(nameURI);
    requestClaims.add(requestClaim);
    requestClaim = new Claim();
    URI emailURI = new URI("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress");
    requestClaim.setClaimType(emailURI);
    requestClaims.add(requestClaim);
    requestClaim = new Claim();
    URI fooURI = new URI("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/foobar");
    requestClaim.setClaimType(fooURI);
    requestClaim.setOptional(true);
    requestClaims.add(requestClaim);
    ClaimsParameters claimsParameters = new ClaimsParameters();
    claimsParameters.setPrincipal(new GuestPrincipal("127.0.0.1"));
    List<URI> supportedClaims = claimsHandler.getSupportedClaimTypes();
    assertEquals(3, supportedClaims.size());
    ProcessedClaimCollection claimsCollection = claimsHandler.retrieveClaimValues(requestClaims, claimsParameters);
    assertEquals(3, claimsCollection.size());
    for (ProcessedClaim claim : claimsCollection) {
        if (claim.getClaimType().equals(nameURI)) {
            assertEquals(1, claim.getValues().size());
            assertEquals("Guest", claim.getValues().get(0));
        } else if (claim.getClaimType().equals(emailURI)) {
            assertEquals(3, claim.getValues().size());
            List<Object> values = claim.getValues();
            assertEquals("Guest@guest.com", values.get(0));
            assertEquals("someguy@somesite.com", values.get(1));
            assertEquals("somedude@cool.com", values.get(2));
        } else if (claim.getClaimType().equals("IpAddress")) {
            assertEquals("127.0.0.1", claim.getValues().get(0));
        }
        assertFalse(claim.getClaimType().equals(fooURI));
    }
    claimsParameters = new ClaimsParameters();
    claimsCollection = claimsHandler.retrieveClaimValues(requestClaims, claimsParameters);
    assertEquals(2, claimsCollection.size());
    claimsParameters = new ClaimsParameters();
    claimsParameters.setPrincipal(new CustomTokenPrincipal("SomeValue"));
    claimsCollection = claimsHandler.retrieveClaimValues(requestClaims, claimsParameters);
    assertEquals(2, claimsCollection.size());
}
Also used : CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) GuestPrincipal(ddf.security.principal.GuestPrincipal) List(java.util.List) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) URI(java.net.URI) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) Claim(org.apache.cxf.rt.security.claims.Claim) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) Test(org.junit.Test)

Aggregations

ProcessedClaim (org.apache.cxf.sts.claims.ProcessedClaim)11 ProcessedClaimCollection (org.apache.cxf.sts.claims.ProcessedClaimCollection)9 URI (java.net.URI)5 Principal (java.security.Principal)5 Claim (org.apache.cxf.rt.security.claims.Claim)5 List (java.util.List)3 X500Principal (javax.security.auth.x500.X500Principal)3 ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)3 ClaimsParameters (org.apache.cxf.sts.claims.ClaimsParameters)3 Connection (org.forgerock.opendj.ldap.Connection)3 BindResult (org.forgerock.opendj.ldap.responses.BindResult)3 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)3 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)3 Test (org.junit.Test)3 GuestPrincipal (ddf.security.principal.GuestPrincipal)2 ArrayList (java.util.ArrayList)2 Attribute (org.forgerock.opendj.ldap.Attribute)2 ByteString (org.forgerock.opendj.ldap.ByteString)2 LdapException (org.forgerock.opendj.ldap.LdapException)2 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)2