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