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