b579931641267b9e291dc9dd79239499e6ffb8c5
[shibboleth/cpp-opensaml.git] / saml / profile / impl / AudienceRestrictionRule.cpp
1 /*
2  *  Copyright 2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * AudienceRestrictionRule.cpp
19  *
20  * SAML AudienceRestriction SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml1/core/Assertions.h"
27 #include "saml2/core/Assertions.h"
28
29 #include <xmltooling/logging.h>
30
31 using namespace opensaml;
32 using namespace xmltooling::logging;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace opensaml {
37     class SAML_DLLLOCAL AudienceRestrictionRule : public SecurityPolicyRule
38     {
39     public:
40         AudienceRestrictionRule(const DOMElement* e);
41
42         virtual ~AudienceRestrictionRule() {
43         }
44         const char* getType() const {
45             return AUDIENCE_POLICY_RULE;
46         }
47         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
48
49     private:
50         vector<const XMLCh*> m_audiences;
51     };
52
53     SecurityPolicyRule* SAML_DLLLOCAL AudienceRestrictionRuleFactory(const DOMElement* const & e)
54     {
55         return new AudienceRestrictionRule(e);
56     }
57 };
58
59 AudienceRestrictionRule::AudienceRestrictionRule(const DOMElement* e)
60 {
61     e = e ? XMLHelper::getFirstChildElement(e, saml2::Audience::LOCAL_NAME) : NULL;
62     while (e) {
63         if (e->hasChildNodes())
64             m_audiences.push_back(e->getFirstChild()->getNodeValue());
65         e = XMLHelper::getNextSiblingElement(e, saml2::Audience::LOCAL_NAME);
66     }
67 }
68
69 bool AudienceRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
70 {
71     const saml2::AudienceRestriction* ac2=dynamic_cast<const saml2::AudienceRestriction*>(&message);
72     if (ac2) {
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<const XMLCh*>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
76                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
77                     return true;
78             }
79             for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
80                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
81                     return true;
82             }
83         }
84
85         ostringstream os;
86         os << *ac2;
87         Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
88             "unacceptable AudienceRestriction in assertion (%s)", os.str().c_str()
89             );
90         throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestriction.");
91     }
92
93     const saml1::AudienceRestrictionCondition* ac1=dynamic_cast<const saml1::AudienceRestrictionCondition*>(&message);
94     if (ac1) {
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<const XMLCh*>::const_iterator a2 = policy.getAudiences().begin(); a2!=policy.getAudiences().end(); ++a2) {
98                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
99                     return true;
100             }
101             for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
102                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
103                     return true;
104             }
105         }
106
107         ostringstream os;
108         os << *ac1;
109         Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
110             "unacceptable AudienceRestrictionCondition in assertion (%s)", os.str().c_str()
111             );
112         throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestrictionCondition.");
113     }
114
115     return false;
116 }