1fe5041e86cc8b52c640806e64a4254aefa6f8cb
[shibboleth/cpp-opensaml.git] / saml / saml1 / core / impl / ProtocolsSchemaValidators.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  * ProtocolsSchemaValidators.cpp
19  * 
20  * Schema-based validators for SAML 1.x Protocols classes
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/core/Protocols.h"
26
27 using namespace opensaml::saml1;
28 using namespace opensaml;
29 using namespace xmltooling;
30 using namespace std;
31
32 namespace opensaml {
33     namespace saml1 {
34         
35         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AssertionArtifact);
36         XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,StatusMessage);
37         
38         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,RespondWith);
39             XMLOBJECTVALIDATOR_REQUIRE(RespondWith,QName);
40         END_XMLOBJECTVALIDATOR;
41
42         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthenticationQuery);
43             XMLOBJECTVALIDATOR_REQUIRE(AuthenticationQuery,AuthenticationMethod);
44             XMLOBJECTVALIDATOR_REQUIRE(AuthenticationQuery,Subject);
45         END_XMLOBJECTVALIDATOR;
46
47         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AttributeQuery);
48             XMLOBJECTVALIDATOR_REQUIRE(AttributeQuery,Subject);
49         END_XMLOBJECTVALIDATOR;
50
51         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthorizationDecisionQuery);
52             XMLOBJECTVALIDATOR_REQUIRE(AuthorizationDecisionQuery,Subject);
53             XMLOBJECTVALIDATOR_REQUIRE(AuthorizationDecisionQuery,Resource);
54             XMLOBJECTVALIDATOR_NONEMPTY(AuthorizationDecisionQuery,Action);
55         END_XMLOBJECTVALIDATOR;
56
57         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Request);
58             XMLOBJECTVALIDATOR_REQUIRE(Request,RequestID);
59             XMLOBJECTVALIDATOR_REQUIRE(Request,IssueInstant);
60             int count=0; 
61             if (ptr->getQuery()!=NULL)
62                 count++;
63             if (!ptr->getAssertionIDReferences().empty())
64                 count++;
65             if (!ptr->getAssertionArtifacts().empty())
66                 count++;
67             if (count != 1)
68                 throw ValidationException("Request must have either a query, >0 assertion references, or >0 artifacts.");
69         END_XMLOBJECTVALIDATOR;
70
71         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,StatusCode);
72             XMLOBJECTVALIDATOR_REQUIRE(StatusCode,Value);
73         END_XMLOBJECTVALIDATOR;
74
75         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Status);
76             XMLOBJECTVALIDATOR_REQUIRE(Status,StatusCode);
77             const QName* value=ptr->getStatusCode()->getValue();
78             if (!value || (*value!=StatusCode::SUCCESS && *value!=StatusCode::REQUESTER &&
79                 *value!=StatusCode::RESPONDER && *value!=StatusCode::VERSIONMISMATCH))
80                 throw ValidationException("Top-level status code not one of the allowable values."); 
81         END_XMLOBJECTVALIDATOR;
82
83         BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Response);
84             XMLOBJECTVALIDATOR_REQUIRE(Response,ResponseID);
85             XMLOBJECTVALIDATOR_REQUIRE(Response,IssueInstant);
86             XMLOBJECTVALIDATOR_REQUIRE(Response,Status);
87         END_XMLOBJECTVALIDATOR;
88     };
89 };
90
91 #define REGISTER_ELEMENT(cname) \
92     q=QName(SAMLConstants::SAML1P_NS,cname::LOCAL_NAME); \
93     XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
94     Validator::registerValidator(q,new cname##SchemaValidator())
95     
96 #define REGISTER_TYPE(cname) \
97     q=QName(SAMLConstants::SAML1P_NS,cname::TYPE_NAME); \
98     XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
99     Validator::registerValidator(q,new cname##SchemaValidator())
100
101 #define REGISTER_ELEMENT_NOVAL(cname) \
102     q=QName(SAMLConstants::SAML1P_NS,cname::LOCAL_NAME); \
103     XMLObjectBuilder::registerBuilder(q,new cname##Builder());
104     
105 #define REGISTER_TYPE_NOVAL(cname) \
106     q=QName(SAMLConstants::SAML1P_NS,cname::TYPE_NAME); \
107     XMLObjectBuilder::registerBuilder(q,new cname##Builder());
108
109 void opensaml::saml1::registerProtocolClasses() {
110     QName q;
111     REGISTER_ELEMENT(AssertionArtifact);
112     REGISTER_ELEMENT(AttributeQuery);
113     REGISTER_ELEMENT(AuthenticationQuery);
114     REGISTER_ELEMENT(AuthorizationDecisionQuery);
115     REGISTER_ELEMENT(Request);
116     REGISTER_ELEMENT(RespondWith);
117     REGISTER_ELEMENT(Response);
118     REGISTER_ELEMENT(Status);
119     REGISTER_ELEMENT(StatusCode);
120     REGISTER_ELEMENT_NOVAL(StatusDetail);
121     REGISTER_ELEMENT(StatusMessage);
122     REGISTER_TYPE(AttributeQuery);
123     REGISTER_TYPE(AuthenticationQuery);
124     REGISTER_TYPE(AuthorizationDecisionQuery);
125     REGISTER_TYPE(Request);
126     REGISTER_TYPE(Response);
127     REGISTER_TYPE(Status);
128     REGISTER_TYPE(StatusCode);
129     REGISTER_TYPE_NOVAL(StatusDetail);
130 }