use of org.apache.cxf.rt.security.claims.ClaimCollection 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<String> expectedClaims = new ArrayList<>();
expectedClaims.add(ClaimTypes.FIRSTNAME.toString());
expectedClaims.add(ClaimTypes.LASTNAME.toString());
expectedClaims.add(ClaimTypes.EMAILADDRESS.toString());
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);
}
}
}
use of org.apache.cxf.rt.security.claims.ClaimCollection 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("http://custom/x509");
claim.setOptional(true);
requestedClaims.add(claim);
List<String> expectedClaims = new ArrayList<>();
expectedClaims.add(ClaimTypes.FIRSTNAME.toString());
expectedClaims.add(ClaimTypes.LASTNAME.toString());
expectedClaims.add(ClaimTypes.EMAILADDRESS.toString());
expectedClaims.add("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 ("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.assertNotNull(cert);
}
}
Assert.assertTrue(foundCert);
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class SecurityContextProviderImpl method getSecurityContext.
public SecurityContext getSecurityContext(Message message, SamlAssertionWrapper wrapper) {
// First check to see if we are allowed to set up a security context
// The SAML Assertion must be signed, or we must explicitly allow unsigned
String allowUnsigned = (String) SecurityUtils.getSecurityPropertyValue(SecurityConstants.ENABLE_UNSIGNED_SAML_ASSERTION_PRINCIPAL, message);
boolean allowUnsignedSamlPrincipals = Boolean.parseBoolean(allowUnsigned);
if (!(wrapper.isSigned() || allowUnsignedSamlPrincipals)) {
return null;
}
ClaimCollection claims = getClaims(wrapper);
Subject subject = getSubject(message, wrapper, claims);
SecurityContext securityContext = doGetSecurityContext(message, subject, claims);
if (securityContext instanceof SAMLSecurityContext) {
Element assertionElement = wrapper.getElement();
((SAMLSecurityContext) securityContext).setAssertionElement(assertionElement);
}
return securityContext;
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class AbstractServiceProviderFilter method setSecurityContext.
protected void setSecurityContext(Message m, SamlAssertionWrapper assertionWrapper) {
Subject subject = SAMLUtils.getSubject(m, assertionWrapper);
final String name = subject.getName();
if (name != null) {
String roleAttributeName = (String) SecurityUtils.getSecurityPropertyValue(SecurityConstants.SAML_ROLE_ATTRIBUTENAME, m);
if (roleAttributeName == null || roleAttributeName.length() == 0) {
roleAttributeName = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role";
}
ClaimCollection claims = org.apache.cxf.rt.security.saml.utils.SAMLUtils.getClaims(assertionWrapper);
Set<Principal> roles = org.apache.cxf.rt.security.saml.utils.SAMLUtils.parseRolesFromClaims(claims, roleAttributeName, null);
SAMLSecurityContext context = new SAMLSecurityContext(new SimplePrincipal(name), roles, claims);
context.setIssuer(org.apache.cxf.rt.security.saml.utils.SAMLUtils.getIssuer(assertionWrapper));
context.setAssertionElement(org.apache.cxf.rt.security.saml.utils.SAMLUtils.getAssertionElement(assertionWrapper));
m.put(SecurityContext.class, context);
}
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class RealmSupportTest method createClaimCollection.
private ClaimCollection createClaimCollection() {
ClaimCollection requestedClaims = new ClaimCollection();
Claim requestClaimA = new Claim();
requestClaimA.setClaimType(URI.create("Claim-A"));
requestClaimA.setOptional(false);
requestedClaims.add(requestClaimA);
Claim requestClaimB = new Claim();
requestClaimB.setClaimType(URI.create("Claim-B"));
requestClaimB.setOptional(false);
requestedClaims.add(requestClaimB);
Claim requestClaimC = new Claim();
requestClaimC.setClaimType(URI.create("Claim-C"));
requestClaimC.setOptional(true);
requestedClaims.add(requestClaimC);
return requestedClaims;
}
Aggregations