ca8c0027e527adbe613e9ded7c73cfa5bd7557da
[shibboleth/cpp-opensaml.git] / saml / saml1 / core / impl / AssertionsSchemaValidators.cpp
1 /*
2 *  Copyright 2001-2006 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  * AssertionsSchemaValidators.cpp
19  * 
20  * Schema-based validators for SAML 1.x Assertions classes
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/core/Assertions.h"
26
27 #include <xmltooling/validation/ValidatorSuite.h>
28
29 using namespace opensaml::saml1;
30 using namespace opensaml;
31 using namespace xmltooling;
32 using namespace std;
33
34 namespace opensaml {
35     namespace saml1 {
36         
37         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,Action);
38         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AssertionIDReference);
39         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,Audience);
40         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,ConfirmationMethod);
41         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,NameIdentifier);
42
43         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AudienceRestrictionCondition);
44             XMLOBJECTVALIDATOR_NONEMPTY(AudienceRestrictionCondition,Audience);
45         END_XMLOBJECTVALIDATOR;
46
47         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Conditions);
48             if (!ptr->hasChildren()) {
49                 XMLOBJECTVALIDATOR_ONEOF(Conditions,NotBefore,NotOnOrAfter);
50             }
51         END_XMLOBJECTVALIDATOR;
52
53         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,SubjectConfirmation);
54             XMLOBJECTVALIDATOR_NONEMPTY(SubjectConfirmation,ConfirmationMethod);
55         END_XMLOBJECTVALIDATOR;
56
57         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Subject);
58             XMLOBJECTVALIDATOR_ONEOF(Subject,NameIdentifier,SubjectConfirmation);
59         END_XMLOBJECTVALIDATOR;
60
61         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,SubjectLocality);
62             XMLOBJECTVALIDATOR_ONEOF(SubjectLocality,IPAddress,DNSAddress);
63         END_XMLOBJECTVALIDATOR;
64
65         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthorityBinding);
66             XMLOBJECTVALIDATOR_REQUIRE(AuthorityBinding,AuthorityKind);
67             XMLOBJECTVALIDATOR_REQUIRE(AuthorityBinding,Location);
68             XMLOBJECTVALIDATOR_REQUIRE(AuthorityBinding,Binding);
69         END_XMLOBJECTVALIDATOR;
70
71         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthenticationStatement);
72             XMLOBJECTVALIDATOR_REQUIRE(AuthenticationStatement,AuthenticationMethod);
73             XMLOBJECTVALIDATOR_REQUIRE(AuthenticationStatement,AuthenticationInstant);
74             XMLOBJECTVALIDATOR_REQUIRE(AuthenticationStatement,Subject);
75         END_XMLOBJECTVALIDATOR;
76
77         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Evidence);
78             if (!ptr->hasChildren())
79                 throw ValidationException("Evidence must have at least one child element.");
80         END_XMLOBJECTVALIDATOR;
81
82         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthorizationDecisionStatement);
83             XMLOBJECTVALIDATOR_REQUIRE(AuthorizationDecisionStatement,Resource);
84             XMLOBJECTVALIDATOR_REQUIRE(AuthorizationDecisionStatement,Decision);
85             if (!XMLString::equals(ptr->getDecision(),AuthorizationDecisionStatement::DECISION_PERMIT) &&
86                 !XMLString::equals(ptr->getDecision(),AuthorizationDecisionStatement::DECISION_DENY) &&
87                 !XMLString::equals(ptr->getDecision(),AuthorizationDecisionStatement::DECISION_INDETERMINATE))
88                 throw ValidationException("Decision must be one of Deny, Permit, or Indeterminate.");
89             XMLOBJECTVALIDATOR_REQUIRE(AuthorizationDecisionStatement,Subject);
90             XMLOBJECTVALIDATOR_NONEMPTY(AuthorizationDecisionStatement,Action);
91         END_XMLOBJECTVALIDATOR;
92
93         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AttributeDesignator);
94             XMLOBJECTVALIDATOR_REQUIRE(AttributeDesignator,AttributeName);
95             XMLOBJECTVALIDATOR_REQUIRE(AttributeDesignator,AttributeNamespace);
96         END_XMLOBJECTVALIDATOR;
97
98         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Attribute);
99             XMLOBJECTVALIDATOR_REQUIRE(Attribute,AttributeName);
100             XMLOBJECTVALIDATOR_REQUIRE(Attribute,AttributeNamespace);
101             XMLOBJECTVALIDATOR_NONEMPTY(Attribute,AttributeValue);
102         END_XMLOBJECTVALIDATOR;
103
104         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AttributeStatement);
105             XMLOBJECTVALIDATOR_NONEMPTY(AttributeStatement,Attribute);
106         END_XMLOBJECTVALIDATOR;
107
108         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Assertion);
109             XMLOBJECTVALIDATOR_REQUIRE(Assertion,AssertionID);
110             XMLOBJECTVALIDATOR_REQUIRE(Assertion,Issuer);
111             XMLOBJECTVALIDATOR_REQUIRE(Assertion,IssueInstant);
112             if (ptr->getAuthenticationStatements().empty() &&
113                 ptr->getAttributeStatements().empty() &&
114                 ptr->getAuthorizationDecisionStatements().empty() &&
115                 ptr->getSubjectStatements().empty() &&
116                 ptr->getStatements().empty())
117                 throw ValidationException("Assertion must have at least one statement.");
118             pair<bool,int> minor=ptr->getMinorVersion();
119             if (!minor.first)
120                 throw ValidationException("Assertion must have MinorVersion");
121             if (minor.second==0 && ptr->getConditions() && !ptr->getConditions()->getDoNotCacheConditions().empty())
122                 throw ValidationException("SAML 1.0 assertions cannot contain DoNotCacheCondition elements.");
123         END_XMLOBJECTVALIDATOR;
124
125         class SAML_DLLLOCAL checkWildcardNS {
126         public:
127             void operator()(const XMLObject* xmlObject) const {
128                 const XMLCh* ns=xmlObject->getElementQName().getNamespaceURI();
129                 if (XMLString::equals(ns,SAMLConstants::SAML1_NS) || !ns || !*ns) {
130                     throw ValidationException(
131                         "Object contains an illegal extension child element ($1).",
132                         params(1,xmlObject->getElementQName().toString().c_str())
133                         );
134                 }
135             }
136         };
137
138         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Advice);
139             const vector<XMLObject*>& anys=ptr->getOthers();
140             for_each(anys.begin(),anys.end(),checkWildcardNS());
141         END_XMLOBJECTVALIDATOR;
142
143     };
144 };
145
146 #define REGISTER_ELEMENT(cname) \
147     q=QName(SAMLConstants::SAML1_NS,cname::LOCAL_NAME); \
148     XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
149     SchemaValidators.registerValidator(q,new cname##SchemaValidator())
150     
151 #define REGISTER_TYPE(cname) \
152     q=QName(SAMLConstants::SAML1_NS,cname::TYPE_NAME); \
153     XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
154     SchemaValidators.registerValidator(q,new cname##SchemaValidator())
155
156 #define REGISTER_ELEMENT_NOVAL(cname) \
157     q=QName(SAMLConstants::SAML1_NS,cname::LOCAL_NAME); \
158     XMLObjectBuilder::registerBuilder(q,new cname##Builder());
159     
160 #define REGISTER_TYPE_NOVAL(cname) \
161     q=QName(SAMLConstants::SAML1_NS,cname::TYPE_NAME); \
162     XMLObjectBuilder::registerBuilder(q,new cname##Builder());
163
164 void opensaml::saml1::registerAssertionClasses() {
165     QName q;
166     REGISTER_ELEMENT(Action);
167     REGISTER_ELEMENT(Advice);
168     REGISTER_ELEMENT(Assertion);
169     REGISTER_ELEMENT(AssertionIDReference);
170     REGISTER_ELEMENT(Attribute);
171     REGISTER_ELEMENT(AttributeDesignator);
172     REGISTER_ELEMENT(AttributeStatement);
173     REGISTER_ELEMENT_NOVAL(AttributeValue);
174     REGISTER_ELEMENT(Audience);
175     REGISTER_ELEMENT(AudienceRestrictionCondition);
176     REGISTER_ELEMENT(AuthenticationStatement);
177     REGISTER_ELEMENT(AuthorityBinding);
178     REGISTER_ELEMENT(AuthorizationDecisionStatement);
179     REGISTER_ELEMENT(Conditions);
180     REGISTER_ELEMENT(ConfirmationMethod);
181     REGISTER_ELEMENT_NOVAL(DoNotCacheCondition);
182     REGISTER_ELEMENT(Evidence);
183     REGISTER_ELEMENT(NameIdentifier);
184     REGISTER_ELEMENT(Subject);
185     REGISTER_ELEMENT(SubjectConfirmation);
186     REGISTER_ELEMENT_NOVAL(SubjectConfirmationData);
187     REGISTER_ELEMENT(SubjectLocality);
188     REGISTER_TYPE(Action);
189     REGISTER_TYPE(Advice);
190     REGISTER_TYPE(Assertion);
191     REGISTER_TYPE(Attribute);
192     REGISTER_TYPE(AttributeDesignator);
193     REGISTER_TYPE(AttributeStatement);
194     REGISTER_TYPE(AudienceRestrictionCondition);
195     REGISTER_TYPE(AuthenticationStatement);
196     REGISTER_TYPE(AuthorityBinding);
197     REGISTER_TYPE(AuthorizationDecisionStatement);
198     REGISTER_TYPE(Conditions);
199     REGISTER_TYPE_NOVAL(DoNotCacheCondition);
200     REGISTER_TYPE(Evidence);
201     REGISTER_TYPE(NameIdentifier);
202     REGISTER_TYPE(Subject);
203     REGISTER_TYPE(SubjectConfirmation);
204     REGISTER_TYPE(SubjectLocality);
205 }