Search in sources :

Example 1 with SCTValidator

use of org.apache.cxf.sts.token.validator.SCTValidator in project cxf by apache.

the class RequestParserUnitTest method testValidateSCT.

/**
 * Test for fetching (and validating) a referenced SecurityContextToken.
 */
@org.junit.Test
public void testValidateSCT() throws Exception {
    Element secHeaderElement = (Element) parseStringToElement(SECURITY_HEADER).getFirstChild();
    RequestSecurityTokenType request = createJaxbObject(VALIDATE_SCT_REFERENCE);
    RequestParser parser = new RequestParser();
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgContext = new WrappedMessageContext(msg);
    // Process the security header and store the results in the message context
    WSSecurityEngine securityEngine = new WSSecurityEngine();
    RequestData reqData = new RequestData();
    reqData.setCallbackHandler(new PasswordCallbackHandler());
    WSHandlerResult results = securityEngine.processSecurityHeader(secHeaderElement, reqData);
    msgContext.put(WSHandlerConstants.RECV_RESULTS, Collections.singletonList(results));
    RequestRequirements requestRequirements = parser.parseRequest(request, msgContext, null, null);
    SCTValidator sctValidator = new SCTValidator();
    assertTrue(sctValidator.canHandleToken(requestRequirements.getTokenRequirements().getValidateTarget()));
}
Also used : RequestData(org.apache.wss4j.dom.handler.RequestData) SCTValidator(org.apache.cxf.sts.token.validator.SCTValidator) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) WSSecurityEngine(org.apache.wss4j.dom.engine.WSSecurityEngine) MessageImpl(org.apache.cxf.message.MessageImpl) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult)

Example 2 with SCTValidator

use of org.apache.cxf.sts.token.validator.SCTValidator in project cxf by apache.

the class ValidateSCTUnitTest method testValidateSCT.

/**
 * Test to successfully validate a SecurityContextToken
 */
@org.junit.Test
public void testValidateSCT() throws Exception {
    TokenValidateOperation validateOperation = new TokenValidateOperation();
    validateOperation.setTokenStore(tokenStore);
    // Add Token Validator
    validateOperation.setTokenValidators(Collections.singletonList(new SCTValidator()));
    // Add STSProperties object
    STSPropertiesMBean stsProperties = new StaticSTSProperties();
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    stsProperties.setEncryptionCrypto(crypto);
    stsProperties.setSignatureCrypto(crypto);
    stsProperties.setEncryptionUsername("myservicekey");
    stsProperties.setSignatureUsername("mystskey");
    stsProperties.setCallbackHandler(new PasswordCallbackHandler());
    stsProperties.setIssuer("STS");
    validateOperation.setStsProperties(stsProperties);
    // Mock up a request
    RequestSecurityTokenType request = new RequestSecurityTokenType();
    JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.STATUS);
    request.getAny().add(tokenType);
    // Get a SecurityContextToken via the SCTProvider
    TokenProviderResponse providerResponse = createSCT();
    Element sct = (Element) providerResponse.getToken();
    ValidateTargetType validateTarget = new ValidateTargetType();
    validateTarget.setAny(sct);
    JAXBElement<ValidateTargetType> validateTargetType = new JAXBElement<ValidateTargetType>(QNameConstants.VALIDATE_TARGET, ValidateTargetType.class, validateTarget);
    request.getAny().add(validateTargetType);
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
    Principal principal = new CustomTokenPrincipal("alice");
    msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
    // Validate a token
    RequestSecurityTokenResponseType response = validateOperation.validate(request, principal, msgCtx);
    assertTrue(validateResponse(response));
    // Now remove the token from the cache before validating again
    tokenStore.remove(tokenStore.getToken(providerResponse.getTokenId()).getId());
    assertNull(tokenStore.getToken(providerResponse.getTokenId()));
    response = validateOperation.validate(request, principal, msgCtx);
    assertFalse(validateResponse(response));
}
Also used : 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) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) JAXBElement(javax.xml.bind.JAXBElement) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Crypto(org.apache.wss4j.common.crypto.Crypto) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) SCTValidator(org.apache.cxf.sts.token.validator.SCTValidator) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SecurityContext(org.apache.cxf.security.SecurityContext) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) TokenProviderResponse(org.apache.cxf.sts.token.provider.TokenProviderResponse) ValidateTargetType(org.apache.cxf.ws.security.sts.provider.model.ValidateTargetType) MessageImpl(org.apache.cxf.message.MessageImpl) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Principal(java.security.Principal)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)2 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)2 MessageImpl (org.apache.cxf.message.MessageImpl)2 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)2 SCTValidator (org.apache.cxf.sts.token.validator.SCTValidator)2 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)2 Element (org.w3c.dom.Element)2 Principal (java.security.Principal)1 SecurityContext (org.apache.cxf.security.SecurityContext)1 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)1 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)1 TokenProviderResponse (org.apache.cxf.sts.token.provider.TokenProviderResponse)1 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)1 ValidateTargetType (org.apache.cxf.ws.security.sts.provider.model.ValidateTargetType)1 Crypto (org.apache.wss4j.common.crypto.Crypto)1 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)1 WSSecurityEngine (org.apache.wss4j.dom.engine.WSSecurityEngine)1 RequestData (org.apache.wss4j.dom.handler.RequestData)1 WSHandlerResult (org.apache.wss4j.dom.handler.WSHandlerResult)1