Search in sources :

Example 11 with ClaimsParameters

use of org.apache.cxf.sts.claims.ClaimsParameters in project ddf by codice.

the class TestAttributeFileClaimsHandler method setup.

@Before
public void setup() throws IOException {
    System.setProperty("ddf.home", "testdir");
    InputStream resourceAsStream = TestAttributeFileClaimsHandler.class.getResourceAsStream("/users.attributes");
    userFile = temporaryFolder.newFile("users.attributes");
    FileOutputStream userFileOs = new FileOutputStream(userFile);
    IOUtils.copy(resourceAsStream, userFileOs);
    attributeFileClaimsHandler.setAttributeFileLocation(userFile.getAbsolutePath());
    attributeFileClaimsHandler.init();
    claimCollection = new ClaimCollection();
    Claim claim = new Claim();
    claim.setClaimType(URI.create("test"));
    claimCollection.add(claim);
    Claim claim1 = new Claim();
    claim1.setClaimType(URI.create("test1"));
    claimCollection.add(claim1);
    Claim claim2 = new Claim();
    claim2.setClaimType(URI.create("reg"));
    claimCollection.add(claim2);
    adminClaimsParameters = new ClaimsParameters();
    Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn("admin");
    adminClaimsParameters.setPrincipal(principal);
    localhostClaimsParameters = new ClaimsParameters();
    principal = mock(Principal.class);
    when(principal.getName()).thenReturn("localhost");
    localhostClaimsParameters.setPrincipal(principal);
    regexClaimsParameters = new ClaimsParameters();
    principal = mock(Principal.class);
    when(principal.getName()).thenReturn("myhostname");
    regexClaimsParameters.setPrincipal(principal);
    unknownClaimsParameters = new ClaimsParameters();
    principal = mock(Principal.class);
    when(principal.getName()).thenReturn("unknown");
    unknownClaimsParameters.setPrincipal(principal);
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) Claim(org.apache.cxf.rt.security.claims.Claim) Principal(java.security.Principal) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) Before(org.junit.Before)

Example 12 with ClaimsParameters

use of org.apache.cxf.sts.claims.ClaimsParameters in project ddf by codice.

the class TestPropertyFileClaimsHandler method testRetrieveClaimValues.

@Test
public void testRetrieveClaimValues() {
    PropertyFileClaimsHandler propertyFileClaimsHandler = new PropertyFileClaimsHandler();
    propertyFileClaimsHandler.setPropertyFileLocation("/users.properties");
    propertyFileClaimsHandler.setRoleClaimType("http://myroletype");
    ClaimCollection claimCollection = new ClaimCollection();
    Claim claim = new Claim();
    try {
        claim.setClaimType(new URI("http://myroletype"));
    } catch (URISyntaxException e) {
        fail("Could not create URI");
    }
    claimCollection.add(claim);
    ClaimsParameters claimsParameters = mock(ClaimsParameters.class);
    Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn("admin");
    when(claimsParameters.getPrincipal()).thenReturn(principal);
    ProcessedClaimCollection processedClaimCollection = propertyFileClaimsHandler.retrieveClaimValues(claimCollection, claimsParameters);
    assertEquals(1, processedClaimCollection.size());
    assertEquals(4, processedClaimCollection.get(0).getValues().size());
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) URISyntaxException(java.net.URISyntaxException) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) URI(java.net.URI) Claim(org.apache.cxf.rt.security.claims.Claim) X500Principal(javax.security.auth.x500.X500Principal) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) Test(org.junit.Test)

Example 13 with ClaimsParameters

use of org.apache.cxf.sts.claims.ClaimsParameters in project ddf by codice.

the class TestPropertyFileClaimsHandler method testRetrieveClaimsValuesNullPrincipal.

@Test
public void testRetrieveClaimsValuesNullPrincipal() {
    PropertyFileClaimsHandler claimsHandler = new PropertyFileClaimsHandler();
    ClaimsParameters claimsParameters = new ClaimsParameters();
    ClaimCollection claimCollection = new ClaimCollection();
    ProcessedClaimCollection processedClaims = claimsHandler.retrieveClaimValues(claimCollection, claimsParameters);
    Assert.assertThat(processedClaims.size(), CoreMatchers.is(equalTo(0)));
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) Test(org.junit.Test)

Example 14 with ClaimsParameters

