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_metadata, &idprole, m_trust, false);
38 policy.getRules().assign(m_rules2.begin(), m_rules2.end());
40 // Read message to use from file.
41 string path = data_path + "saml2/binding/SAML2Response.xml";
42 ifstream in(path.c_str());
43 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44 XercesJanitor<DOMDocument> janitor(doc);
45 auto_ptr<Response> toSend(
46 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
50 CredentialCriteria cc;
51 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
52 Locker clocker(m_creds);
53 const Credential* cred = m_creds->resolve(&cc);
54 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
56 // Freshen timestamp and ID.
57 toSend->setIssueInstant(time(NULL));
61 auto_ptr_XMLCh lit1("MessageEncoder");
62 auto_ptr_XMLCh lit2("template");
63 path = data_path + "binding/template.html";
64 auto_ptr_XMLCh lit3(path.c_str());
65 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
66 XercesJanitor<DOMDocument> janitor2(encoder_config);
67 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
68 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
69 auto_ptr<MessageEncoder> encoder(
70 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
71 samlconstants::SAML20_BINDING_HTTP_POST, pair<const DOMElement*,const XMLCh*>(encoder_config->getDocumentElement(), NULL)
74 Locker locker(m_metadata);
76 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",NULL,cred
82 auto_ptr<MessageDecoder> decoder(
83 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
84 samlconstants::SAML20_BINDING_HTTP_POST, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
87 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
90 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
91 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
92 TSM_ASSERT("Message was not verified.", policy.isSecure());
93 auto_ptr_char entityID(policy.getIssuer()->getName());
94 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
95 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
99 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
101 catch (XMLToolingException& ex) {
107 void testSAML2POSTSimpleSign() {
109 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
110 SecurityPolicy policy(m_metadata, &idprole, m_trust, false);
111 policy.getRules().assign(m_rules2.begin(), m_rules2.end());
113 // Read message to use from file.
114 string path = data_path + "saml2/binding/SAML2Response.xml";
115 ifstream in(path.c_str());
116 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
117 XercesJanitor<DOMDocument> janitor(doc);
118 auto_ptr<Response> toSend(
119 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
123 CredentialCriteria cc;
124 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
125 Locker clocker(m_creds);
126 const Credential* cred = m_creds->resolve(&cc);
127 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
129 // Freshen timestamp and ID.
130 toSend->setIssueInstant(time(NULL));
134 auto_ptr_XMLCh lit1("MessageEncoder");
135 auto_ptr_XMLCh lit2("template");
136 path = data_path + "binding/template.html";
137 auto_ptr_XMLCh lit3(path.c_str());
138 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
139 XercesJanitor<DOMDocument> janitor2(encoder_config);
140 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
141 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
142 auto_ptr<MessageEncoder> encoder(
143 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
144 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, pair<const DOMElement*,const XMLCh*>(encoder_config->getDocumentElement(),NULL)
147 Locker locker(m_metadata);
149 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",NULL,cred
155 auto_ptr<MessageDecoder> decoder(
156 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
157 samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
160 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
163 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
164 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
165 TSM_ASSERT("Message was not verified.", policy.isSecure());
166 auto_ptr_char entityID(policy.getIssuer()->getName());
167 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
168 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
172 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
174 catch (XMLToolingException& ex) {