2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 #include "binding.h"
\r
19 #include <saml/binding/ArtifactMap.h>
\r
20 #include <saml/saml1/core/Assertions.h>
\r
21 #include <saml/saml1/core/Protocols.h>
\r
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>
\r
23 #include <xmltooling/validation/ValidatorSuite.h>
\r
25 using namespace opensaml::saml1p;
\r
26 using namespace opensaml::saml1;
\r
29 class SAML_DLLLOCAL _addcert : public binary_function<X509Data*,XSECCryptoX509*,void> {
\r
31 void operator()(X509Data* bag, XSECCryptoX509* cert) const {
\r
32 safeBuffer& buf=cert->getDEREncodingSB();
\r
33 X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
\r
34 x->setValue(buf.sbStrToXMLCh());
\r
35 bag->getX509Certificates().push_back(x);
\r
40 class SAML1ArtifactTest : public CxxTest::TestSuite,
\r
41 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
\r
44 SAMLBindingBaseTestCase::setUp();
\r
48 SAMLBindingBaseTestCase::tearDown();
\r
51 void testSAML1Artifact() {
\r
53 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
\r
54 SecurityPolicy policy(m_rules1, m_metadata, &idprole, m_trust);
\r
56 // Read message to use from file.
\r
57 string path = data_path + "saml1/binding/SAML1Assertion.xml";
\r
58 ifstream in(path.c_str());
\r
59 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
60 XercesJanitor<DOMDocument> janitor(doc);
\r
61 auto_ptr<Assertion> toSend(
\r
62 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
\r
67 auto_ptr<MessageEncoder> encoder(
\r
68 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
\r
70 encoder->setArtifactGenerator(this);
\r
71 encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",m_creds);
\r
76 auto_ptr<MessageDecoder> decoder(
\r
77 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
\r
79 decoder->setArtifactResolver(this);
\r
80 Locker locker(m_metadata);
\r
81 auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
\r
83 // Test the results.
\r
84 TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
\r
85 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
\r
86 TSM_ASSERT("Message was not verified.", policy.isSecure());
\r
87 auto_ptr_char entityID(policy.getIssuer()->getName());
\r
88 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
\r
89 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
\r
91 // Trigger a replay.
\r
93 TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);
\r
95 catch (XMLToolingException& ex) {
\r
96 TS_TRACE(ex.what());
\r
101 SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
\r
102 return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
\r
105 saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
\r
106 throw BindingException("Not implemented.");
\r
109 Signature* buildSignature(const CredentialResolver* credResolver) const
\r
111 // Build a Signature.
\r
112 Signature* sig = SignatureBuilder::buildSignature();
\r
113 sig->setSigningKey(credResolver->getKey());
\r
116 const vector<XSECCryptoX509*>& certs = credResolver->getCertificates();
\r
117 if (!certs.empty()) {
\r
118 KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
\r
119 X509Data* x509Data=X509DataBuilder::buildX509Data();
\r
120 keyInfo->getX509Datas().push_back(x509Data);
\r
121 for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data));
\r
122 sig->setKeyInfo(keyInfo);
\r
129 const vector<SAMLArtifact*>& artifacts,
\r
130 const IDPSSODescriptor& idpDescriptor,
\r
131 SecurityPolicy& policy
\r
133 TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
\r
134 XMLObject* xmlObject =
\r
135 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
\r
136 Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
\r
137 TSM_ASSERT("Not an assertion.", assertion!=NULL);
\r
138 auto_ptr<Response> response(ResponseBuilder::buildResponse());
\r
139 response->getAssertions().push_back(assertion);
\r
140 Status* status = StatusBuilder::buildStatus();
\r
141 response->setStatus(status);
\r
142 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
\r
143 status->setStatusCode(sc);
\r
144 sc->setValue(&StatusCode::SUCCESS);
\r
145 response->setSignature(buildSignature(m_creds));
\r
146 vector<Signature*> sigs(1,response->getSignature());
\r
147 response->marshall((DOMDocument*)NULL,&sigs);
\r
148 SchemaValidators.validate(response.get());
\r
149 return response.release();
\r
152 saml2p::ArtifactResponse* resolve(
\r
153 const saml2p::SAML2Artifact& artifact,
\r
154 const SSODescriptorType& ssoDescriptor,
\r
155 SecurityPolicy& policy
\r
157 throw BindingException("Not implemented.");
\r