Search in sources :

Example 1 with CustomClaimParser

use of org.apache.cxf.sts.common.CustomClaimParser in project cxf by apache.

the class IssueSamlClaimsUnitTest method testCustomClaimDialect.

/**
 * Test custom claim parser and handler.
 */
@org.junit.Test
public void testCustomClaimDialect() throws Exception {
    TokenIssueOperation issueOperation = new TokenIssueOperation();
    // Add Token Provider
    addTokenProvider(issueOperation);
    // Add Service
    addService(issueOperation);
    // Add STSProperties object
    addSTSProperties(issueOperation);
    // Set the ClaimsManager
    ClaimsManager claimsManager = new ClaimsManager();
    ClaimsHandler claimsHandler = new CustomClaimsHandler();
    ClaimsParser claimsParser = new CustomClaimParser();
    claimsManager.setClaimParsers(Collections.singletonList(claimsParser));
    claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
    issueOperation.setClaimsManager(claimsManager);
    // Mock up a request
    RequestSecurityTokenType request = new RequestSecurityTokenType();
    JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, WSS4JConstants.WSS_SAML2_TOKEN_TYPE);
    request.getAny().add(tokenType);
    Element secondaryParameters = createCustomSecondaryParameters();
    request.getAny().add(secondaryParameters);
    request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy"));
    // Mock up message context
    Map<String, Object> msgCtx = setupMessageContext();
    // Issue a token
    List<RequestSecurityTokenResponseType> securityTokenResponse = issueToken(issueOperation, request, new CustomTokenPrincipal("alice"), msgCtx);
    // Test the generated token.
    Element assertion = null;
    for (Object tokenObject : securityTokenResponse.get(0).getAny()) {
        if (tokenObject instanceof JAXBElement<?> && REQUESTED_SECURITY_TOKEN.equals(((JAXBElement<?>) tokenObject).getName())) {
            RequestedSecurityTokenType rstType = (RequestedSecurityTokenType) ((JAXBElement<?>) tokenObject).getValue();
            assertion = (Element) rstType.getAny();
            break;
        }
    }
    assertNotNull(assertion);
    String tokenString = DOM2Writer.nodeToString(assertion);
    assertTrue(tokenString.contains("AttributeStatement"));
    assertTrue(tokenString.contains("bob@custom"));
}
Also used : ClaimsHandler(org.apache.cxf.sts.claims.ClaimsHandler) CustomClaimsHandler(org.apache.cxf.sts.common.CustomClaimsHandler) RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) JAXBElement(javax.xml.bind.JAXBElement) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) CustomClaimsHandler(org.apache.cxf.sts.common.CustomClaimsHandler) ClaimsParser(org.apache.cxf.sts.claims.ClaimsParser) CustomClaimParser(org.apache.cxf.sts.common.CustomClaimParser) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) ClaimsManager(org.apache.cxf.sts.claims.ClaimsManager)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)1 ClaimsHandler (org.apache.cxf.sts.claims.ClaimsHandler)1 ClaimsManager (org.apache.cxf.sts.claims.ClaimsManager)1 ClaimsParser (org.apache.cxf.sts.claims.ClaimsParser)1 CustomClaimParser (org.apache.cxf.sts.common.CustomClaimParser)1 CustomClaimsHandler (org.apache.cxf.sts.common.CustomClaimsHandler)1 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)1 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)1 RequestedSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType)1 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)1 Element (org.w3c.dom.Element)1