Search in sources :

Example 11 with ClaimsParameters

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

the class AttributeQueryClaimsHandlerTest method retrieveClaimValues.

private ClaimsCollection retrieveClaimValues() {
    ClaimsParameters claimsParameters = mock(ClaimsParameters.class);
    Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn(USERNAME);
    when(claimsParameters.getPrincipal()).thenReturn(principal);
    return spyAttributeQueryClaimsHandler.retrieveClaims(claimsParameters);
}
Also used : Principal(java.security.Principal) ClaimsParameters(ddf.security.claims.ClaimsParameters)

Example 12 with ClaimsParameters

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

the class LdapClaimsHandlerTest method setup.

@Before
public void setup() throws Exception {
    claimsParameters = mock(ClaimsParameters.class);
    when(claimsParameters.getPrincipal()).thenReturn(new UserPrincipal(USER_DN));
    mockEntry = mock(SearchResultEntry.class);
    LinkedAttribute attribute = new LinkedAttribute(ATTRIBUTE_NAME);
    attribute.add(USER_DN);
    mockEntryReader = mock(ConnectionEntryReader.class);
    mockBindRequest = mock(BindRequest.class);
    Map<String, String> map = new HashMap<>();
    map.put(SubjectOperations.NAME_IDENTIFIER_CLAIM_URI, ATTRIBUTE_NAME);
    AttributeMapLoader mockAttributeMapLoader = mock(AttributeMapLoader.class);
    when(mockAttributeMapLoader.buildClaimsMapFile(anyString())).thenReturn(map);
    when(mockAttributeMapLoader.getUser(any(Principal.class))).then(i -> ((Principal) i.getArgument(0)).getName());
    when(mockAttributeMapLoader.getBaseDN(any(Principal.class), anyString(), eq(false))).then(i -> i.getArgument(1));
    claimsHandler = spy(new LdapClaimsHandler(mockAttributeMapLoader));
    doReturn(mockBindRequest).when(claimsHandler).selectBindMethod();
    mockBindResult = mock(BindResult.class);
    mockConnection = mock(Connection.class);
    when(mockConnection.bind(anyString(), any(char[].class))).thenReturn(mockBindResult);
    when(mockConnection.bind(any(BindRequest.class))).thenReturn(mockBindResult);
    when(mockConnection.search(any(), any(), any(), any())).thenReturn(mockEntryReader);
    // two item list (reference and entry)
    when(mockEntryReader.hasNext()).thenReturn(true, true, false);
    // first time indicate a reference followed by entries
    when(mockEntryReader.isEntry()).thenReturn(false, true);
    when(mockEntryReader.readEntry()).thenReturn(mockEntry);
    when(mockEntry.getAttribute(anyString())).thenReturn(attribute);
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    when(mockConnectionFactory.getConnection()).thenReturn(mockConnection);
    claimsHandler.setLdapConnectionFactory(mockConnectionFactory);
    claimsHandler.setPropertyFileLocation("thisstringisnotempty");
    claimsHandler.setBindMethod(BINDING_TYPE);
    claimsHandler.setBindUserCredentials(BIND_USER_CREDENTIALS);
    claimsHandler.setKdcAddress(KCD);
    claimsHandler.setUserBaseDN(USER_BASE_DN);
    claimsHandler.setUserNameAttribute(USER_ATTRIBUTE);
}
Also used : HashMap(java.util.HashMap) BindRequest(org.forgerock.opendj.ldap.requests.BindRequest) Connection(org.forgerock.opendj.ldap.Connection) 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) BindResult(org.forgerock.opendj.ldap.responses.BindResult) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) Principal(java.security.Principal) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) Before(org.junit.Before)

Example 13 with ClaimsParameters

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

the class UsersAttributesFileClaimsHandlerTest method testRetrieveClaimValuesTestHostname.

