Parameterize config namespace for message plugins.
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1ArtifactTest.h
index decc34f..8503a64 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- *  Copyright 2001-2005 Internet2\r
+ *  Copyright 2001-2007 Internet2\r
  * \r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
 #include "binding.h"\r
 \r
 #include <saml/binding/ArtifactMap.h>\r
+#include <saml/saml1/core/Assertions.h>\r
 #include <saml/saml1/core/Protocols.h>\r
 #include <saml/saml1/binding/SAMLArtifactType0001.h>\r
+#include <xmltooling/validation/ValidatorSuite.h>\r
 \r
 using namespace opensaml::saml1p;\r
 using namespace opensaml::saml1;\r
 \r
 class SAML1ArtifactTest : public CxxTest::TestSuite,\r
-    public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {\r
+        public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {\r
 public:\r
     void setUp() {\r
-        m_fields.clear();\r
         SAMLBindingBaseTestCase::setUp();\r
     }\r
 \r
     void tearDown() {\r
-        m_fields.clear();\r
         SAMLBindingBaseTestCase::tearDown();\r
     }\r
 \r
     void testSAML1Artifact() {\r
         try {\r
+            QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
+            SecurityPolicy policy(m_rules1, m_metadata, &idprole, m_trust, false);\r
+\r
             // Read message to use from file.\r
             string path = data_path + "saml1/binding/SAML1Assertion.xml";\r
             ifstream in(path.c_str());\r
             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
             XercesJanitor<DOMDocument> janitor(doc);\r
-            auto_ptr<Assertion> toSend(\r
-                dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
+            auto_ptr<saml1::Assertion> toSend(\r
+                dynamic_cast<saml1::Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
                 );\r
             janitor.release();\r
 \r
+            CredentialCriteria cc;\r
+            cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
+            Locker clocker(m_creds);\r
+            const Credential* cred = m_creds->resolve(&cc);\r
+            TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
+\r
             // Encode message.\r
-            auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_ARTIFACT_ENCODER, NULL));\r
-            encoder->setArtifactGenerator(this);\r
-            encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);\r
+            auto_ptr<MessageEncoder> encoder(\r
+                SAMLConfig::getConfig().MessageEncoderManager.newPlugin(\r
+                    samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)\r
+                    )\r
+                );\r
+            Locker locker(m_metadata);\r
+            encoder->encode(\r
+                *this,toSend.get(),"https://sp.example.org/SAML/SSO",m_metadata->getEntityDescriptor("https://sp.example.org/"),"state",this,cred\r
+                );\r
             toSend.release();\r
             \r
             // Decode message.\r
             string relayState;\r
-            const RoleDescriptor* issuer=NULL;\r
-            bool trusted=false;\r
-            QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
-            auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_ARTIFACT_DECODER, NULL));\r
-            decoder->setArtifactResolver(this);\r
-            Locker locker(m_metadata);\r
-            auto_ptr<Response> response(\r
-                dynamic_cast<Response*>(\r
-                    decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)\r
+            auto_ptr<MessageDecoder> decoder(\r
+                SAMLConfig::getConfig().MessageDecoderManager.newPlugin(\r
+                    samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)\r
                     )\r
                 );\r
+            decoder->setArtifactResolver(this);\r
+            auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));\r
             \r
             // Test the results.\r
             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
-            TSM_ASSERT("Message was not verified.", issuer && trusted);\r
-            auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
+            TSM_ASSERT("Message was not verified.", policy.isSecure());\r
+            auto_ptr_char entityID(policy.getIssuer()->getName());\r
             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
 \r
             // Trigger a replay.\r
-            TSM_ASSERT_THROWS("Did not catch the replay.", \r
-                decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),\r
-                BindingException);\r
+            policy.reset();\r
+            TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);\r
         }\r
         catch (XMLToolingException& ex) {\r
             TS_TRACE(ex.what());\r
@@ -87,36 +97,23 @@ public:
         }\r
     }\r
 \r
-    const char* getMethod() const {\r
-        return "GET";\r
-    } \r
-\r
-    const char* getRequestURL() const {\r
-        return "https://sp.example.org/SAML/Artifact";\r
-    }\r
-    \r
-    const char* getQueryString() const {\r
-        return NULL;\r
-    }\r
-    \r
-    SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
+    SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {\r
         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));\r
     }\r
     \r
-    saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
+    saml2p::SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {\r
         throw BindingException("Not implemented.");\r
     }\r
     \r
     Response* resolve(\r
-        bool& authenticated,\r
         const vector<SAMLArtifact*>& artifacts,\r
         const IDPSSODescriptor& idpDescriptor,\r
-        const X509TrustEngine* trustEngine=NULL\r
+        SecurityPolicy& policy\r
         ) const {\r
         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);\r
         XMLObject* xmlObject =\r
             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");\r
-        Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);\r
+        saml1::Assertion* assertion = dynamic_cast<saml1::Assertion*>(xmlObject);\r
         TSM_ASSERT("Not an assertion.", assertion!=NULL);\r
         auto_ptr<Response> response(ResponseBuilder::buildResponse());\r
         response->getAssertions().push_back(assertion);\r
@@ -125,17 +122,23 @@ public:
         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
         status->setStatusCode(sc);\r
         sc->setValue(&StatusCode::SUCCESS);\r
-        response->marshall();\r
+        response->setSignature(SignatureBuilder::buildSignature());\r
+        vector<Signature*> sigs(1,response->getSignature());\r
+        CredentialCriteria cc;\r
+        cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
+        Locker clocker(m_creds);\r
+        const Credential* cred = m_creds->resolve(&cc);\r
+        TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
+        response->marshall((DOMDocument*)NULL,&sigs,cred);\r
         SchemaValidators.validate(response.get());\r
-        authenticated = true;\r
+        policy.evaluate(*(response.get()), this);\r
         return response.release();\r
     }\r
 \r
-    XMLObject* resolve(\r
-        bool& authenticated,\r
+    saml2p::ArtifactResponse* resolve(\r
         const saml2p::SAML2Artifact& artifact,\r
         const SSODescriptorType& ssoDescriptor,\r
-        const X509TrustEngine* trustEngine=NULL\r
+        SecurityPolicy& policy\r
         ) const {\r
         throw BindingException("Not implemented.");\r
     }\r