2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <saml/saml2/core/Protocols.h>
21 using namespace opensaml::saml2p;
22 using namespace opensaml::saml2;
24 class SAML2POSTTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {
27 SAMLBindingBaseTestCase::setUp();
31 SAMLBindingBaseTestCase::tearDown();
34 void testSAML2POST() {
36 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
37 SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);
39 // Read message to use from file.
40 string path = data_path + "saml2/binding/SAML2Response.xml";
41 ifstream in(path.c_str());
42 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
43 XercesJanitor<DOMDocument> janitor(doc);
44 auto_ptr<Response> toSend(
45 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
49 CredentialCriteria cc;
50 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
51 Locker clocker(m_creds);
52 const Credential* cred = m_creds->resolve(&cc);
53 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
55 // Freshen timestamp and ID.
56 toSend->setIssueInstant(time(NULL));
60 auto_ptr_XMLCh lit1("MessageEncoder");
61 auto_ptr_XMLCh lit2("template");
62 path = data_path + "binding/template.html";
63 auto_ptr_XMLCh lit3(path.c_str());
64 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
65 XercesJanitor<DOMDocument> janitor2(encoder_config);
66 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
67 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
68 auto_ptr<MessageEncoder> encoder(
69 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
70 samlconstants::SAML20_BINDING_HTTP_POST, pair<const DOMElement*,const XMLCh*>(encoder_config->getDocumentElement(), NULL)
73 Locker locker(m_metadata);
75 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",NULL,cred
81 auto_ptr<MessageDecoder> decoder(
82 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
83 samlconstants::SAML20_BINDING_HTTP_POST, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
86 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
89 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
90 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
91 TSM_ASSERT("Message was not verified.", policy.isSecure());
92 auto_ptr_char entityID(policy.getIssuer()->getName());
93 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
94 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
98 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
100 catch (XMLToolingException& ex) {
106 void testSAML2POSTSimpleSign() {
108 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
109 SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);
111 // Read message to use from file.
112 string path = data_path + "saml2/binding/SAML2Response.xml";
113 ifstream in(path.c_str());
114 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
115 XercesJanitor<DOMDocument> janitor(doc);
116 auto_ptr<Response> toSend(
117 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
121 CredentialCriteria cc;
122 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
123 Locker clocker(m_creds);
124 const Credential* cred = m_creds->resolve(&cc);
125 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
127 // Freshen timestamp and ID.
128 toSend->setIssueInstant(time(NULL));
132 auto_ptr_XMLCh lit1("MessageEncoder");
133 auto_ptr_XMLCh lit2("template");
134 path = data_path + "binding/template.html";
135 auto_ptr_XMLCh lit3(path.c_str());
136 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
137 XercesJanitor<DOMDocument> janitor2(encoder_config);
138 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
139 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
140 auto_ptr<MessageEncoder> encoder(
141 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
142 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, pair<const DOMElement*,const XMLCh*>(encoder_config->getDocumentElement(),NULL)
145 Locker locker(m_metadata);
147 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",NULL,cred
153 auto_ptr<MessageDecoder> decoder(
154 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
155 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
158 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
161 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
162 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
163 TSM_ASSERT("Message was not verified.", policy.isSecure());
164 auto_ptr_char entityID(policy.getIssuer()->getName());
165 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
166 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
170 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
172 catch (XMLToolingException& ex) {