Switch encoders to metadata-based recipient parameter.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2ArtifactTest.h
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "binding.h"\r
18 \r
19 #include <saml/binding/ArtifactMap.h>\r
20 #include <saml/saml2/core/Protocols.h>\r
21 #include <saml/saml2/binding/SAML2ArtifactType0004.h>\r
22 #include <xmltooling/validation/ValidatorSuite.h>\r
23 \r
24 using namespace opensaml::saml2p;\r
25 using namespace opensaml::saml2;\r
26 \r
27 class SAML2ArtifactTest : public CxxTest::TestSuite,\r
28         public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {\r
29 public:\r
30     void setUp() {\r
31         SAMLBindingBaseTestCase::setUp();\r
32     }\r
33 \r
34     void tearDown() {\r
35         SAMLBindingBaseTestCase::tearDown();\r
36     }\r
37 \r
38     void testSAML2Artifact() {\r
39         try {\r
40             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
41             SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);\r
42 \r
43             // Read message to use from file.\r
44             string path = data_path + "saml2/binding/SAML2Response.xml";\r
45             ifstream in(path.c_str());\r
46             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
47             XercesJanitor<DOMDocument> janitor(doc);\r
48             auto_ptr<Response> toSend(\r
49                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
50                 );\r
51             janitor.release();\r
52 \r
53             CredentialCriteria cc;\r
54             cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
55             Locker clocker(m_creds);\r
56             const Credential* cred = m_creds->resolve(&cc);\r
57             TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
58 \r
59             // Freshen timestamp.\r
60             toSend->setIssueInstant(time(NULL));\r
61 \r
62             // Encode message.\r
63             auto_ptr<MessageEncoder> encoder(\r
64                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)\r
65                 );\r
66             encoder->setArtifactGenerator(this);\r
67             Locker locker(m_metadata);
68             encoder->encode(
69                 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",cred
70                 );
71             toSend.release();\r
72             \r
73             // Decode message.\r
74             string relayState;\r
75             auto_ptr<MessageDecoder> decoder(\r
76                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)\r
77                 );\r
78             decoder->setArtifactResolver(this);\r
79             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));\r
80             \r
81             // Test the results.\r
82             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");\r
83             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
84             TSM_ASSERT("Message was not verified.", policy.isSecure());\r
85             auto_ptr_char entityID(policy.getIssuer()->getName());\r
86             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
87             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
88 \r
89             // Trigger a replay.\r
90             policy.reset();\r
91             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);\r
92         }\r
93         catch (XMLToolingException& ex) {\r
94             TS_TRACE(ex.what());\r
95             throw;\r
96         }\r
97     }\r
98     \r
99     SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {\r
100         throw BindingException("Not implemented.");\r
101     }\r
102     \r
103     saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {\r
104         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);\r
105     }\r
106     \r
107     saml1p::Response* resolve(\r
108         const vector<SAMLArtifact*>& artifacts,\r
109         const IDPSSODescriptor& idpDescriptor,\r
110         SecurityPolicy& policy\r
111         ) const {\r
112         throw BindingException("Not implemented.");\r
113     }\r
114 \r
115     ArtifactResponse* resolve(\r
116         const SAML2Artifact& artifact,\r
117         const SSODescriptorType& ssoDescriptor,\r
118         SecurityPolicy& policy\r
119         ) const {\r
120         XMLObject* xmlObject =\r
121             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");\r
122         Response* payload = dynamic_cast<Response*>(xmlObject);\r
123         TSM_ASSERT("Not a response.", payload!=NULL);\r
124 \r
125         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());\r
126         response->setPayload(payload);\r
127         Status* status = StatusBuilder::buildStatus();\r
128         response->setStatus(status);\r
129         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
130         status->setStatusCode(sc);\r
131         sc->setValue(StatusCode::SUCCESS);\r
132         response->marshall();\r
133         SchemaValidators.validate(response.get());\r
134         policy.evaluate(*(response.get()), this);\r
135         return response.release();\r
136     }\r
137 };\r