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/saml1/core/Assertions.h>
\r
21 #include <saml/saml1/core/Protocols.h>
\r
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>
\r
23 #include <xmltooling/validation/ValidatorSuite.h>
\r
25 using namespace opensaml::saml1p;
\r
26 using namespace opensaml::saml1;
\r
28 class SAML1ArtifactTest : public CxxTest::TestSuite,
\r
29 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
\r
33 SAMLBindingBaseTestCase::setUp();
\r
38 SAMLBindingBaseTestCase::tearDown();
\r
41 void testSAML1Artifact() {
\r
43 // Read message to use from file.
\r
44 string path = data_path + "saml1/binding/SAML1Assertion.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<Assertion> toSend(
\r
49 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
54 auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_ARTIFACT_ENCODER, NULL));
\r
55 encoder->setArtifactGenerator(this);
\r
56 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
\r
61 const RoleDescriptor* issuer=NULL;
\r
63 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
64 auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_ARTIFACT_DECODER, NULL));
\r
65 decoder->setArtifactResolver(this);
\r
66 Locker locker(m_metadata);
\r
67 auto_ptr<Response> response(
\r
68 dynamic_cast<Response*>(
\r
69 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
\r
73 // Test the results.
\r
74 TSM_ASSERT_EQUALS("TARGET 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.", issuer && trusted);
\r
77 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
\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
82 TSM_ASSERT_THROWS("Did not catch the replay.",
\r
83 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
\r
86 catch (XMLToolingException& ex) {
\r
87 TS_TRACE(ex.what());
\r
92 const char* getMethod() const {
\r
96 const char* getRequestURL() const {
\r
97 return "https://sp.example.org/SAML/Artifact";
\r
100 const char* getQueryString() const {
\r
104 SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
\r
105 return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
\r
108 saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
\r
109 throw BindingException("Not implemented.");
\r
113 bool& authenticated,
\r
114 const vector<SAMLArtifact*>& artifacts,
\r
115 const IDPSSODescriptor& idpDescriptor,
\r
116 const X509TrustEngine* trustEngine=NULL
\r
118 TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
\r
119 XMLObject* xmlObject =
\r
120 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
\r
121 Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
\r
122 TSM_ASSERT("Not an assertion.", assertion!=NULL);
\r
123 auto_ptr<Response> response(ResponseBuilder::buildResponse());
\r
124 response->getAssertions().push_back(assertion);
\r
125 Status* status = StatusBuilder::buildStatus();
\r
126 response->setStatus(status);
\r
127 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
\r
128 status->setStatusCode(sc);
\r
129 sc->setValue(&StatusCode::SUCCESS);
\r
130 response->marshall();
\r
131 SchemaValidators.validate(response.get());
\r
132 authenticated = true;
\r
133 return response.release();
\r
136 saml2p::ArtifactResponse* resolve(
\r
137 bool& authenticated,
\r
138 const saml2p::SAML2Artifact& artifact,
\r
139 const SSODescriptorType& ssoDescriptor,
\r
140 const X509TrustEngine* trustEngine=NULL
\r
142 throw BindingException("Not implemented.");
\r