Major revamp of credential and trust handling code, PKIX engine still needs work.
[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             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",cred);\r
68             toSend.release();\r
69             \r
70             // Decode message.\r
71             string relayState;\r
72             auto_ptr<MessageDecoder> decoder(\r
73                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)\r
74                 );\r
75             decoder->setArtifactResolver(this);\r
76             Locker locker(m_metadata);\r
77             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));\r
78             \r
79             // Test the results.\r
80             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");\r
81             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
82             TSM_ASSERT("Message was not verified.", policy.isSecure());\r
83             auto_ptr_char entityID(policy.getIssuer()->getName());\r
84             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
85             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
86 \r
87             // Trigger a replay.\r
88             policy.reset();\r
89             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);\r
90         }\r
91         catch (XMLToolingException& ex) {\r
92             TS_TRACE(ex.what());\r
93             throw;\r
94         }\r
95     }\r
96     \r
97     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
98         throw BindingException("Not implemented.");\r
99     }\r
100     \r
101     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
102         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);\r
103     }\r
104     \r
105     saml1p::Response* resolve(\r
106         const vector<SAMLArtifact*>& artifacts,\r
107         const IDPSSODescriptor& idpDescriptor,\r
108         SecurityPolicy& policy\r
109         ) const {\r
110         throw BindingException("Not implemented.");\r
111     }\r
112 \r
113     ArtifactResponse* resolve(\r
114         const SAML2Artifact& artifact,\r
115         const SSODescriptorType& ssoDescriptor,\r
116         SecurityPolicy& policy\r
117         ) const {\r
118         XMLObject* xmlObject =\r
119             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");\r
120         Response* payload = dynamic_cast<Response*>(xmlObject);\r
121         TSM_ASSERT("Not a response.", payload!=NULL);\r
122         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());\r
123         response->setPayload(payload);\r
124         Status* status = StatusBuilder::buildStatus();\r
125         response->setStatus(status);\r
126         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
127         status->setStatusCode(sc);\r
128         sc->setValue(StatusCode::SUCCESS);\r
129         response->marshall();\r
130         SchemaValidators.validate(response.get());\r
131         policy.evaluate(*(response.get()), this);\r
132         return response.release();\r
133     }\r
134 };\r