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