Search in sources :

Example 1 with StaticEndpointClaimsHandler

use of org.apache.cxf.sts.claims.StaticEndpointClaimsHandler in project cxf by apache.

the class SAMLClaimsTest method testSaml2StaticEndpointClaimsUnknownAppliesTo.

/**
 * Test the creation of a SAML2 Assertion with StaticEndpointClaimsHandler
 * but unknown AppliesTo value
 */
@org.junit.Test
public void testSaml2StaticEndpointClaimsUnknownAppliesTo() throws Exception {
    TokenProvider samlTokenProvider = new SAMLTokenProvider();
    TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, APPLICATION_APPLIES_TO + "UNKNOWN");
    ClaimsManager claimsManager = new ClaimsManager();
    StaticEndpointClaimsHandler claimsHandler = new StaticEndpointClaimsHandler();
    // Create claims map for specific application
    Map<String, String> endpointClaimsMap = new HashMap<>();
    endpointClaimsMap.put(CLAIM_APPLICATION.toString(), CLAIM_APPLICATION_VALUE);
    Map<String, Map<String, String>> staticClaims = new HashMap<>();
    staticClaims.put(APPLICATION_APPLIES_TO, endpointClaimsMap);
    claimsHandler.setEndpointClaims(staticClaims);
    List<URI> supportedClaims = new ArrayList<>();
    supportedClaims.add(CLAIM_APPLICATION);
    claimsHandler.setSupportedClaims(supportedClaims);
    claimsManager.setClaimHandlers(Collections.singletonList((ClaimsHandler) claimsHandler));
    providerParameters.setClaimsManager(claimsManager);
    ClaimCollection claims = new ClaimCollection();
    Claim claim = new Claim();
    claim.setClaimType(CLAIM_APPLICATION);
    claims.add(claim);
    providerParameters.setRequestedPrimaryClaims(claims);
    assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
    try {
        samlTokenProvider.createToken(providerParameters);
        fail("Failure expected as the claim for the application can't be found due to unknown AppliesTo");
    } catch (Exception ex) {
    // expected on the wrong attribute provider
    }
}
Also used : StaticEndpointClaimsHandler(org.apache.cxf.sts.claims.StaticEndpointClaimsHandler) ClaimsHandler(org.apache.cxf.sts.claims.ClaimsHandler) StaticClaimsHandler(org.apache.cxf.sts.claims.StaticClaimsHandler) CustomClaimsHandler(org.apache.cxf.sts.common.CustomClaimsHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) ClaimsManager(org.apache.cxf.sts.claims.ClaimsManager) StaticEndpointClaimsHandler(org.apache.cxf.sts.claims.StaticEndpointClaimsHandler) HashMap(java.util.HashMap) Map(java.util.Map) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) Claim(org.apache.cxf.rt.security.claims.Claim)

Example 2 with StaticEndpointClaimsHandler

use of org.apache.cxf.sts.claims.StaticEndpointClaimsHandler in project cxf by apache.

the class SAMLClaimsTest method testSaml2StaticEndpointClaims.

/**
 * Test the creation of a SAML2 Assertion with StaticEndpointClaimsHandler
 */
@org.junit.Test
public void testSaml2StaticEndpointClaims() throws Exception {
    TokenProvider samlTokenProvider = new SAMLTokenProvider();
    TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, null);
    ClaimsManager claimsManager = new ClaimsManager();
    StaticEndpointClaimsHandler claimsHandler = new StaticEndpointClaimsHandler();
    // Create claims map for specific application
    Map<String, String> endpointClaimsMap = new HashMap<>();
    endpointClaimsMap.put(CLAIM_APPLICATION.toString(), CLAIM_APPLICATION_VALUE);
    Map<String, Map<String, String>> staticClaims = new HashMap<>();
    staticClaims.put(APPLICATION_APPLIES_TO, endpointClaimsMap);
    claimsHandler.setEndpointClaims(staticClaims);
    List<URI> supportedClaims = new ArrayList<>();
    supportedClaims.add(CLAIM_APPLICATION);
    claimsHandler.setSupportedClaims(supportedClaims);
    claimsManager.setClaimHandlers(Collections.singletonList((ClaimsHandler) claimsHandler));
    providerParameters.setClaimsManager(claimsManager);
    ClaimCollection claims = new ClaimCollection();
    Claim claim = new Claim();
    claim.setClaimType(CLAIM_APPLICATION);
    claims.add(claim);
    providerParameters.setRequestedPrimaryClaims(claims);
    assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
    TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
    assertTrue(providerResponse != null);
    assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
    Element token = (Element) providerResponse.getToken();
    String tokenString = DOM2Writer.nodeToString(token);
    assertTrue(tokenString.contains(providerResponse.getTokenId()));
    assertTrue(tokenString.contains("AttributeStatement"));
    assertTrue(tokenString.contains("alice"));
    assertTrue(tokenString.contains(SAML2Constants.CONF_BEARER));
    SamlAssertionWrapper assertion = new SamlAssertionWrapper(token);
    List<Attribute> attributes = assertion.getSaml2().getAttributeStatements().get(0).getAttributes();
    assertEquals(attributes.size(), 1);
    assertEquals(attributes.get(0).getName(), CLAIM_APPLICATION.toString());
    XMLObject valueObj = attributes.get(0).getAttributeValues().get(0);
    assertEquals(valueObj.getDOM().getTextContent(), CLAIM_APPLICATION_VALUE);
}
Also used : StaticEndpointClaimsHandler(org.apache.cxf.sts.claims.StaticEndpointClaimsHandler) ClaimsHandler(org.apache.cxf.sts.claims.ClaimsHandler) StaticClaimsHandler(org.apache.cxf.sts.claims.StaticClaimsHandler) CustomClaimsHandler(org.apache.cxf.sts.common.CustomClaimsHandler) HashMap(java.util.HashMap) Attribute(org.opensaml.saml.saml2.core.Attribute) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) XMLObject(org.opensaml.core.xml.XMLObject) URI(java.net.URI) ClaimsManager(org.apache.cxf.sts.claims.ClaimsManager) StaticEndpointClaimsHandler(org.apache.cxf.sts.claims.StaticEndpointClaimsHandler) HashMap(java.util.HashMap) Map(java.util.Map) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) Claim(org.apache.cxf.rt.security.claims.Claim)

Aggregations

URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Claim (org.apache.cxf.rt.security.claims.Claim)2 ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)2 ClaimsHandler (org.apache.cxf.sts.claims.ClaimsHandler)2 ClaimsManager (org.apache.cxf.sts.claims.ClaimsManager)2 StaticClaimsHandler (org.apache.cxf.sts.claims.StaticClaimsHandler)2 StaticEndpointClaimsHandler (org.apache.cxf.sts.claims.StaticEndpointClaimsHandler)2 CustomClaimsHandler (org.apache.cxf.sts.common.CustomClaimsHandler)2 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1 SamlAssertionWrapper (org.apache.wss4j.common.saml.SamlAssertionWrapper)1 XMLObject (org.opensaml.core.xml.XMLObject)1 Attribute (org.opensaml.saml.saml2.core.Attribute)1 Element (org.w3c.dom.Element)1