Parameterize config namespace for message plugins.
[shibboleth/opensaml2.git] / samltest / saml2 / binding / SAML2ArtifactTest.h
1 /*
2  *  Copyright 2001-2007 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             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
41             SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);
42
43             // Read message to use from file.
44             string path = data_path + "saml2/binding/SAML2Response.xml";
45             ifstream in(path.c_str());
46             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
47             XercesJanitor<DOMDocument> janitor(doc);
48             auto_ptr<Response> toSend(
49                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
50                 );
51             janitor.release();
52
53             CredentialCriteria cc;
54             cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
55             Locker clocker(m_creds);
56             const Credential* cred = m_creds->resolve(&cc);
57             TSM_ASSERT("Retrieved credential was null", cred!=NULL);
58
59             // Freshen timestamp.
60             toSend->setIssueInstant(time(NULL));
61
62             // Encode message.
63             auto_ptr<MessageEncoder> encoder(
64                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
65                     samlconstants::SAML20_BINDING_HTTP_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
66                     )
67                 );
68             Locker locker(m_metadata);
69             encoder->encode(
70                 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",this,cred
71                 );
72             toSend.release();
73             
74             // Decode message.
75             string relayState;
76             auto_ptr<MessageDecoder> decoder(
77                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
78                     samlconstants::SAML20_BINDING_HTTP_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
79                     )
80                 );
81             decoder->setArtifactResolver(this);
82             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
83             
84             // Test the results.
85             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
86             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
87             TSM_ASSERT("Message was not verified.", policy.isSecure());
88             auto_ptr_char entityID(policy.getIssuer()->getName());
89             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
90             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
91
92             // Trigger a replay.
93             policy.reset();
94             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);
95         }
96         catch (XMLToolingException& ex) {
97             TS_TRACE(ex.what());
98             throw;
99         }
100     }
101     
102     SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {
103         throw BindingException("Not implemented.");
104     }
105     
106     saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {
107         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);
108     }
109     
110     saml1p::Response* resolve(
111         const vector<SAMLArtifact*>& artifacts,
112         const IDPSSODescriptor& idpDescriptor,
113         SecurityPolicy& policy
114         ) const {
115         throw BindingException("Not implemented.");
116     }
117
118     ArtifactResponse* resolve(
119         const SAML2Artifact& artifact,
120         const SSODescriptorType& ssoDescriptor,
121         SecurityPolicy& policy
122         ) const {
123         XMLObject* xmlObject =
124             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");
125         Response* payload = dynamic_cast<Response*>(xmlObject);
126         TSM_ASSERT("Not a response.", payload!=NULL);
127
128         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());
129         response->setPayload(payload);
130         Status* status = StatusBuilder::buildStatus();
131         response->setStatus(status);
132         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
133         status->setStatusCode(sc);
134         sc->setValue(StatusCode::SUCCESS);
135         response->marshall();
136         SchemaValidators.validate(response.get());
137         policy.evaluate(*(response.get()), this);
138         return response.release();
139     }
140 };