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/saml2/core/Protocols.h>
\r
21 using namespace opensaml::saml2p;
\r
22 using namespace opensaml::saml2;
\r
24 class SAML2POSTTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {
\r
28 SAMLBindingBaseTestCase::setUp();
\r
33 SAMLBindingBaseTestCase::tearDown();
\r
36 void testSAML2POSTTrusted() {
\r
38 // Read message to use from file.
\r
39 string path = data_path + "saml2/binding/SAML2Response.xml";
\r
40 ifstream in(path.c_str());
\r
41 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
42 XercesJanitor<DOMDocument> janitor(doc);
\r
43 auto_ptr<Response> toSend(
\r
44 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
48 // Freshen timestamp.
\r
49 toSend->setIssueInstant(time(NULL));
\r
52 auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML2_POST_ENCODER, NULL));
\r
53 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
\r
58 const RoleDescriptor* issuer=NULL;
\r
60 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
61 auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML2_POST_DECODER, NULL));
\r
62 Locker locker(m_metadata);
\r
63 auto_ptr<Response> response(
\r
64 dynamic_cast<Response*>(
\r
65 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
\r
69 // Test the results.
\r
70 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
\r
71 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
\r
72 TSM_ASSERT("Message was not verified.", issuer && trusted);
\r
73 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
\r
74 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
\r
75 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
\r
77 catch (XMLToolingException& ex) {
\r
78 TS_TRACE(ex.what());
\r
83 void testSAML2POSTUntrusted() {
\r
85 // Read message to use from file.
\r
86 string path = data_path + "saml2/binding/SAML2Response.xml";
\r
87 ifstream in(path.c_str());
\r
88 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
89 XercesJanitor<DOMDocument> janitor(doc);
\r
90 auto_ptr<Response> toSend(
\r
91 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
95 // Freshen timestamp and clear ID.
\r
96 toSend->setIssueInstant(time(NULL));
\r
97 toSend->setID(NULL);
\r
100 auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML2_POST_ENCODER, NULL));
\r
101 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");
\r
106 const RoleDescriptor* issuer=NULL;
\r
107 bool trusted=false;
\r
108 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
109 auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML2_POST_DECODER, NULL));
\r
110 Locker locker(m_metadata);
\r
111 auto_ptr<Response> response(
\r
112 dynamic_cast<Response*>(
\r
113 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)
\r
117 // Test the results.
\r
118 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
\r
119 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
\r
120 TSM_ASSERT("Message was verified.", issuer && !trusted);
\r
121 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
\r
122 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
\r
123 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
\r
125 // Trigger a replay.
\r
126 TSM_ASSERT_THROWS("Did not catch the replay.",
\r
127 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
\r
130 catch (XMLToolingException& ex) {
\r
131 TS_TRACE(ex.what());
\r
136 const char* getMethod() const {
\r
140 const char* getRequestURL() const {
\r
141 return "https://sp.example.org/SAML/POST";
\r
144 const char* getQueryString() const {
\r