use of ddf.security.assertion.Attribute in project ddf by codice.
the class SecurityPluginTest method setupMockSubject.
private Subject setupMockSubject() {
List<String> listOfAttributeValues = Arrays.asList(TEST_USER);
Attribute mockAttribute = mock(Attribute.class);
when(mockAttribute.getName()).thenReturn(SubjectOperations.EMAIL_ADDRESS_CLAIM_URI);
when(mockAttribute.getValues()).thenReturn(listOfAttributeValues);
List<Attribute> listOfAttributes = Arrays.asList(mockAttribute);
AttributeStatement mockAttributeStatement = mock(AttributeStatement.class);
when(mockAttributeStatement.getAttributes()).thenReturn(listOfAttributes);
List<AttributeStatement> listOfAttributeStatements = Arrays.asList(mockAttributeStatement);
Subject mockSubject = mock(Subject.class);
PrincipalCollection mockPrincipals = mock(PrincipalCollection.class);
SecurityAssertion mockSecurityAssertion = mock(SecurityAssertion.class);
when(mockSecurityAssertion.getAttributeStatements()).thenReturn(listOfAttributeStatements);
when(mockPrincipals.byType(SecurityAssertion.class)).thenReturn(Collections.singletonList(mockSecurityAssertion));
when(mockSubject.getPrincipals()).thenReturn(mockPrincipals);
return mockSubject;
}
use of ddf.security.assertion.Attribute in project ddf by codice.
the class SubjectUtils method getAttribute.
/**
* Get any attribute from a subject by key.
*
* @param subject
* @param key
* @return attribute values or an empty list if not found.
*/
@Override
public List<String> getAttribute(@Nullable Subject subject, String key) {
Validate.notNull(key);
if (subject == null) {
LOGGER.debug("Incoming subject was null, cannot look up {}.", key);
return Collections.emptyList();
}
PrincipalCollection principals = subject.getPrincipals();
if (principals == null) {
LOGGER.debug("No principals located in the incoming subject, cannot look up {}.", key);
return Collections.emptyList();
}
Collection<SecurityAssertion> assertions = principals.byType(SecurityAssertion.class);
if (assertions.isEmpty()) {
LOGGER.debug("Could not find Security Assertion, cannot look up {}.", key);
return Collections.emptyList();
}
List<SecurityAssertion> assertionList = new ArrayList<>(assertions);
assertionList.sort(new SecurityAssertionComparator());
return assertionList.stream().map(SecurityAssertion::getAttributeStatements).flatMap(List::stream).flatMap(as -> as.getAttributes().stream()).filter(a -> a.getName().equals(key)).flatMap(a -> a.getValues().stream()).collect(Collectors.toList());
}
use of ddf.security.assertion.Attribute in project ddf by codice.
the class SubjectIdentityTest method getAttribute.
private Attribute getAttribute(Map.Entry<String, List<String>> attribute) {
Attribute attr = mock(Attribute.class);
doReturn(attribute.getKey()).when(attr).getName();
doReturn(attribute.getValue()).when(attr).getValues();
return attr;
}
use of ddf.security.assertion.Attribute in project ddf by codice.
the class SubjectUtilsTest method getAttribute.
private Attribute getAttribute(Map.Entry<String, List<String>> attribute) {
Attribute mockAttribute = mock(Attribute.class);
doReturn(attribute.getKey()).when(mockAttribute).getName();
doReturn(attribute.getValue()).when(mockAttribute).getValues();
return mockAttribute;
}
use of ddf.security.assertion.Attribute in project ddf by codice.
the class GuestRealmTest method testDoGetAuthenticationInfo.
@Test
public void testDoGetAuthenticationInfo() {
BaseAuthenticationToken baseAuthenticationToken = new MockBaseAuthenticationToken("principal", "credentials", "0.0.0.0");
baseAuthenticationToken.setAllowGuest(true);
AuthenticationInfo authenticationInfo = guestRealm.doGetAuthenticationInfo(baseAuthenticationToken);
assertEquals(baseAuthenticationToken.getCredentials(), authenticationInfo.getCredentials());
PrincipalCollection principals = authenticationInfo.getPrincipals();
assertEquals(2, principals.asList().size());
Iterator iterator = principals.iterator();
assertEquals("Guest@0.0.0.0", iterator.next());
Object next = iterator.next();
assertTrue(next instanceof SecurityAssertion);
SecurityAssertion securityAssertion = (SecurityAssertion) next;
assertEquals(2, securityAssertion.getAttributeStatements().get(0).getAttributes().size());
boolean claim1 = false;
boolean claim2 = false;
boolean claim3 = false;
boolean claim4 = false;
for (Attribute attribute : securityAssertion.getAttributeStatements().get(0).getAttributes()) {
if (attribute.getName().equals("claim1")) {
claim1 = true;
assertEquals("value1", attribute.getValues().get(0));
}
if (attribute.getName().equals("claim2")) {
claim2 = true;
assertTrue(attribute.getValues().stream().anyMatch(v -> v.equals("value2")));
assertTrue(attribute.getValues().stream().anyMatch(v -> v.equals("value3")));
}
if (attribute.getName().equals(":")) {
claim3 = true;
}
if (attribute.getName().equals("bad")) {
claim4 = true;
}
}
assertTrue(claim1);
assertTrue(claim2);
assertFalse(claim3);
assertFalse(claim4);
AuthenticationInfo newAuthenticationInfo = guestRealm.doGetAuthenticationInfo(baseAuthenticationToken);
assertNotSame(authenticationInfo, newAuthenticationInfo);
}
Aggregations