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