Search in sources :

Example 6 with ClaimsCollection

use of ddf.security.claims.ClaimsCollection in project ddf by codice.

the class CertificateClaimsHandlerTest method testRetrieveClaimValuesWithAltNamesRequested.

@Test
public void testRetrieveClaimValuesWithAltNamesRequested() throws URISyntaxException {
    CertificateClaimsHandler certificateClaimsHandler = new CertificateClaimsHandler();
    certificateClaimsHandler.setCountryClaim("Country");
    certificateClaimsHandler.setEmailClaim("Email");
    Map<String, Object> map = new HashMap<>();
    map.put(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI, "local@localhost");
    map.put(SubjectOperations.COUNTRY_CLAIM_URI, "USA");
    ClaimsParameters parameters = new ClaimsParametersImpl(mock(Principal.class), new HashSet<>(), map);
    ClaimsCollection processedClaims = certificateClaimsHandler.retrieveClaims(parameters);
    assertThat(processedClaims.size(), is(2));
    assertThat(processedClaims.stream().map(ddf.security.claims.Claim::getName).collect(Collectors.toList()), containsInAnyOrder("Email", "Country"));
}
Also used : ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) HashMap(java.util.HashMap) ClaimsCollection(ddf.security.claims.ClaimsCollection) Principal(java.security.Principal) ClaimsParameters(ddf.security.claims.ClaimsParameters) Test(org.junit.Test)

Example 7 with ClaimsCollection

use of ddf.security.claims.ClaimsCollection in project ddf by codice.

the class CertificateClaimsHandlerTest method testRetrieveClaimValuesWithEmail.

@Test
public void testRetrieveClaimValuesWithEmail() throws URISyntaxException {
    CertificateClaimsHandler certificateClaimsHandler = new CertificateClaimsHandler();
    Map<String, Object> map = new HashMap<>();
    map.put(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI, "local@localhost");
    ClaimsParameters parameters = new ClaimsParametersImpl(mock(Principal.class), new HashSet<>(), map);
    ClaimsCollection processedClaims = certificateClaimsHandler.retrieveClaims(parameters);
    assertThat(processedClaims.size(), is(1));
    assertThat(processedClaims.stream().map(ddf.security.claims.Claim::getName).collect(Collectors.toList()), containsInAnyOrder(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI));
}
Also used : ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) HashMap(java.util.HashMap) ClaimsCollection(ddf.security.claims.ClaimsCollection) Principal(java.security.Principal) ClaimsParameters(ddf.security.claims.ClaimsParameters) Test(org.junit.Test)

Example 8 with ClaimsCollection

use of ddf.security.claims.ClaimsCollection in project ddf by codice.

the class RoleClaimsHandlerTest method testRetrieveClaimsValuesIgnoredReferences.