@Test
public void testRetrieveClaimValuesTestHostname() throws IOException {
    // given
    System.setProperty(SystemBaseUrl.INTERNAL_HOST, "testHostname");
    final UsersAttributesFileClaimsHandler usersAttributesFileClaimsHandler = new UsersAttributesFileClaimsHandler();
    usersAttributesFileClaimsHandler.setUsersAttributesFileLocation(getPathForValidTestAttributesFile());
    final ClaimsCollection ClaimsCollection = getClaimsCollectionForValidTestAttributesFile();
    final Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn("testHostname");
    final ClaimsParameters testHostnameClaimsParameters = new ClaimsParametersImpl(principal, new HashSet<>(), new HashMap<>());
    // when
    final ClaimsCollection processedClaims = usersAttributesFileClaimsHandler.retrieveClaims(testHostnameClaimsParameters);
    // then
    assertThat(processedClaims, containsInAnyOrder(allOf(hasProperty("name", is("Clearance")), hasProperty("values", containsInAnyOrder("U"))), allOf(hasProperty("name", is("CountryOfAffiliation")), hasProperty("values", containsInAnyOrder("USA"))), allOf(hasProperty("name", is("classification")), hasProperty("values", containsInAnyOrder("U"))), allOf(hasProperty("name", is("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")), hasProperty("values", containsInAnyOrder("system@testHostname"))), allOf(hasProperty("name", is("ownerProducer")), hasProperty("values", containsInAnyOrder("USA"))), allOf(hasProperty("name", is("releasableTo")), hasProperty("values", containsInAnyOrder("USA"))), allOf(hasProperty("name", is("FineAccessControls")), hasProperty("values", containsInAnyOrder("SCI1", "SCI2"))), allOf(hasProperty("name", is("disseminationControls")), hasProperty("values", containsInAnyOrder("NF")))));
}
Also used : ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) ClaimsCollection(ddf.security.claims.ClaimsCollection) Principal(java.security.Principal) ClaimsParameters(ddf.security.claims.ClaimsParameters) Test(org.junit.Test)

Example 14 with ClaimsParameters

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

the class UsersAttributesFileClaimsHandlerTest method testNoMatchRetrieveClaimValues.

@Test
public void testNoMatchRetrieveClaimValues() throws IOException {
    // given
    System.setProperty(SystemBaseUrl.INTERNAL_HOST, "testHostname");
    final UsersAttributesFileClaimsHandler usersAttributesFileClaimsHandler = new UsersAttributesFileClaimsHandler();
    usersAttributesFileClaimsHandler.setUsersAttributesFileLocation(getPathForValidTestAttributesFile());
    final ClaimsCollection ClaimsCollection = getClaimsCollectionForValidTestAttributesFile();
    final Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn("someNameThat'sNotInTheUsersAttributesFile");
    final ClaimsParameters unknownClaimsParameters = new ClaimsParametersImpl(principal, new HashSet<>(), new HashMap<>());
    // when
    final ClaimsCollection processedClaims = usersAttributesFileClaimsHandler.retrieveClaims(unknownClaimsParameters);
    // then
    assertThat(processedClaims, is(empty()));
}
Also used : ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) ClaimsCollection(ddf.security.claims.ClaimsCollection) Principal(java.security.Principal) ClaimsParameters(ddf.security.claims.ClaimsParameters) Test(org.junit.Test)

Example 15 with ClaimsParameters

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

the class RoleClaimsHandlerTest method testRetrieveClaimsValuesNestedUserOU.

@Test
public void testRetrieveClaimsValuesNestedUserOU() 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("cn");
    LinkedAttribute groupNameAttribute = new LinkedAttribute("cn");
    ClaimsCollection processedClaims;
    RoleClaimsHandler claimsHandler;
    SearchResultEntry membershipSearchResult = mock(SearchResultEntry.class);
    DN resultDN = DN.valueOf("uid=tstark,OU=nested,");
    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.isEntry()).thenReturn(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, false);
    when(groupNameReader.isEntry()).thenReturn(true);
    when(groupNameReader.readEntry()).thenReturn(groupNameSearchResult);
    when(connection.bind(any())).thenReturn(bindResult);
    when(connection.search(any(), any(), eq("(&(objectClass=groupOfNames)(|(member=cn=tstark,OU=nested,)(member=uid=tstark,OU=nested,)))"), any())).thenReturn(groupNameReader);
    when(connection.search(anyString(), any(), anyString(), matches("cn"))).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");
    claimsHandler.setMembershipUserAttribute("cn");
    claimsHandler.setLoginUserAttribute("uid");
    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)

Aggregations

ClaimsParameters (ddf.security.claims.ClaimsParameters)16 ClaimsCollection (ddf.security.claims.ClaimsCollection)14 Test (org.junit.Test)14 ClaimsParametersImpl (ddf.security.claims.impl.ClaimsParametersImpl)12 Principal (java.security.Principal)12 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)4 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)4 Connection (org.forgerock.opendj.ldap.Connection)4 ConnectionFactory (org.forgerock.opendj.ldap.ConnectionFactory)4 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)4 BindResult (org.forgerock.opendj.ldap.responses.BindResult)4 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)4 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Claim (ddf.security.claims.Claim)3 SubjectUtils (ddf.security.service.impl.SubjectUtils)3 DN (org.forgerock.opendj.ldap.DN)3 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)1 X500Principal (javax.security.auth.x500.X500Principal)1