use of org.apache.cxf.rt.security.saml.claims.SAMLClaim in project cxf by apache.
the class ClaimsAuthorizingInterceptor method authorize.
protected boolean authorize(SAMLSecurityContext sc, Method method) {
List<ClaimBean> list = claims.get(method.getName());
org.apache.cxf.rt.security.claims.ClaimCollection actualClaims = sc.getClaims();
for (ClaimBean claimBean : list) {
org.apache.cxf.rt.security.claims.Claim claim = claimBean.getClaim();
org.apache.cxf.rt.security.claims.Claim matchingClaim = null;
for (org.apache.cxf.rt.security.claims.Claim cl : actualClaims) {
if (cl instanceof SAMLClaim && ((SAMLClaim) cl).getName().equals(((SAMLClaim) claim).getName()) && ((SAMLClaim) cl).getNameFormat().equals(((SAMLClaim) claim).getNameFormat())) {
matchingClaim = cl;
break;
}
}
if (matchingClaim == null) {
if (claimBean.getClaimMode() == ClaimMode.STRICT) {
return false;
}
continue;
}
List<Object> claimValues = claim.getValues();
List<Object> matchingClaimValues = matchingClaim.getValues();
if (claimBean.isMatchAll() && !matchingClaimValues.containsAll(claimValues)) {
return false;
}
boolean matched = false;
for (Object value : matchingClaimValues) {
if (claimValues.contains(value)) {
matched = true;
break;
}
}
if (!matched) {
return false;
}
}
return true;
}
use of org.apache.cxf.rt.security.saml.claims.SAMLClaim in project cxf by apache.
the class ClaimsAuthorizingInterceptor method getClaims.
private List<ClaimBean> getClaims(Claims claimsAnn, Claim claimAnn) {
List<ClaimBean> claimsList = new ArrayList<>();
List<Claim> annClaims = new ArrayList<>();
if (claimsAnn != null) {
annClaims.addAll(Arrays.asList(claimsAnn.value()));
} else if (claimAnn != null) {
annClaims.add(claimAnn);
}
for (Claim ann : annClaims) {
SAMLClaim claim = new SAMLClaim();
String claimName = ann.name();
if (nameAliases.containsKey(claimName)) {
claimName = nameAliases.get(claimName);
}
String claimFormat = ann.format();
if (formatAliases.containsKey(claimFormat)) {
claimFormat = formatAliases.get(claimFormat);
}
claim.setName(claimName);
claim.setNameFormat(claimFormat);
for (String value : ann.value()) {
claim.addValue(value);
}
claimsList.add(new ClaimBean(claim, ann.mode(), ann.matchAll()));
}
return claimsList;
}
use of org.apache.cxf.rt.security.saml.claims.SAMLClaim in project cxf by apache.
the class SAMLUtils method parseRolesFromClaims.
/**
* Extract roles from the given Claims
*/
public static Set<Principal> parseRolesFromClaims(ClaimCollection claims, String name, String nameFormat) {
String roleAttributeName = name;
if (roleAttributeName == null) {
roleAttributeName = SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT;
}
Set<Principal> roles = new HashSet<>();
for (Claim claim : claims) {
if (claim instanceof SAMLClaim && ((SAMLClaim) claim).getName().equals(name) && (nameFormat == null || nameFormat.equals(((SAMLClaim) claim).getNameFormat()))) {
for (Object claimValue : claim.getValues()) {
if (claimValue instanceof String) {
roles.add(new SimpleGroup((String) claimValue));
}
}
if (claim.getValues().size() > 1) {
// Don't search for other attributes with the same name if > 1 claim value
break;
}
}
}
return roles;
}
use of org.apache.cxf.rt.security.saml.claims.SAMLClaim in project cxf by apache.
the class SAMLUtils method getClaims.
/**
* Extract Claims from a SAML Assertion
*/
public static ClaimCollection getClaims(SamlAssertionWrapper assertion) {
ClaimCollection claims = new ClaimCollection();
if (assertion.getSamlVersion().equals(SAMLVersion.VERSION_20)) {
List<AttributeStatement> statements = assertion.getSaml2().getAttributeStatements();
for (AttributeStatement as : statements) {
for (Attribute atr : as.getAttributes()) {
SAMLClaim claim = new SAMLClaim();
claim.setClaimType(URI.create(atr.getName()));
claim.setName(atr.getName());
claim.setNameFormat(atr.getNameFormat());
claim.setFriendlyName(atr.getFriendlyName());
for (XMLObject o : atr.getAttributeValues()) {
String attrValue = o.getDOM().getTextContent();
claim.getValues().add(attrValue);
}
claims.add(claim);
}
}
} else {
List<org.opensaml.saml.saml1.core.AttributeStatement> attributeStatements = assertion.getSaml1().getAttributeStatements();
for (org.opensaml.saml.saml1.core.AttributeStatement statement : attributeStatements) {
for (org.opensaml.saml.saml1.core.Attribute atr : statement.getAttributes()) {
SAMLClaim claim = new SAMLClaim();
String claimType = atr.getAttributeName();
if (atr.getAttributeNamespace() != null) {
claimType = atr.getAttributeNamespace() + "/" + claimType;
}
claim.setClaimType(URI.create(claimType));
claim.setName(atr.getAttributeName());
claim.setNameFormat(atr.getAttributeNamespace());
for (XMLObject o : atr.getAttributeValues()) {
String attrValue = o.getDOM().getTextContent();
claim.getValues().add(attrValue);
}
claims.add(claim);
}
}
}
return claims;
}
use of org.apache.cxf.rt.security.saml.claims.SAMLClaim in project cxf by apache.
the class ClaimsAuthorizingInterceptorTest method createClaim.
private org.apache.cxf.rt.security.claims.Claim createClaim(String name, String format, Object... values) {
SAMLClaim claim = new SAMLClaim();
claim.setName(name);
claim.setNameFormat(format);
claim.setValues(Arrays.asList(values));
return claim;
}
Aggregations