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