ReplayCache, some decoder work, and merged schema validators into one suite.
[shibboleth/cpp-opensaml.git] / saml / binding / MessageEncoder.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/MessageEncoder.h\r
19  * \r
20  * Interface to SAML protocol binding message encoders. \r
21  */\r
22 \r
23 #ifndef __saml_encoder_h__\r
24 #define __saml_encoder_h__\r
25 \r
26 #include <saml/base.h>\r
27 \r
28 #include <map>\r
29 #include <string>\r
30 #include <xmltooling/XMLObject.h>\r
31 #include <xmltooling/signature/CredentialResolver.h>\r
32 \r
33 namespace opensaml {\r
34 \r
35     class SAML_API SAMLArtifact;\r
36     namespace saml2p {\r
37         class SAML_API SAML2Artifact;\r
38     };\r
39 \r
40     /**\r
41      * Interface to SAML protocol binding message encoders.\r
42      */\r
43     class SAML_API MessageEncoder\r
44     {\r
45         MAKE_NONCOPYABLE(MessageEncoder);\r
46     public:\r
47         virtual ~MessageEncoder() {}\r
48 \r
49         /**\r
50          * Interface to caller-supplied artifact generation mechanism.\r
51          * \r
52          * Generating an artifact for storage and retrieval requires knowledge of\r
53          * the sender's SourceID (or sometimes SourceLocation), and the relying party's\r
54          * preferred artifact type. This information can be supplied using whatever\r
55          * configuration or defaults are appropriate for the SAML application.\r
56          * A MessageEncoder implementation will invoke the supplied generator interface\r
57          * when it requires an artifact be created.\r
58          */\r
59         class SAML_API ArtifactGenerator {\r
60             MAKE_NONCOPYABLE(ArtifactGenerator);\r
61         protected:\r
62             ArtifactGenerator() {}\r
63         public:\r
64             virtual ~ArtifactGenerator() {}\r
65             \r
66             /**\r
67              * Generate a SAML 1.x artifact suitable for consumption by the relying party.\r
68              * \r
69              * @param relyingParty  the party that will recieve the artifact\r
70              * @return a SAML 1.x artifact with a random assertion handle\r
71              */\r
72             virtual SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const=0;\r
73 \r
74             /**\r
75              * Generate a SAML 2.0 artifact suitable for consumption by the relying party.\r
76              * \r
77              * @param relyingParty  the party that will recieve the artifact\r
78              * @return a SAML 2.0 artifact with a random message handle\r
79              */\r
80             virtual saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const=0;\r
81         };\r
82 \r
83         /**\r
84          * Provides an ArtifactGenerator implementation for the MessageEncoder to use.\r
85          * The implementation's lifetime must be longer than the lifetime of this object. \r
86          * This method must be externally synchronized. \r
87          * \r
88          * @param artifactGenerator   an ArtifactGenerator implementation to use\r
89          */\r
90         void setArtifactGenerator(ArtifactGenerator* artifactGenerator) {\r
91             m_artifactGenerator = artifactGenerator;\r
92         }\r
93         \r
94         /**\r
95          * Encodes an XML object/message into a set of binding-specific data "fields".\r
96          * The XML content cannot have a parent object, and any existing references to\r
97          * the content will be invalidated if the encode method returns successfully.\r
98          * \r
99          * If a CredentialResolver is supplied, the message is also signed in a\r
100          * binding-specific manner. The CredentialResolver <strong>MUST</strong>\r
101          * be locked by the caller. \r
102          * \r
103          * <p>An embedded URLEncoder instance may be required by some bindings\r
104          * in order to produce predictable signature input.\r
105          * \r
106          * <p>Artifact-based bindings require an ArtifactGenerator be set to\r
107          * produce an artifact suitable for the intended recipient.\r
108          * \r
109          * <p>Note that the name/value pairs resulting from the encoding operation are\r
110          * <strong>NOT</strong> URL-encoded or otherwise transformed. It is the caller's\r
111          * responsibility to apply any necessary encoding when preparing the data for\r
112          * transport.\r
113          * \r
114          * @param outputFields      name/value pairs containing the results of encoding the message\r
115          * @param xmlObject         XML object/message to encode\r
116          * @param recipientID       optional entityID of message recipient\r
117          * @param relayState        optional RelayState value to accompany message\r
118          * @param credResolver      optional CredentialResolver instance to supply signing material\r
119          * @param sigAlgorithm      optional signature algorithm identifier\r
120          */\r
121         virtual void encode(\r
122             std::map<std::string,std::string>& outputFields,\r
123             xmltooling::XMLObject* xmlObject,\r
124             const char* recipientID=NULL,\r
125             const char* relayState=NULL,\r
126             const xmlsignature::CredentialResolver* credResolver=NULL,\r
127             const XMLCh* sigAlgorithm=NULL\r
128             ) const=0;\r
129 \r
130     protected:\r
131         MessageEncoder() : m_artifactGenerator(NULL) {}\r
132         \r
133         /**\r
134          * Helper function to build a new XML Signature with KeyInfo, based\r
135          * on the supplied CredentialResolver.\r
136          * \r
137          * @param credResolver      CredentialResolver instance to supply signing material\r
138          * @param sigAlgorithm      optional signature algorithm identifier\r
139          * @return  a new Signature object\r
140          */\r
141         xmlsignature::Signature* buildSignature(\r
142             const xmlsignature::CredentialResolver* credResolver,\r
143             const XMLCh* sigAlgorithm=NULL\r
144             ) const;\r
145         \r
146         /** Pointer to an ArtifactGenerator implementation. */\r
147         const ArtifactGenerator* m_artifactGenerator;\r
148     };\r
149 \r
150     /**\r
151      * Registers MessageEncoder plugins into the runtime.\r
152      */\r
153     void SAML_API registerMessageEncoders();\r
154 \r
155     /** MessageEncoder for SAML 1.x Browser/Artifact "binding" (really part of profile) */\r
156     #define SAML1_ARTIFACT_ENCODER  "urn:oasis:names:tc:SAML:1.0:profiles:artifact-01"\r
157 \r
158     /** MessageEncoder for SAML 1.x Browser/POST "binding" (really part of profile) */\r
159     #define SAML1_POST_ENCODER  "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"\r
160     \r
161     /** MessageEncoder for SAML 2.0 HTTP-Artifact binding */\r
162     #define SAML2_ARTIFACT_ENCODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"\r
163 \r
164     /** MessageEncoder for SAML 2.0 HTTP-POST binding */\r
165     #define SAML2_POST_ENCODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"\r
166 \r
167     /** MessageEncoder for SAML 2.0 HTTP-Redirect binding */\r
168     #define SAML2_REDIRECT_ENCODER "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"\r
169 };\r
170 \r
171 #endif /* __saml_encoder_h__ */\r