use of org.apache.cxf.sts.claims.ProcessedClaimCollection in project cxf by apache.
the class CustomAttributeStatementProvider method getStatement.
public AttributeStatementBean getStatement(TokenProviderParameters providerParameters) {
// Handle Claims
ClaimsManager claimsManager = providerParameters.getClaimsManager();
ProcessedClaimCollection retrievedClaims = new ProcessedClaimCollection();
if (claimsManager != null) {
ClaimsParameters params = new ClaimsParameters();
params.setAdditionalProperties(providerParameters.getAdditionalProperties());
params.setAppliesToAddress(providerParameters.getAppliesToAddress());
params.setEncryptionProperties(providerParameters.getEncryptionProperties());
params.setKeyRequirements(providerParameters.getKeyRequirements());
params.setPrincipal(providerParameters.getPrincipal());
params.setRealm(providerParameters.getRealm());
params.setStsProperties(providerParameters.getStsProperties());
params.setTokenRequirements(providerParameters.getTokenRequirements());
params.setTokenStore(providerParameters.getTokenStore());
params.setMessageContext(providerParameters.getMessageContext());
retrievedClaims = claimsManager.retrieveClaimValues(providerParameters.getRequestedPrimaryClaims(), providerParameters.getRequestedSecondaryClaims(), params);
}
if (retrievedClaims == null) {
return null;
}
Iterator<ProcessedClaim> claimIterator = retrievedClaims.iterator();
if (!claimIterator.hasNext()) {
return null;
}
List<AttributeBean> attributeList = new ArrayList<>();
String tokenType = providerParameters.getTokenRequirements().getTokenType();
AttributeStatementBean attrBean = new AttributeStatementBean();
while (claimIterator.hasNext()) {
ProcessedClaim claim = claimIterator.next();
AttributeBean attributeBean = new AttributeBean();
URI claimType = claim.getClaimType();
if (WSS4JConstants.WSS_SAML2_TOKEN_TYPE.equals(tokenType) || WSS4JConstants.SAML2_NS.equals(tokenType)) {
attributeBean.setQualifiedName(claimType.toString());
attributeBean.setNameFormat(nameFormat);
} else {
String uri = claimType.toString();
int lastSlash = uri.lastIndexOf("/");
if (lastSlash == (uri.length() - 1)) {
uri = uri.substring(0, lastSlash);
lastSlash = uri.lastIndexOf("/");
}
String namespace = uri.substring(0, lastSlash);
String name = uri.substring(lastSlash + 1, uri.length());
attributeBean.setSimpleName(name);
attributeBean.setQualifiedName(namespace);
}
attributeBean.setAttributeValues(claim.getValues());
attributeList.add(attributeBean);
}
attrBean.setSamlAttributes(attributeList);
return attrBean;
}
use of org.apache.cxf.sts.claims.ProcessedClaimCollection in project cxf by apache.
the class LDAPClaimsTest method testRetrieveRolesForBob.
@org.junit.Test
public void testRetrieveRolesForBob() throws Exception {
LdapGroupClaimsHandler claimsHandler = (LdapGroupClaimsHandler) appContext.getBean("testGroupClaimsHandlerOtherUsers");
ClaimsManager claimsManager = new ClaimsManager();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
String user = props.getProperty("otherClaimUser");
Assert.assertNotNull(user, "Property 'claimUser' not configured");
ClaimCollection requestedClaims = new ClaimCollection();
Claim claim = new Claim();
URI roleURI = URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
claim.setClaimType(roleURI);
requestedClaims.add(claim);
ClaimsParameters params = new ClaimsParameters();
params.setPrincipal(new CustomTokenPrincipal(user));
ProcessedClaimCollection retrievedClaims = claimsManager.retrieveClaimValues(requestedClaims, params);
Assert.assertTrue(retrievedClaims.size() == 1);
Assert.assertTrue(retrievedClaims.get(0).getClaimType().equals(roleURI));
Assert.assertTrue(retrievedClaims.get(0).getValues().size() == 2);
}
use of org.apache.cxf.sts.claims.ProcessedClaimCollection 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);
}
Aggregations