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
32 SAMLBindingBaseTestCase::setUp();
\r
37 SAMLBindingBaseTestCase::tearDown();
\r
40 void testSAML2Artifact() {
\r
42 // Read message to use from file.
\r
43 string path = data_path + "saml2/binding/SAML2Response.xml";
\r
44 ifstream in(path.c_str());
\r
45 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
46 XercesJanitor<DOMDocument> janitor(doc);
\r
47 auto_ptr<Response> toSend(
\r
48 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
52 // Freshen timestamp.
\r
53 toSend->setIssueInstant(time(NULL));
\r
56 auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML2_ARTIFACT_ENCODER, NULL));
\r
57 encoder->setArtifactGenerator(this);
\r
58 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
\r
63 const RoleDescriptor* issuer=NULL;
\r
65 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
66 auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML2_ARTIFACT_DECODER, NULL));
\r
67 decoder->setArtifactResolver(this);
\r
68 Locker locker(m_metadata);
\r
69 auto_ptr<Response> response(
\r
70 dynamic_cast<Response*>(
\r
71 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
\r
75 // Test the results.
\r
76 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
\r
77 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
\r
78 TSM_ASSERT("Message was not verified.", issuer && trusted);
\r
79 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
\r
80 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
\r
81 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
\r
83 // Trigger a replay.
\r
84 TSM_ASSERT_THROWS("Did not catch the replay.",
\r
85 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
\r
88 catch (XMLToolingException& ex) {
\r
89 TS_TRACE(ex.what());
\r
94 const char* getMethod() const {
\r
98 const char* getRequestURL() const {
\r
99 return "https://sp.example.org/SAML/Artifact";
\r
102 const char* getQueryString() const {
\r
106 SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
\r
107 throw BindingException("Not implemented.");
\r
110 saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
\r
111 return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);
\r
114 saml1p::Response* resolve(
\r
115 bool& authenticated,
\r
116 const vector<SAMLArtifact*>& artifacts,
\r
117 const IDPSSODescriptor& idpDescriptor,
\r
118 const X509TrustEngine* trustEngine=NULL
\r
120 throw BindingException("Not implemented.");
\r
123 ArtifactResponse* resolve(
\r
124 bool& authenticated,
\r
125 const SAML2Artifact& artifact,
\r
126 const SSODescriptorType& ssoDescriptor,
\r
127 const X509TrustEngine* trustEngine=NULL
\r
129 XMLObject* xmlObject =
\r
130 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");
\r
131 Response* payload = dynamic_cast<Response*>(xmlObject);
\r
132 TSM_ASSERT("Not a response.", payload!=NULL);
\r
133 auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());
\r
134 response->setPayload(payload);
\r
135 Status* status = StatusBuilder::buildStatus();
\r
136 response->setStatus(status);
\r
137 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
\r
138 status->setStatusCode(sc);
\r
139 sc->setValue(StatusCode::SUCCESS);
\r
140 response->marshall();
\r
141 SchemaValidators.validate(response.get());
\r
142 authenticated = true;
\r
143 return response.release();
\r