Search in sources :

Example 6 with LdapClaimsHandler

use of org.apache.cxf.sts.claims.LdapClaimsHandler in project cxf by apache.

the class LDAPClaimsTest method testRetrieveClaims.

@org.junit.Test
public void testRetrieveClaims() throws Exception {
    LdapClaimsHandler claimsHandler = (LdapClaimsHandler) appContext.getBean("testClaimsHandler");
    ClaimsManager claimsManager = new ClaimsManager();
    claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
    String user = props.getProperty("claimUser");
    Assert.assertNotNull(user, "Property 'claimUser' not configured");
    ClaimCollection requestedClaims = createRequestClaimCollection();
    List<URI> expectedClaims = new ArrayList<>();
    expectedClaims.add(ClaimTypes.FIRSTNAME);
    expectedClaims.add(ClaimTypes.LASTNAME);
    expectedClaims.add(ClaimTypes.EMAILADDRESS);
    ClaimsParameters params = new ClaimsParameters();
    params.setPrincipal(new CustomTokenPrincipal(user));
    ProcessedClaimCollection retrievedClaims = claimsManager.retrieveClaimValues(requestedClaims, params);
    Assert.assertTrue("Retrieved number of claims [" + retrievedClaims.size() + "] doesn't match with expected [" + expectedClaims.size() + "]", retrievedClaims.size() == expectedClaims.size());
    for (ProcessedClaim c : retrievedClaims) {
        if (expectedClaims.contains(c.getClaimType())) {
            expectedClaims.remove(c.getClaimType());
        } else {
            Assert.assertTrue("Claim '" + c.getClaimType() + "' not requested", false);
        }
    }
}
Also used : CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) ArrayList(java.util.ArrayList) LdapClaimsHandler(org.apache.cxf.sts.claims.LdapClaimsHandler) ClaimsManager(org.apache.cxf.sts.claims.ClaimsManager) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) URI(java.net.URI) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters)

Example 7 with LdapClaimsHandler

use of org.apache.cxf.sts.claims.LdapClaimsHandler in project cxf by apache.

the class LDAPClaimsTest method testRetrieveBinaryClaims.

@org.junit.Test
public void testRetrieveBinaryClaims() throws Exception {
    LdapClaimsHandler claimsHandler = (LdapClaimsHandler) appContext.getBean("testClaimsHandler");
    ClaimsManager claimsManager = new ClaimsManager();
    claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
    String user = props.getProperty("binaryClaimUser");
    Assert.assertNotNull(user, "Property 'binaryClaimUser' not configured");
    ClaimCollection requestedClaims = createRequestClaimCollection();
    // Ask for the (binary) cert as well
    Claim claim = new Claim();
    claim.setClaimType(URI.create("http://custom/x509"));
    claim.setOptional(true);
    requestedClaims.add(claim);
    List<URI> expectedClaims = new ArrayList<>();
    expectedClaims.add(ClaimTypes.FIRSTNAME);
    expectedClaims.add(ClaimTypes.LASTNAME);
    expectedClaims.add(ClaimTypes.EMAILADDRESS);
    expectedClaims.add(URI.create("http://custom/x509"));
    ClaimsParameters params = new ClaimsParameters();
    params.setPrincipal(new CustomTokenPrincipal(user));
    ProcessedClaimCollection retrievedClaims = claimsManager.retrieveClaimValues(requestedClaims, params);
    Assert.assertTrue("Retrieved number of claims [" + retrievedClaims.size() + "] doesn't match with expected [" + expectedClaims.size() + "]", retrievedClaims.size() == expectedClaims.size());
    boolean foundCert = false;
    for (ProcessedClaim c : retrievedClaims) {
        if (URI.create("http://custom/x509").equals(c.getClaimType())) {
            foundCert = true;
            Assert.assertTrue(c.getValues().get(0) instanceof byte[]);
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            InputStream in = new ByteArrayInputStream((byte[]) c.getValues().get(0));
            X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);
            Assert.assertTrue(cert != null);
        }
    }
    Assert.assertTrue(foundCert);
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) LdapClaimsHandler(org.apache.cxf.sts.claims.LdapClaimsHandler) URI(java.net.URI) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ClaimsParameters(org.apache.cxf.sts.claims.ClaimsParameters) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) ByteArrayInputStream(java.io.ByteArrayInputStream) ClaimsManager(org.apache.cxf.sts.claims.ClaimsManager) 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)

Aggregations

LdapClaimsHandler (org.apache.cxf.sts.claims.LdapClaimsHandler)7 URI (java.net.URI)6 ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)6 ClaimsManager (org.apache.cxf.sts.claims.ClaimsManager)6 ClaimsParameters (org.apache.cxf.sts.claims.ClaimsParameters)6 ProcessedClaim (org.apache.cxf.sts.claims.ProcessedClaim)6 ProcessedClaimCollection (org.apache.cxf.sts.claims.ProcessedClaimCollection)6 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)6 ArrayList (java.util.ArrayList)5 Claim (org.apache.cxf.rt.security.claims.Claim)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1