0bb70eef391d7b7a0887fd36da13bbe4c7d0a535
[shibboleth/cpp-opensaml.git] / saml / binding / MessageEncoder.h
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  * @file saml/binding/MessageEncoder.h
19  * 
20  * Interface to SAML protocol binding message encoders. 
21  */
22
23 #ifndef __saml_encoder_h__
24 #define __saml_encoder_h__
25
26 #include <saml/binding/GenericResponse.h>
27
28 #include <istream>
29 #include <xmltooling/XMLObject.h>
30 #include <xmltooling/security/Credential.h>
31
32 namespace opensaml {
33
34     class SAML_API SAMLArtifact;
35     namespace saml2p {
36         class SAML_API SAML2Artifact;
37     };
38
39     /**
40      * Interface to SAML protocol binding message encoders.
41      */
42     class SAML_API MessageEncoder
43     {
44         MAKE_NONCOPYABLE(MessageEncoder);
45     public:
46         virtual ~MessageEncoder() {}
47
48         /**
49          * Indicates whether the encoding format requires that messages be as compact as possible.
50          *
51          * @return  true iff the encoding has size constraints
52          */
53         virtual bool isCompact() const {
54             return false;
55         }
56
57         /**
58          * Interface to caller-supplied artifact generation mechanism.
59          * 
60          * Generating an artifact for storage and retrieval requires knowledge of
61          * the sender's SourceID (or sometimes SourceLocation), and the relying party's
62          * preferred artifact type. This information can be supplied using whatever
63          * configuration or defaults are appropriate for the SAML application.
64          * A MessageEncoder implementation will invoke the supplied generator interface
65          * when it requires an artifact be created.
66          */
67         class SAML_API ArtifactGenerator {
68             MAKE_NONCOPYABLE(ArtifactGenerator);
69         protected:
70             ArtifactGenerator() {}
71         public:
72             virtual ~ArtifactGenerator() {}
73             
74             /**
75              * Generate a SAML 1.x artifact suitable for consumption by the relying party.
76              * 
77              * @param relyingParty  the party that will recieve the artifact
78              * @return a SAML 1.x artifact with a random assertion handle
79              */
80             virtual SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const=0;
81
82             /**
83              * Generate a SAML 2.0 artifact suitable for consumption by the relying party.
84              * 
85              * @param relyingParty  the party that will recieve the artifact
86              * @return a SAML 2.0 artifact with a random message handle
87              */
88             virtual saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const=0;
89         };
90
91         /**
92          * Provides an ArtifactGenerator implementation for the MessageEncoder to use.
93          * The implementation's lifetime must be longer than the lifetime of this object. 
94          * This method must be externally synchronized. 
95          * 
96          * @param artifactGenerator   an ArtifactGenerator implementation to use
97          */
98         void setArtifactGenerator(ArtifactGenerator* artifactGenerator) {
99             m_artifactGenerator = artifactGenerator;
100         }
101         
102         /**
103          * Encodes an XML object/message into a binding- and transport-specific response.
104          * The XML content cannot have a parent object, and any existing references to
105          * the content will be invalidated if the encode method returns successfully.
106          * 
107          * If a CredentialResolver is supplied, the message is also signed in a
108          * binding-specific manner. The CredentialResolver <strong>MUST</strong>
109          * be locked by the caller. 
110          * 
111          * <p>Artifact-based bindings require an ArtifactGenerator be set to
112          * produce an artifact suitable for the intended recipient.
113          * 
114          * @param genericResponse   reference to interface for sending transport response      
115          * @param xmlObject         XML message to encode
116          * @param destination       destination URL for message
117          * @param recipientID       optional entityID of message recipient
118          * @param relayState        optional RelayState value to accompany message
119          * @param credential        optional Credential to supply signing key
120          * @param signatureAlg      optional signature algorithm identifier
121          * @param digestAlg         optional reference digest algorithm identifier
122          */
123         virtual long encode(
124             GenericResponse& genericResponse,
125             xmltooling::XMLObject* xmlObject,
126             const char* destination,
127             const char* recipientID=NULL,
128             const char* relayState=NULL,
129             const xmltooling::Credential* credential=NULL,
130             const XMLCh* signatureAlg=NULL,
131             const XMLCh* digestAlg=NULL
132             ) const=0;
133
134     protected:
135         MessageEncoder() : m_artifactGenerator(NULL) {}
136         
137         /** Pointer to an ArtifactGenerator implementation. */
138         const ArtifactGenerator* m_artifactGenerator;
139     };
140
141     /**
142      * Registers MessageEncoder plugins into the runtime.
143      */
144     void SAML_API registerMessageEncoders();
145 };
146
147 #endif /* __saml_encoder_h__ */