use of ddf.security.assertion.AttributeStatement in project ddf by codice.
the class SubjectIdentityTest method getSubjectWithAttributes.
private Subject getSubjectWithAttributes(Map<String, List<String>> attributes) {
Subject subject = mock(Subject.class);
PrincipalCollection pc = mock(PrincipalCollection.class);
SecurityAssertion assertion = mock(SecurityAssertion.class);
AttributeStatement as = mock(AttributeStatement.class);
List<Attribute> attrs = attributes.entrySet().stream().map(this::getAttribute).collect(Collectors.toList());
doReturn(pc).when(subject).getPrincipals();
doReturn(Collections.singletonList(assertion)).when(pc).byType(SecurityAssertion.class);
doReturn(ImmutableList.of(assertion)).when(pc).byType(SecurityAssertion.class);
doReturn(Collections.singletonList(as)).when(assertion).getAttributeStatements();
doReturn(attrs).when(as).getAttributes();
return subject;
}
use of ddf.security.assertion.AttributeStatement 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.AttributeStatement in project ddf by codice.
the class PKIRealm method createPrincipalCollectionFromCertificate.
private SimplePrincipalCollection createPrincipalCollectionFromCertificate(X500Principal principal) {
SimplePrincipalCollection principals = new SimplePrincipalCollection();
DefaultSecurityAssertionBuilder assertionBuilder = new DefaultSecurityAssertionBuilder();
AttributeStatement attributeStatement = new AttributeStatementDefault();
HashMap<String, Object> properties = createProperties(principal);
for (ClaimsHandler claimsHandler : claimsHandlers) {
ClaimsCollection claims = claimsHandler.retrieveClaims(new ClaimsParametersImpl(principal, Collections.singleton(principal), properties));
mergeClaimsToAttributes(attributeStatement, claims);
}
final Instant now = Instant.now();
SecurityAssertion assertion = assertionBuilder.addAttributeStatement(attributeStatement).userPrincipal(principal).weight(SecurityAssertion.LOCAL_AUTH_WEIGHT).issuer("DDF").notBefore(Date.from(now)).notOnOrAfter(Date.from(now.plus(fourHours))).tokenType(PKI_TOKEN_TYPE).build();
principals.add(assertion, "PKI");
return principals;
}
use of ddf.security.assertion.AttributeStatement in project ddf by codice.
the class UsernamePasswordRealm method createPrincipalCollectionFromSubject.
private SimplePrincipalCollection createPrincipalCollectionFromSubject(Subject subject) {
SimplePrincipalCollection principals = new SimplePrincipalCollection();
DefaultSecurityAssertionBuilder assertionBuilder = new DefaultSecurityAssertionBuilder();
AttributeStatement attributeStatement = new AttributeStatementDefault();
Principal userPrincipal = subject.getPrincipals().stream().filter(p -> p instanceof UserPrincipal).findFirst().orElseThrow(AuthenticationException::new);
Set<Principal> rolePrincipals = subject.getPrincipals().stream().filter(p -> p instanceof RolePrincipal).collect(Collectors.toSet());
for (ClaimsHandler claimsHandler : claimsHandlers) {
ClaimsCollection claims = claimsHandler.retrieveClaims(new ClaimsParametersImpl(userPrincipal, rolePrincipals, new HashMap<>()));
mergeClaimsToAttributes(attributeStatement, claims);
}
final Instant now = Instant.now();
assertionBuilder.addAttributeStatement(attributeStatement).userPrincipal(userPrincipal).weight(SecurityAssertion.LOCAL_AUTH_WEIGHT).issuer("DDF").notBefore(Date.from(now)).notOnOrAfter(Date.from(now.plus(fourHours)));
for (Principal principal : rolePrincipals) {
assertionBuilder.addPrincipal(principal);
}
assertionBuilder.tokenType(USER_PASS_TOKEN_TYPE);
SecurityAssertion assertion = assertionBuilder.build();
principals.add(assertion, "UP");
return principals;
}
use of ddf.security.assertion.AttributeStatement in project ddf by codice.
the class DefaultSecurityAssertionBuilderTest method testDefaultSecurityAssertionBuilder.
@Test
public void testDefaultSecurityAssertionBuilder() {
Principal principal = mock(Principal.class);
AttributeStatement attributeStatement = mock(AttributeStatement.class);
AuthenticationStatement authenticationStatement = mock(AuthenticationStatement.class);
Object token = new Object();
Date notBefore = Date.from(Instant.now());
Date notOnOrAfter = Date.from(Instant.now().plus(Duration.ofMinutes(1)));
DefaultSecurityAssertionBuilder builder = new DefaultSecurityAssertionBuilder();
SecurityAssertion assertion = builder.userPrincipal(principal).addPrincipal(principal).issuer("test").addAttributeStatement(attributeStatement).addAuthnStatement(authenticationStatement).addSubjectConfirmation("subjectConfirmation").tokenType("testToken").token(token).notBefore(notBefore).notOnOrAfter(notOnOrAfter).weight(7).build();
assertThat(assertion.getPrincipal(), is(principal));
assertThat(assertion.getPrincipals(), hasItem(principal));
assertThat(assertion.getIssuer(), is("test"));
assertThat(assertion.getAttributeStatements(), hasItem(attributeStatement));
assertThat(assertion.getSubjectConfirmations(), hasItem("subjectConfirmation"));
assertThat(assertion.getTokenType(), is("testToken"));
assertThat(assertion.getToken(), is(token));
assertThat(assertion.getNotBefore(), is(notBefore));
assertThat(assertion.getNotOnOrAfter(), is(notOnOrAfter));
assertThat(assertion.getWeight(), is(7));
}
Aggregations