2 * Copyright 2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * DelegationRestrictionRule.cpp
20 * SAML DelegationRestriction SecurityPolicyRule
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "util/SAMLConstants.h"
29 #include <xmltooling/logging.h>
31 using namespace opensaml::saml2;
32 using namespace opensaml;
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
39 class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
42 DelegationRestrictionRule(const DOMElement* e);
44 virtual ~DelegationRestrictionRule() {
45 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
47 const char* getType() const {
48 return DELEGATION_POLICY_RULE;
50 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
53 vector<Delegate*> m_delegates;
61 SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
63 return new DelegationRestrictionRule(e);
66 class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
67 public unary_function<const Delegate*,bool>
69 const Delegate* m_operand;
70 bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
72 f1 = NameIDType::UNSPECIFIED;
74 f2 = NameIDType::UNSPECIFIED;
75 return XMLString::equals(f1, f2);
77 bool matches(const NameID* n1, const NameID* n2) const {
78 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
79 XMLString::equals(n1->getName(), n2->getName()) &&
80 XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
81 XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
84 _isSameDelegate() : m_operand(NULL) {}
85 _isSameDelegate(const Delegate* d) : m_operand(d) {}
87 // d1 is the input from the message, d2 is from the policy
88 bool operator()(const Delegate* d1, const Delegate* d2) const {
89 if (!d1->getNameID()) {
90 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
91 "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
95 if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
96 return matches(d1->getNameID(), d2->getNameID());
101 // d is from the policy
102 bool operator()(const Delegate* d) const {
103 return this->operator()(m_operand, d);
107 static XMLCh match[] = UNICODE_LITERAL_5(m,a,t,c,h);
108 static XMLCh any[] = UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
109 static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
110 static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
115 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_match(MATCH_ANY)
118 const XMLCh* m = e->getAttributeNS(NULL, match);
119 if (XMLString::equals(m, newest))
120 m_match = MATCH_NEWEST;
121 else if (XMLString::equals(m, oldest))
122 m_match = MATCH_OLDEST;
123 else if (m && *m && !XMLString::equals(m, any))
124 throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
127 DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
129 auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
130 Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
132 m_delegates.push_back(down);
135 d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
139 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
145 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
147 const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
151 // If we have no embedded Delegates, the condition evaluates to true.
152 if (m_delegates.empty())
155 const vector<Delegate*>& dels = drt->getDelegates();
156 if (m_match == MATCH_ANY) {
157 // Each Delegate in the condition MUST match an embedded Delegate.
158 for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
159 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
163 else if (m_match == MATCH_OLDEST) {
164 return (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.begin());
166 else if (m_match == MATCH_NEWEST) {
167 return (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.rbegin());