2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 #include "binding.h"
\r
19 #include <saml/binding/ArtifactMap.h>
\r
20 #include <saml/saml2/core/Protocols.h>
\r
21 #include <saml/saml2/binding/SAML2ArtifactType0004.h>
\r
22 #include <xmltooling/validation/ValidatorSuite.h>
\r
24 using namespace opensaml::saml2p;
\r
25 using namespace opensaml::saml2;
\r
27 class SAML2ArtifactTest : public CxxTest::TestSuite,
\r
28 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
\r
31 SAMLBindingBaseTestCase::setUp();
\r
35 SAMLBindingBaseTestCase::tearDown();
\r
38 void testSAML2Artifact() {
\r
40 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
41 SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust);
\r
43 // Read message to use from file.
\r
44 string path = data_path + "saml2/binding/SAML2Response.xml";
\r
45 ifstream in(path.c_str());
\r
46 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
47 XercesJanitor<DOMDocument> janitor(doc);
\r
48 auto_ptr<Response> toSend(
\r
49 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
53 // Freshen timestamp.
\r
54 toSend->setIssueInstant(time(NULL));
\r
57 auto_ptr<MessageEncoder> encoder(
\r
58 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)
\r
60 encoder->setArtifactGenerator(this);
\r
61 encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",m_creds);
\r
66 auto_ptr<MessageDecoder> decoder(
\r
67 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)
\r
69 decoder->setArtifactResolver(this);
\r
70 Locker locker(m_metadata);
\r
71 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
\r
73 // Test the results.
\r
74 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
\r
75 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
\r
76 TSM_ASSERT("Message was not verified.", policy.getIssuer()!=NULL);
\r
77 auto_ptr_char entityID(policy.getIssuer()->getName());
\r
78 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
\r
79 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
\r
81 // Trigger a replay.
\r
83 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);
\r
85 catch (XMLToolingException& ex) {
\r
86 TS_TRACE(ex.what());
\r
91 SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
\r
92 throw BindingException("Not implemented.");
\r
95 saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
\r
96 return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);
\r
99 saml1p::Response* resolve(
\r
100 const vector<SAMLArtifact*>& artifacts,
\r
101 const IDPSSODescriptor& idpDescriptor,
\r
102 SecurityPolicy& policy
\r
104 throw BindingException("Not implemented.");
\r
107 ArtifactResponse* resolve(
\r
108 const SAML2Artifact& artifact,
\r
109 const SSODescriptorType& ssoDescriptor,
\r
110 SecurityPolicy& policy
\r
112 XMLObject* xmlObject =
\r
113 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");
\r
114 Response* payload = dynamic_cast<Response*>(xmlObject);
\r
115 TSM_ASSERT("Not a response.", payload!=NULL);
\r
116 auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());
\r
117 response->setPayload(payload);
\r
118 Status* status = StatusBuilder::buildStatus();
\r
119 response->setStatus(status);
\r
120 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
\r
121 status->setStatusCode(sc);
\r
122 sc->setValue(StatusCode::SUCCESS);
\r
123 response->marshall();
\r
124 SchemaValidators.validate(response.get());
\r
125 return response.release();
\r