use of ddf.security.claims.Claim 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));
}
use of ddf.security.claims.Claim in project ddf by codice.
the class LdapClaimsHandlerTest method testRetrieveClaimsValues.
@Test
public void testRetrieveClaimsValues() throws URISyntaxException, LdapException {
when(mockBindResult.isSuccess()).thenReturn(true);
ClaimsCollection processedClaims = claimsHandler.retrieveClaims(claimsParameters);
assertThat(processedClaims, hasSize(1));
Claim claim = processedClaims.get(0);
assertThat(claim.getValues(), contains(DUMMY_VALUE));
verify(mockConnection).search(eq(USER_BASE_DN), any(), eq("(&(objectclass=person)(uid=cn=Tony Stark,ou=avengers,dc=marvel,dc=com))"), eq(ATTRIBUTE_NAME));
}
use of ddf.security.claims.Claim in project ddf by codice.
the class PKIRealm method mergeClaimsToAttributes.
private void mergeClaimsToAttributes(AttributeStatement attributeStatement, ClaimsCollection claims) {
for (Claim claim : claims) {
Attribute newAttr = new AttributeDefault();
newAttr.setName(claim.getName());
newAttr.setValues(claim.getValues());
boolean found = false;
for (Attribute attribute : attributeStatement.getAttributes()) {
if (attribute.getName().equals(newAttr.getName())) {
found = true;
for (String value : newAttr.getValues()) {
attribute.addValue(value);
}
}
}
if (!found) {
attributeStatement.addAttribute(newAttr);
}
}
}
use of ddf.security.claims.Claim in project ddf by codice.
the class UsernamePasswordRealm method mergeClaimsToAttributes.
private void mergeClaimsToAttributes(AttributeStatement attributeStatement, ClaimsCollection claims) {
for (Claim claim : claims) {
Attribute newAttr = new AttributeDefault();
newAttr.setName(claim.getName());
newAttr.setValues(claim.getValues());
boolean found = false;
for (Attribute attribute : attributeStatement.getAttributes()) {
if (attribute.getName().equals(newAttr.getName())) {
found = true;
for (String value : newAttr.getValues()) {
attribute.addValue(value);
}
}
}
if (!found) {
attributeStatement.addAttribute(newAttr);
}
}
}
use of ddf.security.claims.Claim in project ddf by codice.
the class AttributeQueryClaimsHandler method createSingleValuedClaim.
/**
* Creates a single valued claim.
*
* @param claimType The claim type.
* @param claimValue The claim value.
* @return The claim.
* @throws URISyntaxException
*/
protected Claim createSingleValuedClaim(String claimType, String claimValue) {
Claim claim = new ClaimImpl(claimType);
claim.addValue(claimValue);
LOGGER.debug("Created claim with type [{}] and value [{}].", claimType, claimValue);
return claim;
}
Aggregations