@Test
public void testRetrieveClaimsValuesIgnoredReferences() 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);
    LinkedAttribute membershipAttribute = new LinkedAttribute("uid");
    LinkedAttribute groupNameAttribute = new LinkedAttribute("cn");
    ClaimsCollection processedClaims;
    RoleClaimsHandler claimsHandler;
    SearchResultEntry membershipSearchResult = mock(SearchResultEntry.class);
    DN resultDN = DN.valueOf("uid=tstark,");
    SearchResultEntry groupNameSearchResult = mock(SearchResultEntry.class);
    String groupName = "avengers";
    when(bindResult.isSuccess()).thenReturn(true);
    membershipAttribute.add("tstark");
    when(membershipSearchResult.getAttribute(anyString())).thenReturn(membershipAttribute);
    // simulate two items in the list (a reference and an entry)
    when(membershipReader.hasNext()).thenReturn(true, true, false);
    // test a reference followed by entries thereafter
    when(membershipReader.isEntry()).thenReturn(false, true);
    when(membershipReader.readEntry()).thenReturn(membershipSearchResult);
    when(membershipSearchResult.getName()).thenReturn(resultDN);
    groupNameAttribute.add(groupName);
    when(groupNameSearchResult.getAttribute(anyString())).thenReturn(groupNameAttribute);
    when(groupNameReader.hasNext()).thenReturn(true, true, false);
    when(groupNameReader.isEntry()).thenReturn(false, true);
    when(groupNameReader.readEntry()).thenReturn(groupNameSearchResult);
    when(connection.bind(any())).thenReturn(bindResult);
    when(connection.search(any(), any(), eq("(&(objectClass=groupOfNames)(|(member=uid=tstark,)(member=uid=tstark,)))"), any())).thenReturn(groupNameReader);
    when(connection.search(anyString(), any(), anyString(), matches("uid"))).thenReturn(membershipReader);
    claimsHandler = new RoleClaimsHandler(new AttributeMapLoader(new SubjectUtils()));
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    when(mockConnectionFactory.getConnection()).thenReturn(connection);
    claimsHandler.setLdapConnectionFactory(mockConnectionFactory);
    claimsHandler.setBindMethod("Simple");
    claimsHandler.setBindUserCredentials("foo");
    claimsHandler.setBindUserDN("bar");
    claimsParameters = new ClaimsParametersImpl(new UserPrincipal(USER_CN), new HashSet<>(), new HashMap<>());
    processedClaims = claimsHandler.retrieveClaims(claimsParameters);
    assertThat(processedClaims, hasSize(1));
    Claim claim = processedClaims.get(0);
    assertThat(claim.getValues(), hasSize(1));
    assertThat(claim.getValues().get(0), equalTo(groupName));
}
Also used : SubjectUtils(ddf.security.service.impl.SubjectUtils) HashMap(java.util.HashMap) Connection(org.forgerock.opendj.ldap.Connection) DN(org.forgerock.opendj.ldap.DN) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ClaimsParameters(ddf.security.claims.ClaimsParameters) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) BindResult(org.forgerock.opendj.ldap.responses.BindResult) ClaimsCollection(ddf.security.claims.ClaimsCollection) Claim(ddf.security.claims.Claim) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with ClaimsCollection

use of ddf.security.claims.ClaimsCollection in project ddf by codice.

the class CertificateClaimsHandler method retrieveClaims.

@Override
public ClaimsCollection retrieveClaims(ClaimsParameters parameters) {
    ClaimsCollection claimsColl = new ClaimsCollectionImpl();
    Map<String, Object> additionalProperties = parameters.getAdditionalProperties();
    if (additionalProperties != null) {
        if (additionalProperties.containsKey(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI)) {
            buildClaim(claimsColl, emailClaim, additionalProperties.get(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI));
        }
        if (additionalProperties.containsKey(SubjectOperations.COUNTRY_CLAIM_URI)) {
            buildClaim(claimsColl, countryClaim, additionalProperties.get(SubjectOperations.COUNTRY_CLAIM_URI));
        }
    }
    return claimsColl;
}
Also used : ClaimsCollectionImpl(ddf.security.claims.impl.ClaimsCollectionImpl) ClaimsCollection(ddf.security.claims.ClaimsCollection)

Example 10 with ClaimsCollection

use of ddf.security.claims.ClaimsCollection in project ddf by codice.

the class LdapClaimsHandlerTest method testRetrieveClaimsValuesNullPrincipal.

@Test
public void testRetrieveClaimsValuesNullPrincipal() throws LdapException {
    when(mockBindResult.isSuccess()).thenReturn(false);
    ClaimsCollection processedClaims = claimsHandler.retrieveClaims(claimsParameters);
    assertThat(processedClaims.size(), CoreMatchers.is(equalTo(0)));
}
Also used : ClaimsCollection(ddf.security.claims.ClaimsCollection) Test(org.junit.Test)

Aggregations

ClaimsCollection (ddf.security.claims.ClaimsCollection)29 Test (org.junit.Test)18 Principal (java.security.Principal)16 ClaimsParameters (ddf.security.claims.ClaimsParameters)14 ClaimsParametersImpl (ddf.security.claims.impl.ClaimsParametersImpl)14 HashMap (java.util.HashMap)10 Claim (ddf.security.claims.Claim)9 ClaimsCollectionImpl (ddf.security.claims.impl.ClaimsCollectionImpl)9 ClaimImpl (ddf.security.claims.impl.ClaimImpl)6 ClaimsHandler (ddf.security.claims.ClaimsHandler)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 X500Principal (javax.security.auth.x500.X500Principal)4 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)4 Connection (org.forgerock.opendj.ldap.Connection)4 BindResult (org.forgerock.opendj.ldap.responses.BindResult)4 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)4 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)4 SubjectUtils (ddf.security.service.impl.SubjectUtils)3 AttributeStatement (ddf.security.assertion.AttributeStatement)2