Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CXF-8236] add support of signature challenges in the STSClient #651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,16 @@ protected SecurityToken createSecurityToken(Element el, byte[] requestorEntropy)
tt = DOMUtils.getContent(el);
} else if ("KeySize".equals(ln)) {
retKeySize = DOMUtils.getContent(el);
} else if ("SignChallenge".equals(ln)) {
el = DOMUtils.getFirstElement(el);
if ("Challenge".equals(el.getLocalName())) {
// maybe another implementation of the return object is more useful.
// We need to transport only two values:
// challengeValue and a marker for the kind of response (SignChallenge response)
SecurityToken token = new SecurityToken(DOMUtils.getContent(el));
token.setTokenType("SignChallenge");
return token;
}
Comment on lines +1494 to +1503
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could have a new Challenge class which is referenced in SecurityToken. Challenge could have an enum describing the type, maybe just SIGNATURE for now.

}
}
el = DOMUtils.getNextElement(el);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Element;

import org.apache.cxf.Bus;
import org.apache.cxf.attachment.AttachmentUtil;
import org.apache.cxf.binding.soap.SoapBindingConstants;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Attachment;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.staxutils.W3CDOMStreamWriter;
import org.apache.cxf.ws.security.tokenstore.SecurityToken;
import org.apache.cxf.ws.security.trust.AbstractSTSClient.STSResponse;
import org.apache.cxf.ws.security.wss4j.AttachmentCallbackHandler;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.common.util.XMLUtils;
Expand Down Expand Up @@ -87,6 +93,76 @@ public SecurityToken requestSecurityToken(
}
return token;
}

/**
* see WS-Trust 1.4 chapter 8:
* http://docs.oasis-open.org/ws-sx/ws-trust/v1.4/errata01/os/ws-trust-1.4-errata01-os-complete.html#_Toc325658962
*
* Creating of (manually triggered second) request with a
* RequestSecurityTokenResponse Object
* <wst:RequestSecurityTokenResponse xmlns:wst=
* "http://docs.oasis-open.org/ws-sx/ws-trust/200512" Context=
* "d956dafc-1da5-4661-bab7-834640e659ec"> <wst:SignChallengeResponse>
* <wst:Challenge>4451658898</wst:Challenge> </wst:SignChallengeResponse>
* </wst:RequestSecurityTokenResponse>
*
* @param action
* @param challengeValue the challenge value that was received by first
* issue-response
* @return SecurityToken
* @throws Exception
*/
public SecurityToken requestSecurityTokenResponse(String action, String challengeValue) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer to remove this method altogether. Instead, refactor "issue" so that it can work for both cases. The only difference should be "RequestSecurityTokenResponse" instead of "RequestSecurityToken" for the normal issue case. Maybe the writing out of the Challenge itself could be delegated to the Challenge class itself.

Then in SecurityToken.requestSecurityToken, I think we could handle the SignChallenge case automatically. If the SecurityToken that is returned from the STS contains a SignChallenge then invoke again on the STS with the challenge.

//mostly copied from issue() and requestSecurityToken() methods => needs bigger refactoring
createClient();
BindingOperationInfo boi = findOperation("/RSTR/Issue");

//we need only /RSTR/Issue
client.getRequestContext().putAll(ctx);
if (action != null) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, action);
// nothing found about this in WS-Trust spec:
// } else if (isSecureConv) {
// client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION,
// namespace + "/RST/SCT");
} else {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION,
namespace + "/RSTR/Issue");
}

//deviation from issue: first element must be RequestSecurityTokenResponse
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityTokenResponse", namespace);
writer.writeNamespace("wst", namespace);
if (context != null) {
writer.writeAttribute(null, "Context", context);
}

writer.writeStartElement("wst", "SignChallengeResponse", namespace);

writer.writeStartElement("wst", "Challenge", namespace);
writer.writeCharacters(challengeValue);
writer.writeEndElement();

writer.writeEndElement();
writer.writeEndElement();

Object[] obj = client.invoke(boi, new DOMSource(writer.getDocument().getDocumentElement()));

@SuppressWarnings("unchecked")
Collection<Attachment> attachments =
(Collection<Attachment>)client.getResponseContext().get(Message.ATTACHMENTS);
STSResponse stsResponse = new STSResponse((DOMSource)obj[0], null, null, null, attachments);

SecurityToken token =
createSecurityToken(getDocumentElement(stsResponse.getResponse()), stsResponse.getEntropy());
inlineAttachments(token, stsResponse.getAttachments());

if (stsResponse.getCert() != null) {
token.setX509Certificate(stsResponse.getCert(), stsResponse.getCrypto());
}
return token;
}

public SecurityToken renewSecurityToken(SecurityToken tok) throws Exception {
STSResponse response = renew(tok);
Expand Down