49a334ef8b84aec950623fde5d6db28e63dce584
[shibboleth/sp.git] / shibsp / SessionCache.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 shibsp/SessionCache.h
19  * 
20  * Caches and manages user sessions
21  */
22
23 #ifndef __shibsp_sessioncache_h__
24 #define __shibsp_sessioncache_h__
25
26 #include <shibsp/base.h>
27
28 #ifndef SHIBSP_LITE
29 # include <saml/saml1/core/Assertions.h>
30 # include <saml/saml2/metadata/Metadata.h>
31 #endif
32 #include <xmltooling/Lockable.h>
33
34 namespace shibsp {
35
36     class SHIBSP_API Application;
37     class SHIBSP_API Attribute;
38
39     class SHIBSP_API Session : public virtual xmltooling::Lockable
40     {
41         MAKE_NONCOPYABLE(Session);
42     protected:
43         Session() {}
44         virtual ~Session() {}
45     public:
46         /**
47          * Returns the session key.
48          *
49          * @return unique ID of session
50          */
51         virtual const char* getID() const=0;
52
53         /**
54          * Returns the session expiration.
55          *
56          * @return  the session's expiration time or 0 for none
57          */
58         virtual time_t getExpiration() const=0;
59
60         /**
61          * Returns the last access time of the session.
62          *
63          * @return  the session's last access time
64          */
65         virtual time_t getLastAccess() const=0;
66
67         /**
68          * Returns the address of the client associated with the session.
69          * 
70          * @return  the client's network address
71          */
72         virtual const char* getClientAddress() const=0;
73
74         /**
75          * Returns the entityID of the IdP that initiated the session.
76          * 
77          * @return the IdP's entityID
78          */
79         virtual const char* getEntityID() const=0;
80
81         /**
82          * Returns the protocol family used to initiate the session.
83          *
84          * @return the protocol constant that represents the general SSO protocol used
85          */
86         virtual const char* getProtocol() const=0;
87
88         /**
89          * Returns the UTC timestamp on the authentication event at the IdP.
90          * 
91          * @return  the UTC authentication timestamp 
92          */
93         virtual const char* getAuthnInstant() const=0;
94
95 #ifndef SHIBSP_LITE
96         /**
97          * Returns the NameID associated with a session.
98          * 
99          * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
100          * 
101          * @return a SAML 2.0 NameID associated with the session, if any
102          */
103         virtual const opensaml::saml2::NameID* getNameID() const=0;
104 #endif
105
106         /**
107          * Returns the SessionIndex provided with the session.
108          * 
109          * @return the SessionIndex from the original SSO assertion, if any
110          */
111         virtual const char* getSessionIndex() const=0;
112
113         /**
114          * Returns a URI containing an AuthnContextClassRef provided with the session.
115          * 
116          * <p>SAML 1.x AuthenticationMethods will be returned as class references.
117          * 
118          * @return  a URI identifying the authentication context class
119          */
120         virtual const char* getAuthnContextClassRef() const=0;
121
122         /**
123          * Returns a URI containing an AuthnContextDeclRef provided with the session.
124          * 
125          * @return  a URI identifying the authentication context declaration
126          */
127         virtual const char* getAuthnContextDeclRef() const=0;
128         
129         /**
130          * Returns the resolved attributes associated with the session.
131          * 
132          * @return an immutable array of attributes
133          */
134         virtual const std::vector<Attribute*>& getAttributes() const=0;
135
136         /**
137          * Returns the resolved attributes associated with the session, indexed by ID
138          * 
139          * @return an immutable map of attributes keyed by attribute ID
140          */
141         virtual const std::multimap<std::string,const Attribute*>& getIndexedAttributes() const=0;
142         
143         /**
144          * Returns the identifiers of the assertion(s) cached by the session.
145          * 
146          * <p>The SSO assertion is guaranteed to be first in the set.
147          * 
148          * @return  an immutable array of AssertionID values
149          */
150         virtual const std::vector<const char*>& getAssertionIDs() const=0;
151         
152 #ifndef SHIBSP_LITE
153         /**
154          * Adds additional attributes to the session.
155          * 
156          * @param attributes    reference to an array of Attributes to cache (will be freed by cache)
157          */
158         virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
159
160         /**
161          * Returns an assertion cached by the session.
162          * 
163          * @param id    identifier of the assertion to retrieve
164          * @return pointer to assertion, or NULL
165          */
166         virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
167         
168         /**
169          * Stores an assertion in the session.
170          * 
171          * @param assertion pointer to an assertion to cache (will be freed by cache)
172          */
173         virtual void addAssertion(opensaml::Assertion* assertion)=0;        
174 #endif
175     };
176     
177     /**
178      * Creates and manages user sessions
179      * 
180      * The cache abstracts a persistent (meaning across requests) cache of
181      * instances of the Session interface. Creation of new entries and entry
182      * lookup are confined to this interface to enable the implementation to
183      * remote and/or optimize calls by implementing custom versions of the
184      * Session interface as required.
185      */
186     class SHIBSP_API SessionCache
187     {
188         MAKE_NONCOPYABLE(SessionCache);
189     protected:
190     
191         /**
192          * Constructor
193          * 
194          * <p>The following XML content is supported to configure the cache:
195          * <dl>
196          *  <dt>cacheTimeout</dt>
197          *  <dd>attribute containing maximum lifetime in seconds for unused sessions to remain in cache</dd>
198          * </dl>
199          * 
200          * @param e                 root of DOM tree to configure the cache
201          * @param defaultTimeout    default cacheTimeout setting to use if none specified in DOM
202          */
203         SessionCache(const xercesc::DOMElement* e, unsigned long defaultTimeout=3600);
204         
205         /** Maximum lifetime in seconds for unused sessions to be cached. */
206         unsigned long m_cacheTimeout;
207         
208     public:
209         virtual ~SessionCache() {}
210         
211 #ifndef SHIBSP_LITE
212         /**
213          * Inserts a new session into the cache.
214          * 
215          * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
216          * 
217          * @param expires           expiration time of session
218          * @param application       reference to Application that owns the Session
219          * @param client_addr       network address of client
220          * @param issuer            issuing metadata of assertion issuer, if known
221          * @param protocol          protocol family used to initiate the session
222          * @param nameid            principal identifier, normalized to SAML 2, if any
223          * @param authn_instant     UTC timestamp of authentication at IdP, if known
224          * @param session_index     index of session between principal and IdP, if any
225          * @param authncontext_class    method/category of authentication event, if known
226          * @param authncontext_decl specifics of authentication event, if known
227          * @param tokens            assertions to cache with session, if any
228          * @param attributes        optional array of resolved Attributes to cache with session
229          * @return  newly created session's key
230          */
231         virtual std::string insert(
232             time_t expires,
233             const Application& application,
234             const char* client_addr=NULL,
235             const opensaml::saml2md::EntityDescriptor* issuer=NULL,
236             const XMLCh* protocol=NULL,
237             const opensaml::saml2::NameID* nameid=NULL,
238             const XMLCh* authn_instant=NULL,
239             const XMLCh* session_index=NULL,
240             const XMLCh* authncontext_class=NULL,
241             const XMLCh* authncontext_decl=NULL,
242             const std::vector<const opensaml::Assertion*>* tokens=NULL,
243             const std::vector<Attribute*>* attributes=NULL
244             )=0;
245
246         /**
247          * Returns active sessions that match particular parameters and records the logout
248          * to prevent race conditions.
249          *
250          * <p>On exit, the mapping between these sessions and the associated information MAY be
251          * removed by the cache, so subsequent calls to this method may not return anything.
252          *
253          * <p>Until logout expiration, any attempt to create a session with the same parameters
254          * will be blocked by the cache.
255          * 
256          * @param issuer        source of session(s)
257          * @param nameid        name identifier associated with the session(s) to terminate
258          * @param indexes       indexes of sessions, or NULL for all sessions associated with other parameters
259          * @param expires       logout expiration
260          * @param application   reference to Application that owns the session(s)
261          * @param sessions      on exit, contains the IDs of the matching sessions found
262          */
263         virtual std::vector<std::string>::size_type logout(
264             const opensaml::saml2md::EntityDescriptor* issuer,
265             const opensaml::saml2::NameID& nameid,
266             const std::set<std::string>* indexes,
267             time_t expires,
268             const Application& application,
269             std::vector<std::string>& sessions
270             )=0;
271
272         /**
273          * Determines whether a given session (based on its ID) matches a set of input
274          * criteria.
275          * 
276          * @param key           session key to check
277          * @param issuer        required source of session(s)
278          * @param nameid        required name identifier
279          * @param indexes       session indexes
280          * @param application   reference to Application that owns the Session
281          * @return  true iff the session matches the input criteria
282          */
283         virtual bool matches(
284             const char* key,
285             const opensaml::saml2md::EntityDescriptor* issuer,
286             const opensaml::saml2::NameID& nameid,
287             const std::set<std::string>* indexes,
288             const Application& application
289             )=0;
290
291         /**
292          * Executes a test of the cache's general health.
293          */
294         virtual void test()=0;
295 #endif
296
297         /**
298          * Locates an existing session.
299          * 
300          * <p>If the client address is supplied, then a check will be performed against
301          * the address recorded in the record.
302          * 
303          * @param key           session key
304          * @param application   reference to Application that owns the Session
305          * @param client_addr   network address of client (if known)
306          * @param timeout       inactivity timeout to enforce (0 for none, NULL to bypass check/update of last access)
307          * @return  pointer to locked Session, or NULL
308          */
309         virtual Session* find(
310             const char* key, const Application& application, const char* client_addr=NULL, time_t* timeout=NULL
311             )=0;
312             
313         /**
314          * Deletes an existing session.
315          * 
316          * @param key           session key
317          * @param application   reference to Application that owns the Session
318          */
319         virtual void remove(const char* key, const Application& application)=0;
320     };
321
322 #ifndef SHIBSP_LITE
323     /** SessionCache implementation backed by a StorageService. */
324     #define STORAGESERVICE_SESSION_CACHE    "StorageService"
325 #endif
326
327     /** SessionCache implementation for lite builds that delegates to a remoted version. */
328     #define REMOTED_SESSION_CACHE    "Remoted"
329
330     /**
331      * Registers SessionCache classes into the runtime.
332      */
333     void SHIBSP_API registerSessionCaches();
334 };
335
336 #endif /* __shibsp_sessioncache_h__ */