Parameterize config namespace for message plugins.
[shibboleth/opensaml2.git] / samltest / saml1 / binding / SAML1ArtifactTest.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/saml1/core/Assertions.h>\r
21 #include <saml/saml1/core/Protocols.h>\r
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>\r
23 #include <xmltooling/validation/ValidatorSuite.h>\r
24 \r
25 using namespace opensaml::saml1p;\r
26 using namespace opensaml::saml1;\r
27 \r
28 class SAML1ArtifactTest : public CxxTest::TestSuite,\r
29         public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {\r
30 public:\r
31     void setUp() {\r
32         SAMLBindingBaseTestCase::setUp();\r
33     }\r
34 \r
35     void tearDown() {\r
36         SAMLBindingBaseTestCase::tearDown();\r
37     }\r
38 \r
39     void testSAML1Artifact() {\r
40         try {\r
41             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
42             SecurityPolicy policy(m_rules1, m_metadata, &idprole, m_trust, false);\r
43 \r
44             // Read message to use from file.\r
45             string path = data_path + "saml1/binding/SAML1Assertion.xml";\r
46             ifstream in(path.c_str());\r
47             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
48             XercesJanitor<DOMDocument> janitor(doc);\r
49             auto_ptr<saml1::Assertion> toSend(\r
50                 dynamic_cast<saml1::Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
51                 );\r
52             janitor.release();\r
53 \r
54             CredentialCriteria cc;\r
55             cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
56             Locker clocker(m_creds);\r
57             const Credential* cred = m_creds->resolve(&cc);\r
58             TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
59 \r
60             // Encode message.\r
61             auto_ptr<MessageEncoder> encoder(\r
62                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(\r
63                     samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)\r
64                     )\r
65                 );\r
66             Locker locker(m_metadata);\r
67             encoder->encode(\r
68                 *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",this,cred\r
69                 );\r
70             toSend.release();\r
71             \r
72             // Decode message.\r
73             string relayState;\r
74             auto_ptr<MessageDecoder> decoder(\r
75                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(\r
76                     samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)\r
77                     )\r
78                 );\r
79             decoder->setArtifactResolver(this);\r
80             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));\r
81             \r
82             // Test the results.\r
83             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
84             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
85             TSM_ASSERT("Message was not verified.", policy.isSecure());\r
86             auto_ptr_char entityID(policy.getIssuer()->getName());\r
87             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
88             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
89 \r
90             // Trigger a replay.\r
91             policy.reset();\r
92             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);\r
93         }\r
94         catch (XMLToolingException& ex) {\r
95             TS_TRACE(ex.what());\r
96             throw;\r
97         }\r
98     }\r
99 \r
100     SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {\r
101         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));\r
102     }\r
103     \r
104     saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {\r
105         throw BindingException("Not implemented.");\r
106     }\r
107     \r
108     Response* resolve(\r
109         const vector<SAMLArtifact*>& artifacts,\r
110         const IDPSSODescriptor& idpDescriptor,\r
111         SecurityPolicy& policy\r
112         ) const {\r
113         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);\r
114         XMLObject* xmlObject =\r
115             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");\r
116         saml1::Assertion* assertion = dynamic_cast<saml1::Assertion*>(xmlObject);\r
117         TSM_ASSERT("Not an assertion.", assertion!=NULL);\r
118         auto_ptr<Response> response(ResponseBuilder::buildResponse());\r
119         response->getAssertions().push_back(assertion);\r
120         Status* status = StatusBuilder::buildStatus();\r
121         response->setStatus(status);\r
122         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
123         status->setStatusCode(sc);\r
124         sc->setValue(&StatusCode::SUCCESS);\r
125         response->setSignature(SignatureBuilder::buildSignature());\r
126         vector<Signature*> sigs(1,response->getSignature());\r
127         CredentialCriteria cc;\r
128         cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
129         Locker clocker(m_creds);\r
130         const Credential* cred = m_creds->resolve(&cc);\r
131         TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
132         response->marshall((DOMDocument*)NULL,&sigs,cred);\r
133         SchemaValidators.validate(response.get());\r
134         policy.evaluate(*(response.get()), this);\r
135         return response.release();\r
136     }\r
137 \r
138     saml2p::ArtifactResponse* resolve(\r
139         const saml2p::SAML2Artifact& artifact,\r
140         const SSODescriptorType& ssoDescriptor,\r
141         SecurityPolicy& policy\r
142         ) const {\r
143         throw BindingException("Not implemented.");\r
144     }\r
145 };\r