use of org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType in project cxf by apache.
the class ValidateUnitTest method createToken.
/**
* Mock up a (JAXB) BinarySecurityTokenType.
*/
private JAXBElement<BinarySecurityTokenType> createToken() {
BinarySecurityTokenType binarySecurityToken = new BinarySecurityTokenType();
binarySecurityToken.setId("BST-1234");
binarySecurityToken.setValue("12345678");
binarySecurityToken.setValueType(DummyTokenProvider.TOKEN_TYPE);
binarySecurityToken.setEncodingType(DummyTokenProvider.BASE64_NS);
return new JAXBElement<BinarySecurityTokenType>(QNameConstants.BINARY_SECURITY_TOKEN, BinarySecurityTokenType.class, binarySecurityToken);
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType in project cxf by apache.
the class ValidateX509TokenUnitTest method testValidateX509Token.
/**
* Test to successfully validate an X.509 token
*/
@org.junit.Test
public void testValidateX509Token() 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("myclientkey");
X509Certificate[] certs = crypto.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);
assertTrue(validateResponse(response));
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType in project cxf by apache.
the class ValidateX509TokenUnitTest method createBinarySecurityToken.
private JAXBElement<BinarySecurityTokenType> createBinarySecurityToken(X509Certificate cert) throws Exception {
BinarySecurityTokenType binarySecurityToken = new BinarySecurityTokenType();
binarySecurityToken.setValue(Base64.getMimeEncoder().encodeToString(cert.getEncoded()));
binarySecurityToken.setValueType(X509TokenValidator.X509_V3_TYPE);
binarySecurityToken.setEncodingType(WSS4JConstants.SOAPMESSAGE_NS + "#Base64Binary");
return new JAXBElement<BinarySecurityTokenType>(QNameConstants.BINARY_SECURITY_TOKEN, BinarySecurityTokenType.class, binarySecurityToken);
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType in project cxf by apache.
the class CustomBSTTokenValidator method validateToken.
public TokenValidatorResponse validateToken(TokenValidatorParameters tokenParameters) {
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(STATE.INVALID);
response.setToken(validateTarget);
if (!validateTarget.isBinarySecurityToken()) {
return response;
}
BinarySecurityTokenType binarySecurityToken = (BinarySecurityTokenType) validateTarget.getToken();
//
if (Base64.getMimeEncoder().encodeToString("12345678".getBytes()).equals(binarySecurityToken.getValue())) {
validateTarget.setState(STATE.VALID);
}
response.setPrincipal(new CustomTokenPrincipal("alice"));
return response;
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.BinarySecurityTokenType in project cxf by apache.
the class X509TokenValidatorTest method testInvalidCertificate.
/**
* Test an invalid certificate
*/
@org.junit.Test
public void testInvalidCertificate() throws Exception {
TokenValidator x509TokenValidator = new X509TokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
// Create a ValidateTarget consisting of an X509Certificate
BinarySecurityTokenType binarySecurityToken = new BinarySecurityTokenType();
JAXBElement<BinarySecurityTokenType> tokenType = new JAXBElement<BinarySecurityTokenType>(QNameConstants.BINARY_SECURITY_TOKEN, BinarySecurityTokenType.class, binarySecurityToken);
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
cryptoType.setAlias("eve");
Crypto crypto = CryptoFactory.getInstance(getEveCryptoProperties());
X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
assertTrue(certs != null && certs.length > 0);
binarySecurityToken.setValue(Base64.getMimeEncoder().encodeToString(certs[0].getEncoded()));
binarySecurityToken.setValueType(X509TokenValidator.X509_V3_TYPE);
binarySecurityToken.setEncodingType(WSS4JConstants.SOAPMESSAGE_NS + "#Base64Binary");
ReceivedToken validateTarget = new ReceivedToken(tokenType);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(x509TokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = x509TokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
}
Aggregations