2 * External class so it can be configured by String name to SAML.
5 * samlConfig = SAMLConfig.instance();
6 * samlConfig.setDefaultBindingProvider(SAMLBinding.SOAP,"edu.internet2.middleware.shibboleth.runner.MockHTTPBindingProvider" );
7 * in ShibbolethRunner constructor.
11 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
26 package edu.internet2.middleware.shibboleth.runner;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.net.MalformedURLException;
32 import org.apache.xml.security.c14n.CanonicalizationException;
33 import org.apache.xml.security.c14n.Canonicalizer;
34 import org.apache.xml.security.c14n.InvalidCanonicalizerException;
35 import org.opensaml.BindingException;
36 import org.opensaml.SAMLBinding;
37 import org.opensaml.SAMLConfig;
38 import org.opensaml.SAMLException;
39 import org.opensaml.SAMLRequest;
40 import org.opensaml.SAMLResponse;
41 import org.opensaml.XML;
42 import org.opensaml.provider.SOAPHTTPBindingProvider;
43 import org.w3c.dom.Element;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
47 import edu.internet2.middleware.shibboleth.runner.ShibbolethRunner.IdpTestContext;
51 * This is a replacement for SOAPHTTPBindingProvider in OpenSAML. While that
52 * module builds a URL and URLConnection to send a request to a Web Server
53 * hosting the IdP, this code generates a direct call to the AA or Artifact
54 * Resolver through the IdP Servlet.
56 * <p>The ShibbolethRunner constructor sets this class name as the SAML
57 * default BindingProvider.</p>
59 public class MockHTTPBindingProvider
60 extends SOAPHTTPBindingProvider {
63 /** OpenSAML will construct this object. */
64 public MockHTTPBindingProvider(String binding, Element e) throws SAMLException {
69 * Based on the Http version of this code, this method replaces the URL and
70 * URLConnection with operations on the Mock HttpRequest.
72 public SAMLResponse send(String endpoint, SAMLRequest request, Object callCtx)
76 Element envelope = sendRequest(request, callCtx);
78 IdpTestContext idp = ShibbolethRunner.idp;
81 * Prepare the Idp Mockrunner blocks for the Query
83 idp.request.setLocalPort(8443);
84 idp.request.setRequestURI(endpoint);
85 idp.request.setRequestURL(endpoint);
86 if (endpoint.endsWith("/AA")) {
87 idp.request.setServletPath("/shibboleth.idp/AA");
89 idp.request.setServletPath("/shibboleth.idp/Artifact");
92 idp.request.setContentType("text/xml; charset=UTF-8");
93 idp.request.setHeader("SOAPAction","http://www.oasis-open.org/committees/security");
97 Canonicalizer c = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
98 byte[] bs = c.canonicalizeSubtree(envelope);
99 idp.request.setBodyContent(bs);
101 idp.testModule.doPost();
103 String content_type=idp.response.getContentType();
105 if (content_type == null || !content_type.startsWith("text/xml")) {
106 String outputStreamContent = idp.response.getOutputStreamContent();
107 StringReader outputreader = new StringReader(outputStreamContent);
108 BufferedReader reader=new BufferedReader(outputreader);
109 throw new BindingException(
110 "MockHTTPBindingProvider.send() detected an invalid content type ("
111 + (content_type!=null ? content_type : "none")
112 + ") in the response.");
115 String content = idp.response.getOutputStreamContent();
116 idp.response.resetBuffer(); // Make Response reusable for next test
117 envelope=XML.parserPool.parse(
118 new InputSource(new StringReader(content)),
119 (request.getMinorVersion()>0) ? XML.parserPool.getSchemaSAML11() : XML.parserPool.getSchemaSAML10()
120 ).getDocumentElement();
122 SAMLResponse ret = recvResponse(envelope, callCtx);
124 if (!ret.getInResponseTo().equals(request.getId())) {
125 throw new BindingException("MockHTTPBindingProvider.send() unable to match SAML InResponseTo value to request");
129 catch (MalformedURLException ex) {
130 throw new SAMLException("SAMLSOAPBinding.send() detected a malformed URL in the binding provided", ex);
132 catch (SAXException ex) {
133 throw new SAMLException("SAMLSOAPBinding.send() caught an XML exception while parsing the response", ex);
135 catch (InvalidCanonicalizerException ex) {
136 throw new SAMLException("SAMLSOAPBinding.send() caught a C14N exception while serializing the request", ex);
138 catch (CanonicalizationException ex) {
139 throw new SAMLException("SAMLSOAPBinding.send() caught a C14N exception while serializing the request", ex);
141 catch (java.io.IOException ex) {
142 throw new SAMLException("SAMLSOAPBinding.send() caught an I/O exception", ex);
148 public SAMLResponse send(String endpoint, SAMLRequest request) throws SAMLException {
149 return send(endpoint, request, null);