518962248599b16e3414375e477bf84490cde982
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1ArtifactTest.h
1 /*
2  *  Copyright 2001-2009 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/saml1/core/Assertions.h>
21 #include <saml/saml1/core/Protocols.h>
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>
23 #include <xmltooling/security/SecurityHelper.h>
24 #include <xmltooling/signature/Signature.h>
25 #include <xmltooling/validation/ValidatorSuite.h>
26
27 using namespace opensaml::saml1p;
28 using namespace opensaml::saml1;
29
30 class SAML1ArtifactTest : public CxxTest::TestSuite,
31         public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
32 public:
33     void setUp() {
34         SAMLBindingBaseTestCase::setUp();
35     }
36
37     void tearDown() {
38         SAMLBindingBaseTestCase::tearDown();
39     }
40
41     void testSAML1Artifact() {
42         try {
43             xmltooling::QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
44             SecurityPolicy policy(m_metadata, &idprole, m_trust, false);
45             policy.getRules().assign(m_rules.begin(), m_rules.end());
46
47             // Read message to use from file.
48             string path = data_path + "saml1/binding/SAML1Assertion.xml";
49             ifstream in(path.c_str());
50             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
51             XercesJanitor<DOMDocument> janitor(doc);
52             auto_ptr<saml1::Assertion> toSend(
53                 dynamic_cast<saml1::Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
54                 );
55             janitor.release();
56
57             CredentialCriteria cc;
58             cc.setUsage(Credential::SIGNING_CREDENTIAL);
59             Locker clocker(m_creds);
60             const Credential* cred = m_creds->resolve(&cc);
61             TSM_ASSERT("Retrieved credential was null", cred!=NULL);
62
63             // Encode message.
64             auto_ptr<MessageEncoder> encoder(
65                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
66                     samlconstants::SAML1_PROFILE_BROWSER_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::SAML1_PROFILE_BROWSER_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("TARGET 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         static const char* providerIdStr = "https://idp.example.org/";
111         return new SAMLArtifactType0001(
112             SecurityHelper::doHash("SHA1", providerIdStr, strlen(providerIdStr), false)
113         );
114     }
115     
116     saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {
117         throw BindingException("Not implemented.");
118     }
119     
120     Response* resolve(
121         const vector<SAMLArtifact*>& artifacts,
122         const IDPSSODescriptor& idpDescriptor,
123         SecurityPolicy& policy
124         ) const {
125         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
126         XMLObject* xmlObject =
127             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
128         saml1::Assertion* assertion = dynamic_cast<saml1::Assertion*>(xmlObject);
129         TSM_ASSERT("Not an assertion.", assertion!=NULL);
130         auto_ptr<Response> response(ResponseBuilder::buildResponse());
131         response->getAssertions().push_back(assertion);
132         Status* status = StatusBuilder::buildStatus();
133         response->setStatus(status);
134         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
135         status->setStatusCode(sc);
136         sc->setValue(&StatusCode::SUCCESS);
137         response->setSignature(SignatureBuilder::buildSignature());
138         vector<Signature*> sigs(1,response->getSignature());
139         CredentialCriteria cc;
140         cc.setUsage(Credential::SIGNING_CREDENTIAL);
141         Locker clocker(m_creds);
142         const Credential* cred = m_creds->resolve(&cc);
143         TSM_ASSERT("Retrieved credential was null", cred!=NULL);
144         response->marshall((DOMDocument*)NULL,&sigs,cred);
145         SchemaValidators.validate(response.get());
146         policy.evaluate(*(response.get()), this);
147         return response.release();
148     }
149
150     saml2p::ArtifactResponse* resolve(
151         const saml2p::SAML2Artifact& artifact,
152         const SSODescriptorType& ssoDescriptor,
153         SecurityPolicy& policy
154         ) const {
155         throw BindingException("Not implemented.");
156     }
157 };