f3003c4017e6e900a91ef133c60b3bbd74c6688b
[shibboleth/sp.git] / shibsp / ServiceProvider.h
1 /*
2  *  Copyright 2001-2010 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/ServiceProvider.h
19  * 
20  * Interface to a Shibboleth ServiceProvider instance.
21  */
22
23 #ifndef __shibsp_sp_h__
24 #define __shibsp_sp_h__
25
26 #include <shibsp/util/PropertySet.h>
27
28 #include <xmltooling/Lockable.h>
29
30 namespace xmltooling {
31     class XMLTOOL_API SOAPTransport;
32     class XMLTOOL_API StorageService;
33 };
34
35 #ifndef SHIBSP_LITE
36 namespace opensaml {
37     class SAML_API SecurityPolicyRule;
38 };
39 #endif
40
41 namespace shibsp {
42
43     class SHIBSP_API Application;
44     class SHIBSP_API Handler;
45     class SHIBSP_API ListenerService;
46     class SHIBSP_API Remoted;
47     class SHIBSP_API RequestMapper;
48     class SHIBSP_API SessionCache;
49     class SHIBSP_API SPRequest;
50     class SHIBSP_API TemplateParameters;
51 #ifndef SHIBSP_LITE
52     class SHIBSP_API SecurityPolicyProvider;
53     class SHIBSP_API TransactionLog;
54 #endif
55
56     /**
57      * Interface to a Shibboleth ServiceProvider instance.
58      * 
59      * <p>A ServiceProvider exposes configuration and infrastructure services required
60      * by the SP implementation, allowing a flexible configuration format.
61      */
62         class SHIBSP_API ServiceProvider : public virtual xmltooling::Lockable, public virtual PropertySet
63     {
64         MAKE_NONCOPYABLE(ServiceProvider);
65     protected:
66         ServiceProvider();
67     public:
68         virtual ~ServiceProvider();
69         
70         /**
71          * Loads a configuration and prepares the instance for use.
72          * 
73          * <p>Implemented as a separate method so that services can rely on
74          * other services while they initialize by accessing the ServiceProvider
75          * from the SPConfig singleton.
76          */
77         virtual void init()=0;
78
79 #ifndef SHIBSP_LITE
80         /**
81          * Returns a TransactionLog instance.
82          * 
83          * @return  a TransactionLog instance
84          */
85         virtual TransactionLog* getTransactionLog() const=0;
86
87         /**
88          * Returns a StorageService instance based on an ID.
89          * 
90          * @param id    a nullptr-terminated key identifying the StorageService to the configuration 
91          * @return  a StorageService if available, or nullptr
92          */
93         virtual xmltooling::StorageService* getStorageService(const char* id) const=0;
94 #endif
95
96         /**
97          * Returns a SessionCache instance.
98          * 
99          * @param required  true iff an exception should be thrown if no SessionCache is available
100          * @return  a SessionCache
101          */
102         virtual SessionCache* getSessionCache(bool required=true) const=0;
103
104         /**
105          * Returns a ListenerService instance.
106          * 
107          * @param required  true iff an exception should be thrown if no ListenerService is available
108          * @return  a ListenerService
109          */
110         virtual ListenerService* getListenerService(bool required=true) const=0;
111         
112 #ifndef SHIBSP_LITE
113         /**
114          * Returns a SecurityPolicyProvider instance.
115          *
116          * @param required true iff an exception should be thrown if no SecurityPolicyProvider is available
117          * @return  a SecurityPolicyProvider
118          */
119         virtual SecurityPolicyProvider* getSecurityPolicyProvider(bool required=true) const;
120
121         /**
122          * @deprecated
123                  * Returns the security policy settings for an identified policy.
124          *
125                  * @param id    identifies the policy to return, or nullptr for default
126          * @return a PropertySet
127                  */
128         virtual const PropertySet* getPolicySettings(const char* id) const=0;
129
130         /**
131          * @deprecated
132                  * Returns the security policy rules for an identified policy.
133          *
134                  * @param id    identifies the policy to return, or nullptr for default
135          * @return an array of policy rules
136                  */
137         virtual const std::vector<const opensaml::SecurityPolicyRule*>& getPolicyRules(const char* id) const=0;
138
139         /**
140          * Sets implementation-specific transport options.
141          *
142          * @param transport a SOAPTransport object
143          * @return  true iff all options were successfully set
144          */
145         virtual bool setTransportOptions(xmltooling::SOAPTransport& transport) const=0;
146 #endif
147
148         /**
149          * Returns a RequestMapper instance.
150          * 
151          * @param required  true iff an exception should be thrown if no RequestMapper is available
152          * @return  a RequestMapper
153          */
154         virtual RequestMapper* getRequestMapper(bool required=true) const=0;
155         
156         /**
157          * Returns an Application instance matching the specified ID.
158          * 
159          * @param applicationId the ID of the application, or nullptr for the default
160          * @return  pointer to the application, or nullptr
161          */
162         virtual const Application* getApplication(const char* applicationId) const=0;
163
164         /**
165          * Enforces requirements for an authenticated session.
166          * 
167          * <p>If the return value's first member is true, then request processing should terminate
168          * with the second member as a status value. If false, processing can continue. 
169          * 
170          * @param request   SP request interface
171          * @param handler   true iff a request to a registered Handler location can be directly executed
172          * @return a pair containing a "request completed" indicator and a server-specific response code
173          */
174         virtual std::pair<bool,long> doAuthentication(SPRequest& request, bool handler=false) const;
175         
176         /**
177          * Enforces authorization requirements based on the authenticated session.
178          * 
179          * <p>If the return value's first member is true, then request processing should terminate
180          * with the second member as a status value. If false, processing can continue. 
181          * 
182          * @param request   SP request interface
183          * @return a pair containing a "request completed" indicator and a server-specific response code
184          */
185         virtual std::pair<bool,long> doAuthorization(SPRequest& request) const;
186         
187         /**
188          * Publishes session contents to the request in the form of headers or environment variables.
189          * 
190          * <p>If the return value's first member is true, then request processing should terminate
191          * with the second member as a status value. If false, processing can continue. 
192          * 
193          * @param request   SP request interface
194          * @param requireSession    set to true iff an error should result if no session exists 
195          * @return a pair containing a "request completed" indicator and a server-specific response code
196          */
197         virtual std::pair<bool,long> doExport(SPRequest& request, bool requireSession=true) const;
198
199         /**
200          * Services requests for registered Handler locations. 
201          * 
202          * <p>If the return value's first member is true, then request processing should terminate
203          * with the second member as a status value. If false, processing can continue. 
204          * 
205          * @param request   SP request interface
206          * @return a pair containing a "request completed" indicator and a server-specific response code
207          */
208         virtual std::pair<bool,long> doHandler(SPRequest& request) const;
209
210         /**
211          * Register for a message. Returns existing remote service, allowing message hooking.
212          *
213          * @param address   message address to register
214          * @param svc       pointer to remote service
215          * @return  previous service registered for message, if any
216          */
217         virtual Remoted* regListener(const char* address, Remoted* svc);
218
219         /**
220          * Unregisters service from an address, possibly restoring an original.
221          *
222          * @param address   message address to modify
223          * @param current   pointer to unregistering service
224          * @param restore   service to "restore" registration for
225          * @return  true iff the current service was still registered
226          */
227         virtual bool unregListener(const char* address, Remoted* current, Remoted* restore=nullptr);
228
229         /**
230          * Returns current service registered at an address, if any.
231          *
232          * @param address message address to access
233          * @return  registered service, or nullptr
234          */
235         virtual Remoted* lookupListener(const char* address) const;
236
237     private:
238         std::map<std::string,Remoted*> m_listenerMap;
239     };
240
241     /**
242      * Registers ServiceProvider classes into the runtime.
243      */
244     void SHIBSP_API registerServiceProviders();
245
246     /** SP based on integrated XML and native server configuration. */
247     #define XML_SERVICE_PROVIDER "XML"
248 };
249
250 #endif /* __shibsp_sp_h__ */