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 * AudienceRestrictionRule.cpp
20 * SAML AudienceRestriction SecurityPolicyRule
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml2/core/Assertions.h"
30 #include <xmltooling/logging.h>
32 using namespace opensaml;
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
38 class SAML_DLLLOCAL AudienceRestrictionRule : public SecurityPolicyRule
41 AudienceRestrictionRule(const DOMElement* e);
43 virtual ~AudienceRestrictionRule() {
45 const char* getType() const {
46 return AUDIENCE_POLICY_RULE;
48 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
51 vector<const XMLCh*> m_audiences;
54 SecurityPolicyRule* SAML_DLLLOCAL AudienceRestrictionRuleFactory(const DOMElement* const & e)
56 return new AudienceRestrictionRule(e);
60 AudienceRestrictionRule::AudienceRestrictionRule(const DOMElement* e)
62 e = e ? XMLHelper::getFirstChildElement(e, saml2::Audience::LOCAL_NAME) : NULL;
64 if (e->hasChildNodes())
65 m_audiences.push_back(e->getFirstChild()->getNodeValue());
66 e = XMLHelper::getNextSiblingElement(e, saml2::Audience::LOCAL_NAME);
70 bool AudienceRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
72 const saml2::AudienceRestriction* ac2=dynamic_cast<const saml2::AudienceRestriction*>(&message);
74 const vector<saml2::Audience*>& auds2 = ac2->getAudiences();
75 for (vector<saml2::Audience*>::const_iterator a1 = auds2.begin(); a1!=auds2.end(); ++a1) {
76 for (vector<xstring>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
77 if (XMLString::equals((*a1)->getAudienceURI(), a2->c_str()))
80 for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
81 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
88 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
89 "unacceptable AudienceRestriction in assertion (%s)", os.str().c_str()
91 throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestriction.");
94 const saml1::AudienceRestrictionCondition* ac1=dynamic_cast<const saml1::AudienceRestrictionCondition*>(&message);
96 const vector<saml1::Audience*>& auds1 = ac1->getAudiences();
97 for (vector<saml1::Audience*>::const_iterator a1 = auds1.begin(); a1!=auds1.end(); ++a1) {
98 for (vector<xstring>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
99 if (XMLString::equals((*a1)->getAudienceURI(), a2->c_str()))
102 for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
103 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
110 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
111 "unacceptable AudienceRestrictionCondition in assertion (%s)", os.str().c_str()
113 throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestrictionCondition.");