CPPXT-104 - Add exception handling to integer conversions
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SecurityPolicy.cpp
index 2d40425..e772a62 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
 #include "binding/SecurityPolicyRule.h"
 #include "saml2/core/Assertions.h"
 
+#include <boost/bind.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
+
 using namespace opensaml::saml2md;
 using namespace opensaml::saml2;
 using namespace opensaml;
 using namespace xmltooling;
+using namespace boost;
 using namespace std;
 
 namespace opensaml {
@@ -81,27 +89,22 @@ SecurityPolicy::SecurityPolicy(
     const xmltooling::QName* role,
     const xmltooling::TrustEngine* trustEngine,
     bool validate
-    ) : m_metadataCriteria(NULL),
+    ) : m_metadataCriteria(nullptr),
         m_issueInstant(0),
-        m_issuer(NULL),
-        m_issuerRole(NULL),
+        m_issuerRole(nullptr),
         m_authenticated(false),
-        m_matchingPolicy(NULL),
         m_metadata(metadataProvider),
-        m_role(NULL),
+        m_role(role ? new xmltooling::QName(*role) : nullptr),
         m_trust(trustEngine),
         m_validate(validate),
         m_entityOnly(true),
         m_ts(0)
 {
-    if (role)
-        m_role = new xmltooling::QName(*role);
 }
 
 SecurityPolicy::~SecurityPolicy()
 {
     delete m_metadataCriteria;
-    delete m_issuer;
 }
 
 const MetadataProvider* SecurityPolicy::getMetadataProvider() const
@@ -120,7 +123,7 @@ MetadataProvider::Criteria& SecurityPolicy::getMetadataProviderCriteria() const
 
 const xmltooling::QName* SecurityPolicy::getRole() const
 {
-    return m_role;
+    return m_role.get();
 }
 
 const TrustEngine* SecurityPolicy::getTrustEngine() const
@@ -151,7 +154,7 @@ vector<xstring>& SecurityPolicy::getAudiences()
 time_t SecurityPolicy::getTime() const
 {
     if (m_ts == 0)
-        return m_ts = time(NULL);
+        return m_ts = time(nullptr);
     return m_ts;
 }
 
@@ -179,8 +182,7 @@ void SecurityPolicy::setMetadataProviderCriteria(MetadataProvider::Criteria* cri
 
 void SecurityPolicy::setRole(const xmltooling::QName* role)
 {
-    delete m_role;
-    m_role = role ? new xmltooling::QName(*role) : NULL;
+    m_role.reset(role ? new xmltooling::QName(*role) : nullptr);
 }
 
 void SecurityPolicy::setTrustEngine(const TrustEngine* trust)
@@ -212,8 +214,10 @@ void SecurityPolicy::setCorrelationID(const XMLCh* correlationID)
 
 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
 {
-    for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
-        (*i)->evaluate(message,request,*this);
+    for_each(
+        m_rules.begin(), m_rules.end(),
+        boost::bind(&SecurityPolicyRule::evaluate, _1, boost::ref(message), request, boost::ref(*this))
+        );
 }
 
 void SecurityPolicy::reset(bool messageOnly)
@@ -226,9 +230,8 @@ void SecurityPolicy::_reset(bool messageOnly)
     m_messageID.erase();
     m_issueInstant=0;
     if (!messageOnly) {
-        delete m_issuer;
-        m_issuer=NULL;
-        m_issuerRole=NULL;
+        m_issuer.reset();
+        m_issuerRole=nullptr;
         m_authenticated=false;
     }
 }
@@ -245,7 +248,7 @@ time_t SecurityPolicy::getIssueInstant() const
 
 const Issuer* SecurityPolicy::getIssuer() const
 {
-    return m_issuer;
+    return m_issuer.get();
 }
 
 const RoleDescriptor* SecurityPolicy::getIssuerMetadata() const
@@ -272,25 +275,25 @@ void SecurityPolicy::setIssueInstant(time_t issueInstant)
 
 void SecurityPolicy::setIssuer(const Issuer* issuer)
 {
-    if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
+    if (!getIssuerMatchingPolicy().issuerMatches(m_issuer.get(), issuer))
         throw SecurityPolicyException("An Issuer was supplied that conflicts with previous results.");
 
-    if (!m_issuer) {
+    if (!m_issuer.get()) {
         if (m_entityOnly && issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY))
             throw SecurityPolicyException("A non-entity Issuer was supplied, violating policy.");
-        m_issuerRole = NULL;
-        m_issuer=issuer->cloneIssuer();
+        m_issuerRole = nullptr;
+        m_issuer.reset(issuer->cloneIssuer());
     }
 }
 
 void SecurityPolicy::setIssuer(const XMLCh* issuer)
 {
-    if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
+    if (!getIssuerMatchingPolicy().issuerMatches(m_issuer.get(), issuer))
         throw SecurityPolicyException("An Issuer was supplied that conflicts with previous results.");
 
-    if (!m_issuer && issuer && *issuer) {
-        m_issuerRole = NULL;
-        m_issuer = IssuerBuilder::buildIssuer();
+    if (!m_issuer.get() && issuer && *issuer) {
+        m_issuerRole = nullptr;
+        m_issuer.reset(IssuerBuilder::buildIssuer());
         m_issuer->setName(issuer);
     }
 }
@@ -299,7 +302,7 @@ void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
 {
     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
         throw SecurityPolicyException("A rule supplied a RoleDescriptor that conflicts with previous results.");
-    m_issuerRole=issuerRole;
+    m_issuerRole = issuerRole;
 }
 
 void SecurityPolicy::setAuthenticated(bool auth)
@@ -317,7 +320,7 @@ SecurityPolicy::IssuerMatchingPolicy::~IssuerMatchingPolicy()
 
 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
 {
-    // NULL matches anything for the purposes of this interface.
+    // nullptr matches anything for the purposes of this interface.
     if (!issuer1 || !issuer2)
         return true;
 
@@ -346,7 +349,7 @@ bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1,
 
 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const XMLCh* issuer2) const
 {
-    // NULL matches anything for the purposes of this interface.
+    // nullptr matches anything for the purposes of this interface.
     if (!issuer1 || !issuer2 || !*issuer2)
         return true;
 
@@ -373,11 +376,10 @@ SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
 
 const SecurityPolicy::IssuerMatchingPolicy& SecurityPolicy::getIssuerMatchingPolicy() const
 {
-    return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
+    return m_matchingPolicy.get() ? *m_matchingPolicy.get() : m_defaultMatching;
 }
 
 void SecurityPolicy::setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy)
 {
-    delete m_matchingPolicy;
-    m_matchingPolicy = matchingPolicy;
+    m_matchingPolicy.reset(matchingPolicy);
 }