Merged trust engines back into a unified version, made metadata roles a "KeyInfoSource".
[shibboleth/cpp-opensaml.git] / saml / binding / MessageDecoder.h
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  * @file saml/binding/MessageDecoder.h
19  * 
20  * Interface to SAML protocol binding message decoders. 
21  */
22
23 #ifndef __saml_decoder_h__
24 #define __saml_decoder_h__
25
26 #include <saml/binding/GenericRequest.h>
27 #include <saml/binding/SecurityPolicy.h>
28 #include <xmltooling/XMLObject.h>
29
30 namespace opensaml {
31     
32     class SAML_API SAMLArtifact;
33     namespace saml1p {
34         class SAML_API Response;
35     };
36     namespace saml2p {
37         class SAML_API SAML2Artifact;
38         class SAML_API ArtifactResponse;
39     };
40     namespace saml2md {
41         class SAML_API MetadataProvider;
42         class SAML_API IDPSSODescriptor;
43         class SAML_API RoleDescriptor;
44         class SAML_API SSODescriptorType;
45     };
46
47     /**
48      * Interface to SAML protocol binding message decoders.
49      */
50     class SAML_API MessageDecoder
51     {
52         MAKE_NONCOPYABLE(MessageDecoder);
53     public:
54         virtual ~MessageDecoder() {}
55
56         /**
57          * Interface to caller-supplied artifact resolution mechanism.
58          * 
59          * Resolving artifacts requires internally performing a SOAP-based
60          * call to the artifact source, usually in a mutually authenticated fashion.
61          * The potential options vary widely, so the work is encapsulated by this
62          * interface, though of course other library facilities may be used.
63          * 
64          * <p>A MessageDecoder implementation will invoke the supplied interface
65          * when it requires an artifact be resolved.
66          */
67         class SAML_API ArtifactResolver {
68             MAKE_NONCOPYABLE(ArtifactResolver);
69         protected:
70             ArtifactResolver() {}
71             
72             /** Flag controlling schema validation. */
73             bool m_validate;
74
75         public:
76             virtual ~ArtifactResolver() {}
77
78             /**
79              * Controls schema validation of incoming XML messages.
80              * This is separate from other forms of programmatic validation of objects,
81              * but can detect a much wider range of syntax errors. 
82              * 
83              * @param validate  true iff the resolver should use a validating XML parser
84              */
85             void setValidating(bool validate=true) {
86                 m_validate = validate;
87             }
88             
89             /**
90              * Resolves one or more SAML 1.x artifacts into a response containing a set of
91              * resolved Assertions. The caller is responsible for the resulting Response.
92              * The supplied SecurityPolicy is used to access caller-supplied infrastructure
93              * and to pass back the result of authenticating the resolution process. 
94              * 
95              * @param artifacts         one or more SAML 1.x artifacts
96              * @param idpDescriptor     reference to IdP role of artifact issuer
97              * @param policy            reference to policy containing rules, MetadataProvider, TrustEngine, etc. 
98              * @return the corresponding SAML Assertions wrapped in a Response.
99              */
100             virtual saml1p::Response* resolve(
101                 const std::vector<SAMLArtifact*>& artifacts,
102                 const saml2md::IDPSSODescriptor& idpDescriptor,
103                 SecurityPolicy& policy
104                 ) const=0;
105
106             /**
107              * Resolves a SAML 2.0 artifact into the corresponding SAML protocol message.
108              * The caller is responsible for the resulting ArtifactResponse message.
109              * The supplied SecurityPolicy is used to access caller-supplied infrastructure
110              * and to pass back the result of authenticating the resolution process. 
111              * 
112              * @param artifact          reference to a SAML 2.0 artifact
113              * @param ssoDescriptor     reference to SSO role of artifact issuer (may be SP or IdP)
114              * @param policy            reference to policy containing rules, MetadataProvider, TrustEngine, etc. 
115              * @return the corresponding SAML protocol message or NULL
116              */
117             virtual saml2p::ArtifactResponse* resolve(
118                 const saml2p::SAML2Artifact& artifact,
119                 const saml2md::SSODescriptorType& ssoDescriptor,
120                 SecurityPolicy& policy
121                 ) const=0;
122         };
123
124         /**
125          * Provides an ArtifactResolver implementation for the MessageDecoder to use.
126          * The implementation's lifetime must be longer than the lifetime of this object. 
127          * This method must be externally synchronized. 
128          * 
129          * @param artifactResolver   an ArtifactResolver implementation to use
130          */
131         void setArtifactResolver(ArtifactResolver* artifactResolver) {
132             m_artifactResolver = artifactResolver;
133             if (m_artifactResolver)
134                 m_artifactResolver->setValidating(m_validate);
135         }
136         
137         /**
138          * Controls schema validation of incoming XML messages.
139          * This is separate from other forms of programmatic validation of objects,
140          * but can detect a much wider range of syntax errors. 
141          * 
142          * @param validate  true iff the decoder should use a validating XML parser
143          */
144         void setValidating(bool validate=true) {
145             m_validate = validate;
146             if (m_artifactResolver)
147                 m_artifactResolver->setValidating(m_validate);
148         }
149
150         /**
151          * Decodes a transport request into a SAML protocol message, and evaluates it
152          * against a supplied SecurityPolicy. If the transport request does not contain
153          * the information necessary to decode the request, NULL will be returned.
154          * Errors during the decoding process will be raised as exceptions.
155          * 
156          * <p>Artifact-based bindings require an ArtifactResolver be set to
157          * turn an artifact into the corresponding message.
158          * 
159          * @param relayState        will be set to RelayState/TARGET value accompanying message
160          * @param genericRequest    reference to interface for accessing transport request to decode
161          * @param policy            reference to policy containing rules, MetadataProvider, TrustEngine, etc. 
162          * @return  the decoded message, or NULL if the decoder did not recognize the request content
163          */
164         virtual xmltooling::XMLObject* decode(
165             std::string& relayState,
166             const GenericRequest& genericRequest,
167             SecurityPolicy& policy
168             ) const=0;
169
170     protected:
171         MessageDecoder() : m_artifactResolver(NULL), m_validate(false) {}
172
173         /** Pointer to an ArtifactResolver implementation. */
174         ArtifactResolver* m_artifactResolver;
175         
176         /** Flag controlling schema validation. */
177         bool m_validate;
178     };
179
180     /**
181      * Registers MessageDecoder plugins into the runtime.
182      */
183     void SAML_API registerMessageDecoders();
184 };
185
186 #endif /* __saml_decoder_h__ */