c8764cdaeaa921d14210fcab948c6193a6015c5c
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2ArtifactTest.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/saml2/core/Protocols.h>
21 #include <saml/saml2/binding/SAML2ArtifactType0004.h>
22 #include <xmltooling/validation/ValidatorSuite.h>
23
24 using namespace opensaml::saml2p;
25 using namespace opensaml::saml2;
26
27 class SAML2ArtifactTest : public CxxTest::TestSuite,
28     public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
29 public:
30     void setUp() {
31         SAMLBindingBaseTestCase::setUp();
32     }
33
34     void tearDown() {
35         SAMLBindingBaseTestCase::tearDown();
36     }
37
38     void testSAML2Artifact() {
39         try {
40             // Read message to use from file.
41             string path = data_path + "saml2/binding/SAML2Response.xml";
42             ifstream in(path.c_str());
43             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44             XercesJanitor<DOMDocument> janitor(doc);
45             auto_ptr<Response> toSend(
46                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
47                 );
48             janitor.release();
49
50             // Freshen timestamp.
51             toSend->setIssueInstant(time(NULL));
52
53             // Encode message.
54             auto_ptr<MessageEncoder> encoder(
55                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)
56                 );
57             encoder->setArtifactGenerator(this);
58             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/Artifact","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::SAML20_BINDING_HTTP_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("RelayState 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     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
97         throw BindingException("Not implemented.");
98     }
99     
100     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
101         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);
102     }
103     
104     saml1p::Response* resolve(
105         bool& authenticated,
106         const vector<SAMLArtifact*>& artifacts,
107         const IDPSSODescriptor& idpDescriptor,
108         const X509TrustEngine* trustEngine=NULL
109         ) const {
110         throw BindingException("Not implemented.");
111     }
112
113     ArtifactResponse* resolve(
114         bool& authenticated,
115         const SAML2Artifact& artifact,
116         const SSODescriptorType& ssoDescriptor,
117         const X509TrustEngine* trustEngine=NULL
118         ) const {
119         XMLObject* xmlObject =
120             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");
121         Response* payload = dynamic_cast<Response*>(xmlObject);
122         TSM_ASSERT("Not a response.", payload!=NULL);
123         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());
124         response->setPayload(payload);
125         Status* status = StatusBuilder::buildStatus();
126         response->setStatus(status);
127         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
128         status->setStatusCode(sc);
129         sc->setValue(StatusCode::SUCCESS);
130         response->marshall();
131         SchemaValidators.validate(response.get());
132         authenticated = true;
133         return response.release();
134     }
135 };