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