use of org.apache.cxf.ws.security.sts.provider.model.secext.PasswordString in project ddf by codice.
the class WssBasicAuthenticationHandler method getBaseAuthenticationToken.
protected BaseAuthenticationToken getBaseAuthenticationToken(String realm, String username, String password) {
if (null == parser) {
throw new IllegalStateException("XMLParser must be configured.");
}
UsernameTokenType usernameTokenType = new UsernameTokenType();
AttributedString user = new AttributedString();
user.setValue(username);
usernameTokenType.setUsername(user);
String usernameToken = null;
// Add a password
PasswordString pass = new PasswordString();
pass.setValue(password);
pass.setType(WSConstants.PASSWORD_TEXT);
JAXBElement<PasswordString> passwordType = new JAXBElement<>(QNameConstants.PASSWORD, PasswordString.class, pass);
usernameTokenType.getAny().add(passwordType);
// Marshall the received JAXB object into a DOM Element
List<String> ctxPath = new ArrayList<>(2);
ctxPath.add(ObjectFactory.class.getPackage().getName());
ctxPath.add(org.apache.cxf.ws.security.sts.provider.model.wstrust14.ObjectFactory.class.getPackage().getName());
ParserConfigurator configurator = parser.configureParser(ctxPath, WssBasicAuthenticationHandler.class.getClassLoader());
ByteArrayOutputStream os = new ByteArrayOutputStream();
JAXBElement<UsernameTokenType> tokenType = new JAXBElement<>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameTokenType);
try {
parser.marshal(configurator, tokenType, os);
usernameToken = os.toString("UTF-8");
} catch (ParserException | UnsupportedEncodingException ex) {
LOGGER.info("Unable to parse username token.", ex);
}
BaseAuthenticationToken baseAuthenticationToken = new BaseAuthenticationToken(null, "", usernameToken);
baseAuthenticationToken.setUseWssSts(true);
return baseAuthenticationToken;
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.PasswordString in project cxf by apache.
the class ValidateUsernameTokenUnitTest method createUsernameToken.
private JAXBElement<UsernameTokenType> createUsernameToken(String name, String password) {
UsernameTokenType usernameToken = new UsernameTokenType();
AttributedString username = new AttributedString();
username.setValue(name);
usernameToken.setUsername(username);
// Add a password
PasswordString passwordString = new PasswordString();
passwordString.setValue(password);
passwordString.setType(WSS4JConstants.PASSWORD_TEXT);
JAXBElement<PasswordString> passwordType = new JAXBElement<PasswordString>(QNameConstants.PASSWORD, PasswordString.class, passwordString);
usernameToken.getAny().add(passwordType);
return new JAXBElement<UsernameTokenType>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameToken);
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.PasswordString in project cxf by apache.
the class UsernameTokenValidatorTest method testInvalidUsernameTokenText.
/**
* Test an invalid UsernameToken with password text
*/
@org.junit.Test
public void testInvalidUsernameTokenText() throws Exception {
TokenValidator usernameTokenValidator = new UsernameTokenValidator();
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(WSS4JConstants.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);
validatorParameters.setToken(validateTarget);
assertTrue(usernameTokenValidator.canHandleToken(validateTarget));
// This will fail as the username is bad
TokenValidatorResponse validatorResponse = usernameTokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
// This will fail as the password is bad
username.setValue("alice");
password.setValue("badpassword");
validatorResponse = usernameTokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.PasswordString in project ddf by codice.
the class UPBSTValidator method getUsernameTokenType.
public UsernameTokenType getUsernameTokenType(UPAuthenticationToken token) {
UsernameTokenType usernameTokenType = new UsernameTokenType();
AttributedString user = new AttributedString();
user.setValue(token.getUsername());
usernameTokenType.setUsername(user);
// Add a password
PasswordString password = new PasswordString();
password.setValue(token.getPassword());
password.setType(WSConstants.PASSWORD_TEXT);
JAXBElement<PasswordString> passwordType = new JAXBElement<PasswordString>(QNameConstants.PASSWORD, PasswordString.class, password);
usernameTokenType.getAny().add(passwordType);
return usernameTokenType;
}
use of org.apache.cxf.ws.security.sts.provider.model.secext.PasswordString in project cxf by apache.
the class IssueJWTOnbehalfofUnitTest method createUsernameToken.
private JAXBElement<UsernameTokenType> createUsernameToken(String name, String password) {
UsernameTokenType usernameToken = new UsernameTokenType();
AttributedString username = new AttributedString();
username.setValue(name);
usernameToken.setUsername(username);
// Add a password
if (password != null) {
PasswordString passwordString = new PasswordString();
passwordString.setValue(password);
passwordString.setType(WSS4JConstants.PASSWORD_TEXT);
JAXBElement<PasswordString> passwordType = new JAXBElement<PasswordString>(QNameConstants.PASSWORD, PasswordString.class, passwordString);
usernameToken.getAny().add(passwordType);
}
return new JAXBElement<UsernameTokenType>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameToken);
}
Aggregations