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