New policy rules for handling conditions.
[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             if (XMLString::equals(policy.getRecipient(), (*a1)->getAudienceURI())) {
76                 return true;
77             }
78             for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
79                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
80                     return true;
81             }
82         }
83
84         ostringstream os;
85         os << *ac2;
86         Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
87             "unacceptable AudienceRestriction in assertion (%s)", os.str().c_str()
88             );
89         throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestriction.");
90     }
91
92     const saml1::AudienceRestrictionCondition* ac1=dynamic_cast<const saml1::AudienceRestrictionCondition*>(&message);
93     if (ac1) {
94         const vector<saml1::Audience*>& auds1 = ac1->getAudiences();
95         for (vector<saml1::Audience*>::const_iterator a1 = auds1.begin(); a1!=auds1.end(); ++a1) {
96             if (XMLString::equals(policy.getRecipient(), (*a1)->getAudienceURI())) {
97                 return true;
98             }
99             for (vector<const XMLCh*>::const_iterator a2 = m_audiences.begin(); a2!=m_audiences.end(); ++a2) {
100                 if (XMLString::equals((*a1)->getAudienceURI(), *a2))
101                     return true;
102             }
103         }
104
105         ostringstream os;
106         os << *ac1;
107         Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.AudienceRestriction").error(
108             "unacceptable AudienceRestrictionCondition in assertion (%s)", os.str().c_str()
109             );
110         throw SecurityPolicyException("Assertion contains an unacceptable AudienceRestrictionCondition.");
111     }
112
113     return false;
114 }