use of org.apache.ws.security.message.WSSecUsernameToken in project iaf by ibissource.
the class SoapWrapper method signMessage.
public String signMessage(String soapMessage, String user, String password) throws SenderException {
try {
WSSecurityEngine secEngine = WSSecurityEngine.getInstance();
WSSConfig config = secEngine.getWssConfig();
config.setPrecisionInMilliSeconds(false);
// create context
AxisClient tmpEngine = new AxisClient(new NullProvider());
MessageContext msgContext = new MessageContext(tmpEngine);
InputStream in = new ByteArrayInputStream(soapMessage.getBytes());
Message msg = new Message(in);
msg.setMessageContext(msgContext);
// create unsigned envelope
SOAPEnvelope unsignedEnvelope = msg.getSOAPEnvelope();
Document doc = unsignedEnvelope.getAsDocument();
// create security header and insert it into unsigned envelope
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
// add a UsernameToken
WSSecUsernameToken tokenBuilder = new WSSecUsernameToken();
tokenBuilder.setPasswordType(WSConstants.PASSWORD_DIGEST);
tokenBuilder.setUserInfo(user, password);
tokenBuilder.addNonce();
tokenBuilder.addCreated();
tokenBuilder.prepare(doc);
WSSecSignature sign = new WSSecSignature();
sign.setUsernameToken(tokenBuilder);
sign.setKeyIdentifierType(WSConstants.UT_SIGNING);
sign.setSignatureAlgorithm(XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
sign.build(doc, null, secHeader);
tokenBuilder.prependToHeader(secHeader);
// add a Timestamp
WSSecTimestamp timestampBuilder = new WSSecTimestamp();
timestampBuilder.setTimeToLive(300);
timestampBuilder.prepare(doc);
timestampBuilder.prependToHeader(secHeader);
Document signedDoc = doc;
return DOM2Writer.nodeToString(signedDoc);
} catch (Exception e) {
throw new SenderException(e);
}
}
use of org.apache.ws.security.message.WSSecUsernameToken in project OpenAM by OpenRock.
the class TokenSpecification method usernameTokenOnBehalfOfElement.
/**
* @param username the UsernameToken username
* @param password the UsernameToken password
* @return A UsernameToken element to be used as the OnBehalfOf Element in a RequestSecurityToken defining the ISSUE
* operation invocation.
*/
public static Element usernameTokenOnBehalfOfElement(String username, String password) {
WSSecUsernameToken unt = new WSSecUsernameToken();
unt.setUserInfo(username, password);
unt.setPasswordType(WSConstants.PASSWORD_TEXT);
unt.addCreated();
Date expirationDate = new Date();
expirationDate.setTime(System.currentTimeMillis() + (1000 * 60));
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
unt.prepare(document);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
return unt.getUsernameTokenElement();
}
Aggregations