TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
// Create a ValidateTarget consisting of a UsernameToken
UsernameTokenType usernameToken = new UsernameTokenType();
AttributedString username = new AttributedString();
username.setValue("eve");
usernameToken.setUsername(username);
JAXBElement<UsernameTokenType> tokenType =
new JAXBElement<UsernameTokenType>(
QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameToken
);
// Add a password
PasswordString password = new PasswordString();
password.setValue("clarinet");
password.setType(WSConstants.PASSWORD_TEXT);
JAXBElement<PasswordString> passwordType =
new JAXBElement<PasswordString>(
QNameConstants.PASSWORD, PasswordString.class, password
);
usernameToken.getAny().add(passwordType);
ReceivedToken validateTarget = new ReceivedToken(tokenType);
tokenRequirements.setValidateTarget(validateTarget);
assertTrue(usernameTokenValidator.canHandleToken(validateTarget));
// This will fail as the username is bad
TokenValidatorResponse validatorResponse =
usernameTokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertFalse(validatorResponse.isValid());
// This will fail as the password is bad
username.setValue("alice");
password.setValue("badpassword");
validatorResponse = usernameTokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertFalse(validatorResponse.isValid());
}