Expose audience collection on security policy.
[shibboleth/cpp-opensaml.git] / saml / saml1 / profile / impl / AssertionValidator.cpp
1 /*
2  *  Copyright 2001-2007 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  * AssertionValidator.cpp
19  * 
20  * SAML 1.x basic assertion validator
21  */
22
23 #include "internal.h"
24 #include "saml1/core/Assertions.h"
25 #include "saml1/profile/AssertionValidator.h"
26
27 #include <xmltooling/logging.h>
28 #include <xmltooling/util/NDC.h>
29
30 using namespace opensaml::saml1;
31 using namespace xmltooling::logging;
32 using namespace xmltooling;
33 using namespace std;
34
35 void AssertionValidator::validate(const xmltooling::XMLObject* xmlObject) const
36 {
37     const Assertion* a=dynamic_cast<const Assertion*>(xmlObject);
38     if (!a)
39         throw ValidationException("Validator only applies to SAML 1.x Assertion objects.");
40     validateAssertion(*a);
41 }
42
43 void AssertionValidator::validateAssertion(const Assertion& assertion) const
44 {
45 #ifdef _DEBUG
46     xmltooling::NDC ndc("validate");
47 #endif
48
49     const Conditions* conds = assertion.getConditions();
50     if (!conds)
51         return;
52     
53     // First verify the time conditions, using the specified timestamp, if non-zero.
54     if (m_ts>0) {
55         unsigned int skew = XMLToolingConfig::getConfig().clock_skew_secs;
56         time_t t=conds->getNotBeforeEpoch();
57         if (m_ts+skew < t)
58             throw ValidationException("Assertion is not yet valid.");
59         t=conds->getNotOnOrAfterEpoch();
60         if (t <= m_ts-skew)
61             throw ValidationException("Assertion is no longer valid.");
62     }
63
64     // Now we process conditions, starting with the known types and then extensions.
65
66     const vector<AudienceRestrictionCondition*>& acvec = conds->getAudienceRestrictionConditions();
67     for (vector<AudienceRestrictionCondition*>::const_iterator ac = acvec.begin(); ac!=acvec.end(); ++ac)
68         validateCondition(*ac);
69
70     const vector<DoNotCacheCondition*>& dncvec = conds->getDoNotCacheConditions();
71     for (vector<DoNotCacheCondition*>::const_iterator dnc = dncvec.begin(); dnc!=dncvec.end(); ++dnc) 
72         validateCondition(*dnc);
73
74     const vector<Condition*>& convec = conds->getConditions();
75     for (vector<Condition*>::const_iterator c = convec.begin(); c!=convec.end(); ++c)
76         validateCondition(*c);
77 }
78
79 void AssertionValidator::validateCondition(const Condition* c) const
80 {
81     const AudienceRestrictionCondition* ac=dynamic_cast<const AudienceRestrictionCondition*>(c);
82     if (!ac) {
83         Category::getInstance(SAML_LOGCAT".AssertionValidator").error("unrecognized Condition in assertion (%s)",
84             c->getSchemaType() ? c->getSchemaType()->toString().c_str() : c->getElementQName().toString().c_str());
85         throw ValidationException("Assertion contains an unrecognized condition.");
86     }
87
88     bool found = false;
89     const vector<Audience*>& auds1 = ac->getAudiences();
90     for (vector<Audience*>::const_iterator a = auds1.begin(); !found && a!=auds1.end(); ++a) {
91         if (XMLString::equals(m_recipient, (*a)->getAudienceURI())) {
92             found = true;
93         }
94         else if (m_audiences) {
95             for (vector<const XMLCh*>::const_iterator a2 = m_audiences->begin(); !found && a2!=m_audiences->end(); ++a2) {
96                 found = XMLString::equals((*a)->getAudienceURI(), *a2);
97             }
98         }
99     }
100
101     if (!found) {
102         ostringstream os;
103         os << *ac;
104         Category::getInstance(SAML_LOGCAT".AssertionValidator").error(
105             "unacceptable AudienceRestrictionCondition in assertion (%s)", os.str().c_str()
106             );
107         throw ValidationException("Assertion contains an unacceptable AudienceRestrictionCondition.");
108     }
109 }