a133cf1faa912feee318fb9564120c0516e33f51
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1ArtifactTest.h
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "binding.h"
18
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>
24
25 using namespace opensaml::saml1p;
26 using namespace opensaml::saml1;
27
28 class SAML1ArtifactTest : public CxxTest::TestSuite,
29     public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
30 public:
31     void setUp() {
32         m_fields.clear();
33         SAMLBindingBaseTestCase::setUp();
34     }
35
36     void tearDown() {
37         m_fields.clear();
38         SAMLBindingBaseTestCase::tearDown();
39     }
40
41     void testSAML1Artifact() {
42         try {
43             // Read message to use from file.
44             string path = data_path + "saml1/binding/SAML1Assertion.xml";
45             ifstream in(path.c_str());
46             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
47             XercesJanitor<DOMDocument> janitor(doc);
48             auto_ptr<Assertion> toSend(
49                 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
50                 );
51             janitor.release();
52
53             // Encode message.
54             auto_ptr<MessageEncoder> encoder(
55                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
56                 );
57             encoder->setArtifactGenerator(this);
58             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
59             toSend.release();
60             
61             // Decode message.
62             string relayState;
63             const RoleDescriptor* issuer=NULL;
64             bool trusted=false;
65             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
66             auto_ptr<MessageDecoder> decoder(
67                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
68                 );
69             decoder->setArtifactResolver(this);
70             Locker locker(m_metadata);
71             auto_ptr<Response> response(
72                 dynamic_cast<Response*>(
73                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
74                     )
75                 );
76             
77             // Test the results.
78             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
79             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
80             TSM_ASSERT("Message was not verified.", issuer && trusted);
81             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
82             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
83             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
84
85             // Trigger a replay.
86             TSM_ASSERT_THROWS("Did not catch the replay.", 
87                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
88                 BindingException);
89         }
90         catch (XMLToolingException& ex) {
91             TS_TRACE(ex.what());
92             throw;
93         }
94     }
95
96     const char* getMethod() const {
97         return "GET";
98     } 
99
100     const char* getRequestURL() const {
101         return "https://sp.example.org/SAML/Artifact";
102     }
103     
104     const char* getQueryString() const {
105         return NULL;
106     }
107     
108     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
109         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
110     }
111     
112     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
113         throw BindingException("Not implemented.");
114     }
115     
116     Response* resolve(
117         bool& authenticated,
118         const vector<SAMLArtifact*>& artifacts,
119         const IDPSSODescriptor& idpDescriptor,
120         const X509TrustEngine* trustEngine=NULL
121         ) const {
122         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
123         XMLObject* xmlObject =
124             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
125         Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
126         TSM_ASSERT("Not an assertion.", assertion!=NULL);
127         auto_ptr<Response> response(ResponseBuilder::buildResponse());
128         response->getAssertions().push_back(assertion);
129         Status* status = StatusBuilder::buildStatus();
130         response->setStatus(status);
131         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
132         status->setStatusCode(sc);
133         sc->setValue(&StatusCode::SUCCESS);
134         response->marshall();
135         SchemaValidators.validate(response.get());
136         authenticated = true;
137         return response.release();
138     }
139
140     saml2p::ArtifactResponse* resolve(
141         bool& authenticated,
142         const saml2p::SAML2Artifact& artifact,
143         const SSODescriptorType& ssoDescriptor,
144         const X509TrustEngine* trustEngine=NULL
145         ) const {
146         throw BindingException("Not implemented.");
147     }
148 };