2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
23 #include <saml/binding/ArtifactMap.h>
24 #include <saml/saml2/core/Protocols.h>
25 #include <saml/saml2/binding/SAML2ArtifactType0004.h>
26 #include <xmltooling/security/SecurityHelper.h>
27 #include <xmltooling/validation/ValidatorSuite.h>
29 using namespace opensaml::saml2p;
30 using namespace opensaml::saml2;
32 class SAML2ArtifactTest : public CxxTest::TestSuite,
33 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
36 SAMLBindingBaseTestCase::setUp();
40 SAMLBindingBaseTestCase::tearDown();
43 void testSAML2Artifact() {
45 xmltooling::QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
46 SecurityPolicy policy(m_metadata.get(), &idprole, m_trust.get(), false);
47 policy.getRules().assign(m_rules.begin(), m_rules.end());
49 // Read message to use from file.
50 string path = data_path + "saml2/binding/SAML2Response.xml";
51 ifstream in(path.c_str());
52 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
53 XercesJanitor<DOMDocument> janitor(doc);
54 auto_ptr<Response> toSend(
55 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
59 CredentialCriteria cc;
60 cc.setUsage(Credential::SIGNING_CREDENTIAL);
61 Locker clocker(m_creds.get());
62 const Credential* cred = m_creds->resolve(&cc);
63 TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
66 toSend->setIssueInstant(time(nullptr));
69 boost::scoped_ptr<MessageEncoder> encoder(
70 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
71 samlconstants::SAML20_BINDING_HTTP_ARTIFACT, pair<const DOMElement*,const XMLCh*>(nullptr,nullptr)
74 Locker locker(m_metadata.get());
78 "https://sp.example.org/SAML/SSO",
79 m_metadata->getEntityDescriptor(MetadataProvider::Criteria("https://sp.example.org/")).first,
88 boost::scoped_ptr<MessageDecoder> decoder(
89 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
90 samlconstants::SAML20_BINDING_HTTP_ARTIFACT, pair<const DOMElement*,const XMLCh*>(nullptr,nullptr)
93 decoder->setArtifactResolver(this);
94 boost::scoped_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
97 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
98 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
99 TSM_ASSERT("Message was not verified.", policy.isAuthenticated());
100 auto_ptr_char entityID(policy.getIssuer()->getName());
101 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
102 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
106 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);
108 catch (XMLToolingException& ex) {
114 SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {
115 throw BindingException("Not implemented.");
118 saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {
119 static const char* providerIdStr = "https://idp.example.org/";
120 return new SAML2ArtifactType0004(
121 SecurityHelper::doHash("SHA1", providerIdStr, strlen(providerIdStr), false), 1
125 saml1p::Response* resolve(
126 const vector<SAMLArtifact*>& artifacts,
127 const IDPSSODescriptor& idpDescriptor,
128 SecurityPolicy& policy
130 throw BindingException("Not implemented.");
133 ArtifactResponse* resolve(
134 const SAML2Artifact& artifact,
135 const SSODescriptorType& ssoDescriptor,
136 SecurityPolicy& policy
138 XMLObject* xmlObject =
139 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");
140 Response* payload = dynamic_cast<Response*>(xmlObject);
141 TSM_ASSERT("Not a response.", payload!=nullptr);
143 auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());
144 response->setPayload(payload);
145 saml2p::Status* status = StatusBuilder::buildStatus();
146 response->setStatus(status);
147 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
148 status->setStatusCode(sc);
149 sc->setValue(StatusCode::SUCCESS);
150 response->marshall();
151 SchemaValidators.validate(response.get());
152 policy.evaluate(*(response.get()), this);
153 return response.release();