Search in sources :

Example 1 with ClaimBean

use of org.apache.cxf.rt.security.saml.claims.ClaimBean 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;
}
Also used : SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) ClaimBean(org.apache.cxf.rt.security.saml.claims.ClaimBean)

Example 2 with ClaimBean

use of org.apache.cxf.rt.security.saml.claims.ClaimBean 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;
}
Also used : SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) ArrayList(java.util.ArrayList) ClaimBean(org.apache.cxf.rt.security.saml.claims.ClaimBean) Claim(org.apache.cxf.security.claims.authorization.Claim) SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim)

Example 3 with ClaimBean

use of org.apache.cxf.rt.security.saml.claims.ClaimBean in project cxf by apache.

the class ClaimsAuthorizingInterceptorTest method testUserInRoleAndClaims.

@Test
public void testUserInRoleAndClaims() throws Exception {
    SecureAnnotationsInterceptor in = new SecureAnnotationsInterceptor();
    in.setAnnotationClassName(SecureRole.class.getName());
    in.setSecuredObject(new TestService2());
    Message m = prepareMessage(TestService2.class, "test", createDefaultClaim("admin"), createClaim("a", "b", "c"));
    in.handleMessage(m);
    ClaimsAuthorizingInterceptor in2 = new ClaimsAuthorizingInterceptor();
    org.apache.cxf.rt.security.saml.claims.SAMLClaim claim = new org.apache.cxf.rt.security.saml.claims.SAMLClaim();
    claim.setNameFormat("a");
    claim.setName("b");
    claim.addValue("c");
    in2.setClaims(Collections.singletonMap("test", Collections.singletonList(new ClaimBean(claim))));
    in2.handleMessage(m);
    try {
        in.handleMessage(prepareMessage(TestService2.class, "test", createDefaultClaim("user")));
        fail("AccessDeniedException expected");
    } catch (AccessDeniedException ex) {
    // expected
    }
}
Also used : SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) Assert(org.junit.Assert) AccessDeniedException(org.apache.cxf.interceptor.security.AccessDeniedException) Message(org.apache.cxf.message.Message) SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim) ClaimBean(org.apache.cxf.rt.security.saml.claims.ClaimBean) SecureAnnotationsInterceptor(org.apache.cxf.interceptor.security.SecureAnnotationsInterceptor) Test(org.junit.Test)

Example 4 with ClaimBean

use of org.apache.cxf.rt.security.saml.claims.ClaimBean in project cxf by apache.

the class ClaimsAuthorizingInterceptor method findClaims.

protected void findClaims(Class<?> cls) {
    if (cls == null || cls == Object.class) {
        return;
    }
    List<ClaimBean> clsClaims = getClaims(cls.getAnnotation(Claims.class), cls.getAnnotation(Claim.class));
    for (Method m : cls.getMethods()) {
        if (SKIP_METHODS.contains(m.getName())) {
            continue;
        }
        List<ClaimBean> methodClaims = getClaims(m.getAnnotation(Claims.class), m.getAnnotation(Claim.class));
        List<ClaimBean> allClaims = new ArrayList<>(methodClaims);
        for (ClaimBean bean : clsClaims) {
            if (isClaimOverridden(bean, methodClaims)) {
                continue;
            }
            allClaims.add(bean);
        }
        claims.put(m.getName(), allClaims);
    }
    if (!claims.isEmpty()) {
        return;
    }
    findClaims(cls.getSuperclass());
    if (!claims.isEmpty()) {
        return;
    }
    for (Class<?> interfaceCls : cls.getInterfaces()) {
        findClaims(interfaceCls);
    }
}
Also used : Claims(org.apache.cxf.security.claims.authorization.Claims) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ClaimBean(org.apache.cxf.rt.security.saml.claims.ClaimBean) Claim(org.apache.cxf.security.claims.authorization.Claim) SAMLClaim(org.apache.cxf.rt.security.saml.claims.SAMLClaim)

Aggregations

ClaimBean (org.apache.cxf.rt.security.saml.claims.ClaimBean)4 SAMLClaim (org.apache.cxf.rt.security.saml.claims.SAMLClaim)4 ArrayList (java.util.ArrayList)2 Claim (org.apache.cxf.security.claims.authorization.Claim)2 Method (java.lang.reflect.Method)1 AccessDeniedException (org.apache.cxf.interceptor.security.AccessDeniedException)1 SecureAnnotationsInterceptor (org.apache.cxf.interceptor.security.SecureAnnotationsInterceptor)1 Message (org.apache.cxf.message.Message)1 Claims (org.apache.cxf.security.claims.authorization.Claims)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1