Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / saml / binding / SecurityPolicy.h
index 1870595..5f75c34 100644 (file)
 #ifndef __saml_secpol_h__
 #define __saml_secpol_h__
 
-#include <saml/binding/GenericRequest.h>
+#include <saml/base.h>
 
 #include <ctime>
 #include <vector>
 #include <xmltooling/XMLObject.h>
+#include <xmltooling/io/GenericRequest.h>
 #include <xmltooling/security/TrustEngine.h>
 
 #if defined (_MSC_VER)
@@ -67,33 +68,15 @@ namespace opensaml {
          * @param metadataProvider  locked MetadataProvider instance
          * @param role              identifies the role (generally IdP or SP) of the policy peer 
          * @param trustEngine       TrustEngine to authenticate policy peer
+         * @param validate          true iff XML parsing should be done with validation
          */
         SecurityPolicy(
             const saml2md::MetadataProvider* metadataProvider=NULL,
             const xmltooling::QName* role=NULL,
-            const xmltooling::TrustEngine* trustEngine=NULL
-            ) : m_messageQName(NULL), m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_secure(false),
-                m_matchingPolicy(NULL), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine) {
-            if (role)
-                m_role = new xmltooling::QName(*role);
-        }
-
-        /**
-         * Constructor for policy using existing rules. The lifetime of the policy rules
-         * must be at least as long as the policy object.
-         *
-         * @param rules             reference to array of policy rules to use 
-         * @param metadataProvider  locked MetadataProvider instance
-         * @param role              identifies the role (generally IdP or SP) of the policy peer 
-         * @param trustEngine       TrustEngine to authenticate policy peer
-         */
-        SecurityPolicy(
-            const std::vector<const SecurityPolicyRule*>& rules,
-            const saml2md::MetadataProvider* metadataProvider=NULL,
-            const xmltooling::QName* role=NULL,
-            const xmltooling::TrustEngine* trustEngine=NULL
-            ) : m_messageQName(NULL), m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_secure(false),
-                m_matchingPolicy(NULL), m_rules(rules), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine) {
+            const xmltooling::TrustEngine* trustEngine=NULL,
+            bool validate=true
+            ) : m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_secure(false),
+                m_matchingPolicy(NULL), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine), m_validate(validate) {
             if (role)
                 m_role = new xmltooling::QName(*role);
         }
@@ -128,13 +111,23 @@ namespace opensaml {
         }
 
         /**
-         * Adds a SecurityPolicyRule to the policy. The lifetime of the policy rule
-         * must be at least as long as the policy object.
+         * Returns XML message validation setting.
          * 
-         * @param rule  SecurityPolicyRule to add
+         * @return validation flag
          */
-        void addRule(const SecurityPolicyRule* rule) {
-            m_rules.push_back(rule);
+        bool getValidating() const {
+            return m_validate;
+        } 
+
+        /**
+         * Gets a mutable array of installed policy rules.
+         *
+         * <p>If adding rules, their lifetime must be at least as long as the policy object.
+         * 
+         * @return  mutable array of rules
+         */
+        std::vector<const SecurityPolicyRule*>& getRules() {
+            return m_rules;
         }
 
         /**
@@ -166,31 +159,41 @@ namespace opensaml {
         }
 
         /**
+         * Controls schema validation of incoming XML messages.
+         * This is separate from other forms of programmatic validation of objects,
+         * but can detect a much wider range of syntax errors. 
+         * 
+         * @param validate  validation setting
+         */
+        void setValidating(bool validate=true) {
+            m_validate = validate;
+        }
+        
+        /**
          * Evaluates the policy against the given request and message,
          * possibly populating message information in the policy object.
          * 
          * @param message           the incoming message
          * @param request           the protocol request
+         * @param protocol          the protocol family in use
          *
          * @throws BindingException raised if the message/request is invalid according to the supplied rules
          */
-        void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request=NULL);
+        void evaluate(
+            const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL, const XMLCh* protocol=NULL
+            );
 
         /**
-         * Resets the policy object and clears any per-message state.
+         * Resets the policy object and/or clears any per-message state.
+         *
+         * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
+         * when evaluating multiple layers of a message.
+         *
+         * @param messageOnly   true iff security and issuer state should be left in place
          */
-        void reset();
+        void reset(bool messageOnly=false);
         
         /**
-         * Returns the message element/type as determined by the registered policies.
-         * 
-         * @return message element/type as determined by the registered policies
-         */
-        const xmltooling::QName* getMessageQName() const {
-            return m_messageQName;
-        }
-
-        /**
          * Returns the message identifier as determined by the registered policies.
          * 
          * @return message identifier as determined by the registered policies
@@ -236,23 +239,13 @@ namespace opensaml {
         }
 
         /**
-         * Sets the message element/type as determined by the registered policies.
-         * 
-         * @param messageQName message element/type
-         */
-        void setMessageQName(const xmltooling::QName* messageQName) {
-            delete m_messageQName;
-            m_messageQName = messageQName ? new xmltooling::QName(*messageQName) : NULL;
-        }
-
-        /**
          * Sets the message identifier as determined by the registered policies.
          * 
          * @param id message identifier
          */
         void setMessageID(const XMLCh* id) {
-            XMLString::release(&m_messageID);
-            m_messageID = XMLString::replicate(id);
+            xercesc::XMLString::release(&m_messageID);
+            m_messageID = xercesc::XMLString::replicate(id);
         }
 
         /**
@@ -266,11 +259,17 @@ namespace opensaml {
 
         /**
          * Sets the issuer of the message as determined by the registered policies.
-         * The policy object takes ownership of the Issuer object.
          * 
          * @param issuer issuer of the message
          */
-        void setIssuer(saml2::Issuer* issuer);
+        void setIssuer(const saml2::Issuer* issuer);
+
+        /**
+         * Sets the issuer of the message as determined by the registered policies.
+         * 
+         * @param issuer issuer of the message
+         */
+        void setIssuer(const XMLCh* issuer);
         
         /**
          * Sets the metadata for the role the issuer is operating in.
@@ -307,6 +306,19 @@ namespace opensaml {
              * @return  true iff the operands match
              */
             virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
+
+            /**
+             * Returns true iff the two operands "match". Applications can override this method to
+             * support non-standard issuer matching for complex policies. 
+             * 
+             * <p>The default implementation does a basic comparison of the XML content, treating
+             * an unsupplied Format as an "entityID".
+             * 
+             * @param issuer1   the first Issuer to match
+             * @param issuer2   the second Issuer to match
+             * @return  true iff the operands match
+             */
+            virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
         };
 
         /**
@@ -337,7 +349,6 @@ namespace opensaml {
 
     private:
         // information extracted from message 
-        xmltooling::QName* m_messageQName;
         XMLCh* m_messageID;
         time_t m_issueInstant;
         saml2::Issuer* m_issuer;
@@ -350,6 +361,7 @@ namespace opensaml {
         const saml2md::MetadataProvider* m_metadata;
         xmltooling::QName* m_role;
         const xmltooling::TrustEngine* m_trust;
+        bool m_validate;
     };
 
 };