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/SecurityPolicyRule.h"
26 #include "saml1/core/Assertions.h"
27 #include "saml2/core/Assertions.h"
29 #include <xmltooling/logging.h>
31 using namespace opensaml;
32 using namespace xmltooling::logging;
33 using namespace xmltooling;
37 class SAML_DLLLOCAL AudienceRestrictionRule : public SecurityPolicyRule
40 AudienceRestrictionRule(const DOMElement* e);
42 virtual ~AudienceRestrictionRule() {
44 const char* getType() const {
45 return AUDIENCE_POLICY_RULE;
47 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
50 vector<const XMLCh*> m_audiences;
53 SecurityPolicyRule* SAML_DLLLOCAL AudienceRestrictionRuleFactory(const DOMElement* const & e)
55 return new AudienceRestrictionRule(e);
59 AudienceRestrictionRule::AudienceRestrictionRule(const DOMElement* e)
61 e = e ? XMLHelper::getFirstChildElement(e, saml2::Audience::LOCAL_NAME) : NULL;
63 if (e->hasChildNodes())
64 m_audiences.push_back(e->getFirstChild()->getNodeValue());
65 e = XMLHelper::getNextSiblingElement(e, saml2::Audience::LOCAL_NAME);
69 bool AudienceRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
71 const saml2::AudienceRestriction* ac2=dynamic_cast<const saml2::AudienceRestriction*>(&message);
73 const vector<saml2::Audience*>& auds2 = ac2->getAudiences();
74 for (vector<saml2::Audience*>::const_iterator a1 = auds2.begin(); a1!=auds2.end(); ++a1) {
75 for (vector<xstring>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
76 if (XMLString::equals((*a1)->getAudienceURI(), a2->c_str()))
79 for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
80 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
87 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
88 "unacceptable AudienceRestriction in assertion (%s)", os.str().c_str()
90 throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestriction.");
93 const saml1::AudienceRestrictionCondition* ac1=dynamic_cast<const saml1::AudienceRestrictionCondition*>(&message);
95 const vector<saml1::Audience*>& auds1 = ac1->getAudiences();
96 for (vector<saml1::Audience*>::const_iterator a1 = auds1.begin(); a1!=auds1.end(); ++a1) {
97 for (vector<xstring>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
98 if (XMLString::equals((*a1)->getAudienceURI(), a2->c_str()))
101 for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
102 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
109 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
110 "unacceptable AudienceRestrictionCondition in assertion (%s)", os.str().c_str()
112 throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestrictionCondition.");