Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / handler / AssertionConsumerService.h
1 /*
2  *  Copyright 2001-2009 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 shibsp/handler/AssertionConsumerService.h
19  * 
20  * Base class for handlers that create sessions by consuming SSO protocol responses. 
21  */
22
23 #ifndef __shibsp_acshandler_h__
24 #define __shibsp_acshandler_h__
25
26 #include <shibsp/handler/AbstractHandler.h>
27 #include <shibsp/handler/RemotedHandler.h>
28 #ifndef SHIBSP_LITE
29 # include <saml/binding/MessageDecoder.h>
30 # include <saml/saml1/core/Assertions.h>
31 # include <saml/saml2/metadata/Metadata.h>
32 #endif
33 #include <xmltooling/unicode.h>
34
35 namespace shibsp {
36
37     class SHIBSP_API Attribute;
38     class SHIBSP_API ResolutionContext;
39
40 #if defined (_MSC_VER)
41     #pragma warning( push )
42     #pragma warning( disable : 4250 )
43 #endif
44
45     /**
46      * Base class for handlers that create sessions by consuming SSO protocol responses.
47      */
48     class SHIBSP_API AssertionConsumerService : public AbstractHandler, public RemotedHandler 
49     {
50     public:
51         virtual ~AssertionConsumerService();
52
53         std::pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
54         void receive(DDF& in, std::ostream& out);
55
56     protected:
57         /**
58          * Constructor
59          * 
60          * @param e     root of DOM configuration
61          * @param appId ID of application that "owns" the handler
62          * @param log   a logging object to use
63          */
64         AssertionConsumerService(const xercesc::DOMElement* e, const char* appId, xmltooling::logging::Category& log);
65
66         /**
67          * Enforce address checking requirements.
68          * 
69          * @param application   reference to application receiving message
70          * @param httpRequest   client request that initiated session
71          * @param issuedTo      address for which security assertion was issued
72          */
73         void checkAddress(const Application& application, const xmltooling::HTTPRequest& httpRequest, const char* issuedTo) const;
74         
75 #ifndef SHIBSP_LITE
76         void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const;
77         
78         /**
79          * Returns a SecurityPolicy instance to use for an incoming request.
80          *
81          * <p>Allows handlers to customize the type of policy object their policy rules might require.
82          * <p>The caller <strong>MUST</strong> lock the application's MetadataProvider for the life
83          * of the returned object.
84          *
85          * @param application   reference to application receiving message
86          * @param role          identifies the role (generally IdP or SP) of the policy peer
87          * @param validate      true iff XML parsing should be done with validation
88          * @param policyId      identifies policy rules to auto-attach, defaults to the application's set
89          * @return  a new policy instance, which the caller is responsible for freeing
90          */
91         virtual opensaml::SecurityPolicy* createSecurityPolicy(
92             const Application& application, const xmltooling::QName* role, bool validate, const char* policyId
93             ) const;
94
95         /**
96          * Implement protocol-specific handling of the incoming decoded message.
97          * 
98          * <p>The result of implementing the protocol should be an exception or
99          * modifications to the request/response objects to reflect processing
100          * of the message.
101          * 
102          * @param application   reference to application receiving message
103          * @param httpRequest   client request that included message
104          * @param httpResponse  response to client
105          * @param policy        the SecurityPolicy in effect, after having evaluated the message
106          * @param settings      policy configuration settings in effect
107          * @param xmlObject     a protocol-specific message object
108          */
109         virtual void implementProtocol(
110             const Application& application,
111             const xmltooling::HTTPRequest& httpRequest,
112             xmltooling::HTTPResponse& httpResponse,
113             opensaml::SecurityPolicy& policy,
114             const PropertySet* settings,
115             const xmltooling::XMLObject& xmlObject
116             ) const=0;
117
118         /**
119          * Extracts policy-relevant assertion details.
120          * 
121          * @param assertion the incoming assertion
122          * @param protocol  the protocol family in use
123          * @param policy    SecurityPolicy to provide various components and track message data
124          */
125         virtual void extractMessageDetails(
126             const opensaml::Assertion& assertion, const XMLCh* protocol, opensaml::SecurityPolicy& policy
127             ) const;
128
129         /**
130          * Attempt SSO-initiated attribute resolution using the supplied information,
131          * including NameID and token extraction and filtering followed by
132          * secondary resolution.
133          * 
134          * <p>The caller must free the returned context handle.
135          * 
136          * @param application           reference to application receiving message
137          * @param issuer                source of SSO tokens
138          * @param protocol              SSO protocol used
139          * @param v1nameid              identifier of principal in SAML 1.x form, if any
140          * @param nameid                identifier of principal in SAML 2.0 form
141          * @param authncontext_class    method/category of authentication event, if known
142          * @param authncontext_decl     specifics of authentication event, if known
143          * @param tokens                available assertions, if any
144          */
145         ResolutionContext* resolveAttributes(
146             const Application& application,
147             const opensaml::saml2md::RoleDescriptor* issuer=NULL,
148             const XMLCh* protocol=NULL,
149             const opensaml::saml1::NameIdentifier* v1nameid=NULL,
150             const opensaml::saml2::NameID* nameid=NULL,
151             const XMLCh* authncontext_class=NULL,
152             const XMLCh* authncontext_decl=NULL,
153             const std::vector<const opensaml::Assertion*>* tokens=NULL
154             ) const;
155
156     public:
157         const char* getType() const {
158             return "AssertionConsumerService";
159         }
160
161 #endif
162     private:
163         std::pair<bool,long> processMessage(
164             const Application& application, const xmltooling::HTTPRequest& httpRequest, xmltooling::HTTPResponse& httpResponse
165             ) const;
166         
167         std::pair<bool,long> sendRedirect(
168             const Application& application,
169             const xmltooling::HTTPRequest& request,
170             xmltooling::HTTPResponse& response,
171             const char* entityID,
172             const char* relayState
173             ) const;
174
175         void maintainHistory(
176             const Application& application, const xmltooling::HTTPRequest& request, xmltooling::HTTPResponse& response, const char* entityID
177             ) const;
178                 
179 #ifndef SHIBSP_LITE
180         opensaml::MessageDecoder* m_decoder;
181         xmltooling::QName m_role;
182 #endif
183     };
184
185 #if defined (_MSC_VER)
186     #pragma warning( pop )
187 #endif
188
189 };
190
191 #endif /* __shibsp_acshandler_h__ */