2 * Copyright 2001-2005 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/validation/ValidatorSuite.h>
25 using namespace opensaml::saml1p;
26 using namespace opensaml::saml1;
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,
41 public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
44 SAMLBindingBaseTestCase::setUp();
48 SAMLBindingBaseTestCase::tearDown();
51 void testSAML1Artifact() {
53 // Read message to use from file.
54 string path = data_path + "saml1/binding/SAML1Assertion.xml";
55 ifstream in(path.c_str());
56 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
57 XercesJanitor<DOMDocument> janitor(doc);
58 auto_ptr<Assertion> toSend(
59 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
64 auto_ptr<MessageEncoder> encoder(
65 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
67 encoder->setArtifactGenerator(this);
68 encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/Artifact","https://sp.example.org/","state",m_creds);
73 const RoleDescriptor* issuer=NULL;
74 const XMLCh* securityMech=NULL;
75 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
76 auto_ptr<MessageDecoder> decoder(
77 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
79 decoder->setArtifactResolver(this);
80 Locker locker(m_metadata);
81 auto_ptr<Response> response(
82 dynamic_cast<Response*>(
83 decoder->decode(relayState,issuer,securityMech,*this,m_metadata,&idprole,m_trust)
88 TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
89 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
90 TSM_ASSERT("Message was not verified.", issuer && securityMech && securityMech==samlconstants::SAML1P_NS);
91 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
92 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
93 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
96 TSM_ASSERT_THROWS("Did not catch the replay.",
97 decoder->decode(relayState,issuer,securityMech,*this,m_metadata,&idprole,m_trust),
100 catch (XMLToolingException& ex) {
106 SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
107 return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
110 saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
111 throw BindingException("Not implemented.");
114 Signature* buildSignature(const CredentialResolver* credResolver) const
\r
116 // Build a Signature.
\r
117 Signature* sig = SignatureBuilder::buildSignature();
\r
118 sig->setSigningKey(credResolver->getKey());
\r
121 const vector<XSECCryptoX509*>& certs = credResolver->getCertificates();
\r
122 if (!certs.empty()) {
\r
123 KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
\r
124 X509Data* x509Data=X509DataBuilder::buildX509Data();
\r
125 keyInfo->getX509Datas().push_back(x509Data);
\r
126 for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data));
\r
127 sig->setKeyInfo(keyInfo);
\r
134 const XMLCh*& securityMech,
135 const vector<SAMLArtifact*>& artifacts,
136 const IDPSSODescriptor& idpDescriptor,
137 const X509TrustEngine* trustEngine=NULL
139 TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
140 XMLObject* xmlObject =
141 SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
142 Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
143 TSM_ASSERT("Not an assertion.", assertion!=NULL);
144 auto_ptr<Response> response(ResponseBuilder::buildResponse());
145 response->getAssertions().push_back(assertion);
146 Status* status = StatusBuilder::buildStatus();
147 response->setStatus(status);
148 StatusCode* sc = StatusCodeBuilder::buildStatusCode();
149 status->setStatusCode(sc);
150 sc->setValue(&StatusCode::SUCCESS);
151 response->setSignature(buildSignature(m_creds));
152 vector<Signature*> sigs(1,response->getSignature());
153 response->marshall((DOMDocument*)NULL,&sigs);
154 SchemaValidators.validate(response.get());
156 return response.release();
159 saml2p::ArtifactResponse* resolve(
160 const XMLCh*& securityMech,
161 const saml2p::SAML2Artifact& artifact,
162 const SSODescriptorType& ssoDescriptor,
163 const X509TrustEngine* trustEngine=NULL
165 throw BindingException("Not implemented.");