use of org.apache.cxf.rt.security.claims.ClaimCollection in project cas by apereo.
the class WrappingSecurityTokenServiceClaimsHandlerTests method verifyClaimNoPrincipal.
@Test
public void verifyClaimNoPrincipal() {
val claims = new ClaimCollection();
val claim = new Claim();
claim.setClaimType(WSFederationClaims.COMMON_NAME.getUri());
claims.add(claim);
val parameters = new ClaimsParameters();
parameters.setRealm("CAS");
val handler = new WrappingSecurityTokenServiceClaimsHandler("CAS", "https://apereo.org/cas");
assertTrue(handler.retrieveClaimValues(claims, parameters).isEmpty());
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cas by apereo.
the class WrappingSecurityTokenServiceClaimsHandlerTests method verifyClaims.
@Test
public void verifyClaims() {
val claims = new ClaimCollection();
val claim = new Claim();
claim.setClaimType(WSFederationClaims.COMMON_NAME.getUri());
claims.add(claim);
val parameters = new ClaimsParameters();
parameters.setRealm("CAS");
parameters.setPrincipal(mock(Principal.class));
val handler = new WrappingSecurityTokenServiceClaimsHandler("CAS", "https://apereo.org/cas");
assertFalse(handler.retrieveClaimValues(claims, parameters).isEmpty());
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class SAMLClaimsTest method testSAML2MultipleRoles.
@org.junit.Test
public void testSAML2MultipleRoles() throws Exception {
AttributeBean attributeBean = new AttributeBean();
attributeBean.setQualifiedName(SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT);
attributeBean.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
attributeBean.addAttributeValue("employee");
attributeBean.addAttributeValue("boss");
SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler();
samlCallbackHandler.setAttributes(Collections.singletonList(attributeBean));
// Create the SAML Assertion via the CallbackHandler
SAMLCallback samlCallback = new SAMLCallback();
SAMLUtil.doSAMLCallback(samlCallbackHandler, samlCallback);
SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
Document doc = DOMUtils.newDocument();
samlAssertion.toDOM(doc);
ClaimCollection claims = SAMLUtils.getClaims(samlAssertion);
assertEquals(claims.getDialect().toString(), "http://schemas.xmlsoap.org/ws/2005/05/identity");
assertEquals(1, claims.size());
// Check Claim values
Claim claim = claims.get(0);
assertEquals(claim.getClaimType(), SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT);
assertEquals(2, claim.getValues().size());
assertTrue(claim.getValues().contains("employee"));
assertTrue(claim.getValues().contains("boss"));
// Check SAMLClaim values
assertTrue(claim instanceof SAMLClaim);
assertEquals(SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT, ((SAMLClaim) claim).getName());
assertEquals(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED, ((SAMLClaim) claim).getNameFormat());
// Check roles
Set<Principal> roles = SAMLUtils.parseRolesFromClaims(claims, SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT, SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
assertEquals(2, roles.size());
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class ClaimsAuthorizingInterceptorTest method prepareMessage.
private Message prepareMessage(Class<?> cls, String methodName, String roleName, org.apache.cxf.rt.security.claims.Claim... claim) throws Exception {
ClaimCollection claims = new ClaimCollection();
Collections.addAll(claims, claim);
Set<Principal> roles = parseRolesFromClaims(claims, roleName, "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
ClaimsSecurityContext sc = new ClaimsSecurityContext() {
private Principal p = new SimplePrincipal("user");
@Override
public Principal getUserPrincipal() {
return p;
}
@Override
public boolean isUserInRole(String role) {
if (roles == null) {
return false;
}
for (Principal principalRole : roles) {
if (principalRole != p && principalRole.getName().equals(role)) {
return true;
}
}
return false;
}
@Override
public Subject getSubject() {
return null;
}
@Override
public Set<Principal> getUserRoles() {
return roles;
}
@Override
public ClaimCollection getClaims() {
return claims;
}
};
Message m = new MessageImpl();
m.setExchange(new ExchangeImpl());
m.put(SecurityContext.class, sc);
m.put("org.apache.cxf.resource.method", cls.getMethod(methodName, new Class[] {}));
return m;
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class SAMLClaimsTest method testSAML2MultipleClaims.
@org.junit.Test
public void testSAML2MultipleClaims() throws Exception {
AttributeBean attributeBean = new AttributeBean();
attributeBean.setQualifiedName(SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT);
attributeBean.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
attributeBean.addAttributeValue("employee");
AttributeBean attributeBean2 = new AttributeBean();
attributeBean2.setQualifiedName("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname");
attributeBean2.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
attributeBean2.addAttributeValue("smith");
SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler();
List<AttributeBean> attributes = new ArrayList<>();
attributes.add(attributeBean);
attributes.add(attributeBean2);
samlCallbackHandler.setAttributes(attributes);
// Create the SAML Assertion via the CallbackHandler
SAMLCallback samlCallback = new SAMLCallback();
SAMLUtil.doSAMLCallback(samlCallbackHandler, samlCallback);
SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
Document doc = DOMUtils.newDocument();
samlAssertion.toDOM(doc);
ClaimCollection claims = SAMLUtils.getClaims(samlAssertion);
assertEquals(claims.getDialect().toString(), "http://schemas.xmlsoap.org/ws/2005/05/identity");
assertEquals(2, claims.size());
// Check roles
Set<Principal> roles = SAMLUtils.parseRolesFromClaims(claims, SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT, SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
assertEquals(1, roles.size());
Principal p = roles.iterator().next();
assertEquals("employee", p.getName());
}
Aggregations