ReplayCache, some decoder work, and merged schema validators into one suite.
[shibboleth/cpp-opensaml.git] / saml / binding / MessageDecoder.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file saml/binding/MessageDecoder.h\r
19  * \r
20  * Interface to SAML protocol binding message decoders. \r
21  */\r
22 \r
23 #ifndef __saml_decoder_h__\r
24 #define __saml_decoder_h__\r
25 \r
26 #include <saml/base.h>\r
27 \r
28 #include <xmltooling/XMLObject.h>\r
29 \r
30 namespace opensaml {\r
31     \r
32     class SAML_API SAMLArtifact;\r
33     class SAML_API X509TrustEngine;\r
34     namespace saml1p {\r
35         class SAML_API Response;\r
36     };\r
37     namespace saml2p {\r
38         class SAML_API SAML2Artifact;\r
39     };\r
40     namespace saml2md {\r
41         class SAML_API MetadataProvider;\r
42         class SAML_API IDPSSODescriptor;\r
43         class SAML_API RoleDescriptor;\r
44         class SAML_API SSODescriptorType;\r
45     }\r
46 \r
47     /**\r
48      * Interface to SAML protocol binding message decoders.\r
49      */\r
50     class SAML_API MessageDecoder\r
51     {\r
52         MAKE_NONCOPYABLE(MessageDecoder);\r
53     public:\r
54         virtual ~MessageDecoder() {}\r
55 \r
56         /**\r
57          * Interface to caller-supplied shim for accessing HTTP request context.\r
58          * \r
59          * To supply information from the surrounding web server environment,\r
60          * a shim must be supplied in the form of this interface to adapt the\r
61          * library to different proprietary server APIs.\r
62          */\r
63         class SAML_API HTTPRequest {\r
64             MAKE_NONCOPYABLE(HTTPRequest);\r
65         protected:\r
66             HTTPRequest() {}\r
67         public:\r
68             virtual ~HTTPRequest() {}\r
69             \r
70             /**\r
71              * Returns the HTTP method of the request (GET, POST, etc.)\r
72              * \r
73              * @return the HTTP method\r
74              */\r
75             virtual const char* getMethod() const=0;\r
76             \r
77             /**\r
78              * Returns the complete request URL, including scheme, host, port.\r
79              * \r
80              * @return the request URL\r
81              */\r
82             virtual const char* getRequestURL() const=0;\r
83             \r
84             /**\r
85              * Returns the HTTP query string appened to the request. The query\r
86              * string is returned without any decoding applied, everything found\r
87              * after the ? delimiter. \r
88              * \r
89              * @return the query string\r
90              */\r
91             virtual const char* getQueryString() const=0;\r
92             \r
93             /**\r
94              * Returns a decoded named parameter value from the query string or form body.\r
95              * If a parameter has multiple values, only one will be returned.\r
96              * \r
97              * @param name  the name of the parameter to return\r
98              * @return a single parameter value or NULL\r
99              */\r
100             virtual const char* getParameter(const char* name) const=0;\r
101 \r
102             /**\r
103              * Returns all of the decoded values of a named parameter from the query string\r
104              * or form body. All values found will be returned.\r
105              * \r
106              * @param name      the name of the parameter to return\r
107              * @param values    a vector in which to return pointers to the decoded values\r
108              * @return  the number of values returned\r
109              */            \r
110             virtual std::vector<const char*>::size_type getParameters(\r
111                 const char* name, std::vector<const char*>& values\r
112                 ) const=0;\r
113         };\r
114 \r
115         /**\r
116          * Interface to caller-supplied artifact resolution mechanism.\r
117          * \r
118          * Resolving artifacts requires internally performing a SOAP-based\r
119          * call to the artifact source, usually in a mutually authenticated fashion.\r
120          * The potential options vary widely, so the work is encapsulated by this\r
121          * interface, though of course other library facilities may be used.\r
122          * \r
123          * <p>A MessageDecoder implementation will invoke the supplied interface\r
124          * when it requires an artifact be resolved.\r
125          */\r
126         class SAML_API ArtifactResolver {\r
127             MAKE_NONCOPYABLE(ArtifactResolver);\r
128         protected:\r
129             ArtifactResolver() {}\r
130         public:\r
131             virtual ~ArtifactResolver() {}\r
132             \r
133             /**\r
134              * Resolves one or more SAML 1.x artifacts into a response containing a set of\r
135              * resolved Assertions. The caller is responsible for the resulting Response. \r
136              * \r
137              * @param artifacts         one or more SAML 1.x artifacts\r
138              * @param idpDescriptor     reference to IdP role of artifact issuer\r
139              * @param trustEngine       optional pointer to X509TrustEngine supplied to MessageDecoder\r
140              * @return the corresponding SAML Assertions wrapped in a Response.\r
141              */\r
142             virtual saml1p::Response* resolve(\r
143                 const std::vector<const SAMLArtifact*>& artifacts,\r
144                 const saml2md::IDPSSODescriptor& idpDescriptor,\r
145                 const X509TrustEngine* trustEngine=NULL\r
146                 ) const=0;\r
147 \r
148             /**\r
149              * Resolves a SAML 2.0 artifact into the corresponding SAML protocol message.\r
150              * The caller is responsible for the resulting XMLObject.\r
151              * \r
152              * @param artifact          reference to a SAML 2.0 artifact\r
153              * @param ssoDescriptor     reference to SSO role of artifact issuer (may be SP or IdP)\r
154              * @param trustEngine       optional pointer to X509TrustEngine supplied to MessageDecoder\r
155              * @return the corresponding SAML protocol message or NULL\r
156              */\r
157             virtual xmltooling::XMLObject* resolve(\r
158                 const saml2p::SAML2Artifact& artifact,\r
159                 const saml2md::SSODescriptorType& ssoDescriptor,\r
160                 const X509TrustEngine* trustEngine=NULL\r
161                 ) const=0;\r
162         };\r
163 \r
164         /**\r
165          * Provides an ArtifactResolver implementation for the MessageDecoder to use.\r
166          * The implementation's lifetime must be longer than the lifetime of this object. \r
167          * This method must be externally synchronized. \r
168          * \r
169          * @param artifactResolver   an ArtifactResolver implementation to use\r
170          */\r
171         void setArtifactResolver(ArtifactResolver* artifactResolver) {\r
172             m_artifactResolver = artifactResolver;\r
173         }\r
174         \r
175         /**\r
176          * Controls schema validation of incoming XML messages.\r
177          * This is separate from other forms of programmatic validation of objects,\r
178          * but can detect a much wider range of syntax errors. \r
179          * \r
180          * @param validate  true iff the decoder should use a validating XML parser\r
181          */\r
182         void setValidating(bool validate=true) {\r
183             m_validate = validate;\r
184         }\r
185 \r
186         /**\r
187          * Decodes an HTTP request into a SAML protocol message, and returns related\r
188          * information about the issuer of the message and whether it can be trusted.\r
189          * If the HTTP request does not contain the information necessary to decode\r
190          * the request, a NULL will be returned. Errors during the decoding process\r
191          * will be raised as exceptions.\r
192          * \r
193          * <p>Artifact-based bindings require an ArtifactResolver be set to\r
194          * turn an artifact into the corresponding message.\r
195          * \r
196          * <p>In some cases, a message may be returned but not authenticated. The caller\r
197          * should examine the issuerTrusted output value to establish this.  \r
198          * \r
199          * @param relayState        RelayState/TARGET value accompanying message\r
200          * @param issuer            role descriptor of issuing party\r
201          * @param issuerTrusted     will be true iff the message was authenticated (signed or obtained via secure backchannel)\r
202          * @param httpRequest       reference to interface for accessing HTTP message to decode\r
203          * @param metadataProvider  optional MetadataProvider instance to authenticate the message\r
204          * @param role              optional, identifies the role (generally IdP or SP) of the peer who issued the message \r
205          * @param trustEngine       optional X509TrustEngine to authenticate the message\r
206          * @return  the decoded message, or NULL if the decoder did not recognize the request content\r
207          */\r
208         virtual xmltooling::XMLObject* decode(\r
209             std::string& relayState,\r
210             const saml2md::RoleDescriptor*& issuer,\r
211             bool& issuerTrusted,\r
212             const HTTPRequest& httpRequest,\r
213             const saml2md::MetadataProvider* metadataProvider=NULL,\r
214             const xmltooling::QName* role=NULL,\r
215             const X509TrustEngine* trustEngine=NULL\r
216             ) const=0;\r
217 \r
218     protected:\r
219         MessageDecoder() : m_artifactResolver(NULL), m_validate(false) {}\r
220 \r
221         /** Pointer to an ArtifactResolver implementation. */\r
222         const ArtifactResolver* m_artifactResolver;\r
223         \r
224         /** Flag controlling schema validation. */\r
225         bool m_validate;\r
226     };\r
227 \r
228     /**\r
229      * Registers MessageDecoder plugins into the runtime.\r
230      */\r
231     void SAML_API registerMessageDecoders();\r
232 \r
233     /** MessageDecoder for SAML 1.x Browser/Artifact "binding" (really part of profile) */\r
234     #define SAML1_ARTIFACT_DECODER  "urn:oasis:names:tc:SAML:1.0:profiles:artifact-01"\r
235 \r
236     /** MessageDecoder for SAML 1.x Browser/POST "binding" (really part of profile) */\r
237     #define SAML1_POST_DECODER  "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"\r
238     \r
239     /** MessageDecoder for SAML 2.0 HTTP-Artifact binding */\r
240     #define SAML2_ARTIFACT_DECODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"\r
241 \r
242     /** MessageDecoder for SAML 2.0 HTTP-POST binding */\r
243     #define SAML2_POST_DECODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"\r
244 \r
245     /** MessageDecoder for SAML 2.0 HTTP-Redirect binding */\r
246     #define SAML2_REDIRECT_DECODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"\r
247 };\r
248 \r
249 #endif /* __saml_decoder_h__ */\r