use of org.apache.cxf.sts.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);
    PowerMockito.mockStatic(BindMethodChooser.class);
    when(BindMethodChooser.selectBindMethod(eq(BINDING_TYPE), eq(BIND_USER_DN), eq(BIND_USER_CREDENTIALS), eq(REALM), eq(KCD))).thenReturn(mockBindRequest);
    Map<String, String> map = new HashMap<>();
    map.put(NAME_IDENTIFIER_CLAIM_URI, ATTRIBUTE_NAME);
    PowerMockito.mockStatic(AttributeMapLoader.class);
    when(AttributeMapLoader.buildClaimsMapFile(anyString())).thenReturn(map);
    when(AttributeMapLoader.getUser(any(Principal.class))).then(i -> i.getArgumentAt(0, Principal.class).getName());
    when(AttributeMapLoader.getBaseDN(any(Principal.class), anyString(), eq(false))).then(i -> i.getArgumentAt(1, String.class));
    claimsHandler = new LdapClaimsHandler();
    mockBindResult = mock(BindResult.class);
    mockConnection = mock(Connection.class);
    mockConnectionFactory = PowerMockito.mock(LDAPConnectionFactory.class);
    when(mockConnectionFactory.getConnection()).thenReturn(mockConnection);
    when(mockConnection.bind(anyString(), any(char[].class))).thenReturn(mockBindResult);
    when(mockConnection.bind(any(BindRequest.class))).thenReturn(mockBindResult);
    when(mockConnection.search(anyObject(), anyObject(), anyObject(), anyObject())).thenReturn(mockEntryReader);
    when(mockEntryReader.hasNext()).thenReturn(true, false);
    when(mockEntryReader.readEntry()).thenReturn(mockEntry);
    when(mockEntry.getAttribute(anyString())).thenReturn(attribute);
    claimsHandler.setLdapConnectionFactory(mockConnectionFactory);
    claimsHandler.setPropertyFileLocation("thisstringisnotempty");
    claimsHandler.setBindMethod(BINDING_TYPE);
    claimsHandler.setBindUserCredentials(BIND_USER_CREDENTIALS);
    claimsHandler.setRealm(REALM);
    claimsHandler.setKdcAddress(KCD);
    claimsHandler.setUserBaseDN(USER_BASE_DN);
    claims = new ClaimCollection();
    Claim claim = new Claim();
    claim.setClaimType(new URI(NAME_IDENTIFIER_CLAIM_URI));
    claims.add(claim);
}
Also used : HashMap(java.util.HashMap) BindRequest(org.forgerock.opendj.ldap.requests.BindRequest) Connection(org.forgerock.opendj.ldap.Connection) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) NAME_IDENTIFIER_CLAIM_URI(ddf.security.SubjectUtils.NAME_IDENTIFIER_CLAIM_URI) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) BindResult(org.forgerock.opendj.ldap.responses.BindResult) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) Principal(java.security.Principal) Claim(org.apache.cxf.rt.security.claims.Claim) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) Before(org.junit.Before)

Example 15 with ClaimsParameters

use of org.apache.cxf.sts.claims.ClaimsParameters in project ddf by codice.

the class RoleClaimsHandlerTest method testRetrieveClaimsValuesNotNullPrincipal.

@Test
public void testRetrieveClaimsValuesNotNullPrincipal() 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);
    LDAPConnectionFactory connectionFactory = PowerMockito.mock(LDAPConnectionFactory.class);
    LinkedAttribute membershipAttribute = new LinkedAttribute("uid");
    LinkedAttribute groupNameAttribute = new LinkedAttribute("cn");
    ProcessedClaimCollection processedClaims;
    RoleClaimsHandler claimsHandler;
    SearchResultEntry membershipSearchResult = mock(SearchResultEntry.class);
    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.readEntry()).thenReturn(membershipSearchResult);
    groupNameAttribute.add(groupName);
    when(groupNameSearchResult.getAttribute(anyString())).thenReturn(groupNameAttribute);
    when(groupNameReader.hasNext()).thenReturn(true, false);
    when(groupNameReader.readEntry()).thenReturn(groupNameSearchResult);
    when(connection.bind(anyObject())).thenReturn(bindResult);
    when(connection.search(anyObject(), anyObject(), eq("(&(objectClass=groupOfNames)(member=uid=tstark,))"), anyVararg())).thenReturn(groupNameReader);
    when(connection.search(anyString(), anyObject(), anyString(), matches("uid"))).thenReturn(membershipReader);
    when(connectionFactory.getConnection()).thenReturn(connection);
    claimsHandler = new RoleClaimsHandler();
    claimsHandler.setLdapConnectionFactory(connectionFactory);
    claimsHandler.setBindMethod("Simple");
    claimsHandler.setBindUserCredentials("foo");
    claimsHandler.setBindUserDN("bar");
    claimsParameters = new ClaimsParameters();
    claimsParameters.setPrincipal(new UserPrincipal(USER_CN));
    ClaimCollection claimCollection = new ClaimCollection();
    processedClaims = claimsHandler.retrieveClaimValues(claimCollection, claimsParameters);
    assertThat(processedClaims, hasSize(1));
    ProcessedClaim claim = processedClaims.get(0);
    assertThat(claim.getPrincipal(), equalTo(new UserPrincipal(USER_CN)));
    assertThat(claim.getValues(), hasSize(1));
    assertThat(claim.getValues().get(0), equalTo(groupName));
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) Connection(org.forgerock.opendj.ldap.Connection) Matchers.anyString(org.mockito.Matchers.anyString) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) BindResult(org.forgerock.opendj.ldap.responses.BindResult) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)17 ClaimsParameters (org.apache.cxf.sts.claims.ClaimsParameters)17 ProcessedClaimCollection (org.apache.cxf.sts.claims.ProcessedClaimCollection)17 Test (org.junit.Test)13 Principal (java.security.Principal)10 URI (java.net.URI)6 HashMap (java.util.HashMap)6 Claim (org.apache.cxf.rt.security.claims.Claim)6 ProcessedClaim (org.apache.cxf.sts.claims.ProcessedClaim)4 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)2 Connection (org.forgerock.opendj.ldap.Connection)2 LDAPConnectionFactory (org.forgerock.opendj.ldap.LDAPConnectionFactory)2 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)2 BindResult (org.forgerock.opendj.ldap.responses.BindResult)2 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)2 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2