Search in sources :

Example 21 with ValidateTargetType

use of org.apache.cxf.ws.security.sts.provider.model.ValidateTargetType in project cxf by apache.

the class ValidateX509TokenUnitTest method testValidateInvalidX509Token.

/**
 * Test to validate an invalid X.509 token
 */
@org.junit.Test
public void testValidateInvalidX509Token() throws Exception {
    TokenValidateOperation validateOperation = new TokenValidateOperation();
    // Add Token Validator
    List<TokenValidator> validatorList = new ArrayList<>();
    validatorList.add(new X509TokenValidator());
    validateOperation.setTokenValidators(validatorList);
    // 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);
    // Create a BinarySecurityToken
    CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
    cryptoType.setAlias("eve");
    Crypto eveCrypto = CryptoFactory.getInstance(getEveCryptoProperties());
    X509Certificate[] certs = eveCrypto.getX509Certificates(cryptoType);
    assertTrue(certs != null && certs.length > 0);
    JAXBElement<BinarySecurityTokenType> binarySecurityTokenType = createBinarySecurityToken(certs[0]);
    ValidateTargetType validateTarget = new ValidateTargetType();
    validateTarget.setAny(binarySecurityTokenType);
    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);
    assertFalse(validateResponse(response));
}
Also used : RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) ArrayList(java.util.ArrayList) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) X509TokenValidator(org.apache.cxf.sts.token.validator.X509TokenValidator) TokenValidator(org.apache.cxf.sts.token.validator.TokenValidator) BinarySecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) JAXBElement(javax.xml.bind.JAXBElement) CryptoType(org.apache.wss4j.common.crypto.CryptoType) X509Certificate(java.security.cert.X509Certificate) X509TokenValidator(org.apache.cxf.sts.token.validator.X509TokenValidator) Crypto(org.apache.wss4j.common.crypto.Crypto) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SecurityContext(org.apache.cxf.security.SecurityContext) 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)21 ValidateTargetType (org.apache.cxf.ws.security.sts.provider.model.ValidateTargetType)21 ArrayList (java.util.ArrayList)20 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)20 MessageImpl (org.apache.cxf.message.MessageImpl)20 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)20 TokenValidator (org.apache.cxf.sts.token.validator.TokenValidator)20 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)20 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)20 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)18 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)16 Crypto (org.apache.wss4j.common.crypto.Crypto)16 Principal (java.security.Principal)14 SecurityContext (org.apache.cxf.security.SecurityContext)14 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)14 Element (org.w3c.dom.Element)13 SAMLTokenValidator (org.apache.cxf.sts.token.validator.SAMLTokenValidator)10 SAMLTokenProvider (org.apache.cxf.sts.token.provider.SAMLTokenProvider)8 TokenProvider (org.apache.cxf.sts.token.provider.TokenProvider)8 RequestedSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType)8