2 * Copyright 2001-2009 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/binding/ArtifactMap.h>
20 #include <saml/saml1/core/Assertions.h>
21 #include <saml/saml1/core/Protocols.h>
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>
23 #include <xmltooling/security/SecurityHelper.h>
24 #include <xmltooling/signature/Signature.h>
25 #include <xmltooling/validation/ValidatorSuite.h>
27 using namespace opensaml::saml1p;
28 using namespace opensaml::saml1;
30 class SAML1ArtifactTest : public CxxTest::TestSuite,
31 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
34 SAMLBindingBaseTestCase::setUp();
38 SAMLBindingBaseTestCase::tearDown();
41 void testSAML1Artifact() {
43 xmltooling::QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
44 SecurityPolicy policy(m_metadata, &idprole, m_trust, false);
45 policy.getRules().assign(m_rules.begin(), m_rules.end());
47 // Read message to use from file.
48 string path = data_path + "saml1/binding/SAML1Assertion.xml";
49 ifstream in(path.c_str());
50 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
51 XercesJanitor<DOMDocument> janitor(doc);
52 auto_ptr<saml1::Assertion> toSend(
53 dynamic_cast<saml1::Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
57 CredentialCriteria cc;
58 cc.setUsage(Credential::SIGNING_CREDENTIAL);
59 Locker clocker(m_creds);
60 const Credential* cred = m_creds->resolve(&cc);
61 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
64 auto_ptr<MessageEncoder> encoder(
65 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
66 samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
69 Locker locker(m_metadata);
73 "https://sp.example.org/SAML/SSO",
74 m_metadata->getEntityDescriptor(MetadataProvider::Criteria("https://sp.example.org/")).first,
83 auto_ptr<MessageDecoder> decoder(
84 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
85 samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
88 decoder->setArtifactResolver(this);
89 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
92 TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
93 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
94 TSM_ASSERT("Message was not verified.", policy.isAuthenticated());
95 auto_ptr_char entityID(policy.getIssuer()->getName());
96 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
97 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
101 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);
103 catch (XMLToolingException& ex) {
109 SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {
110 static const char* providerIdStr = "https://idp.example.org/";
111 return new SAMLArtifactType0001(
112 SecurityHelper::doHash("SHA1", providerIdStr, strlen(providerIdStr), false)
116 saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {
117 throw BindingException("Not implemented.");
121 const vector<SAMLArtifact*>& artifacts,
122 const IDPSSODescriptor& idpDescriptor,
123 SecurityPolicy& policy
125 TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
126 XMLObject* xmlObject =
127 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
128 saml1::Assertion* assertion = dynamic_cast<saml1::Assertion*>(xmlObject);
129 TSM_ASSERT("Not an assertion.", assertion!=NULL);
130 auto_ptr<Response> response(ResponseBuilder::buildResponse());
131 response->getAssertions().push_back(assertion);
132 Status* status = StatusBuilder::buildStatus();
133 response->setStatus(status);
134 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
135 status->setStatusCode(sc);
136 sc->setValue(&StatusCode::SUCCESS);
137 response->setSignature(SignatureBuilder::buildSignature());
138 vector<Signature*> sigs(1,response->getSignature());
139 CredentialCriteria cc;
140 cc.setUsage(Credential::SIGNING_CREDENTIAL);
141 Locker clocker(m_creds);
142 const Credential* cred = m_creds->resolve(&cc);
143 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
144 response->marshall((DOMDocument*)NULL,&sigs,cred);
145 SchemaValidators.validate(response.get());
146 policy.evaluate(*(response.get()), this);
147 return response.release();
150 saml2p::ArtifactResponse* resolve(
151 const saml2p::SAML2Artifact& artifact,
152 const SSODescriptorType& ssoDescriptor,
153 SecurityPolicy& policy
155 throw BindingException("Not implemented.");