2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file shibsp/SessionCache.h
20 * Caches and manages user sessions
23 #ifndef __shibsp_sessioncache_h__
24 #define __shibsp_sessioncache_h__
26 #include <shibsp/base.h>
28 # include <saml/saml1/core/Assertions.h>
29 # include <saml/saml2/metadata/Metadata.h>
31 #include <xmltooling/Lockable.h>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/io/HTTPResponse.h>
37 class SHIBSP_API Application;
38 class SHIBSP_API Attribute;
41 * Encapsulates access to a user's security session.
43 * <p>The SessionCache does not itself require locking to manage
44 * concurrency, but access to each Session is generally exclusive
45 * or at least controlled, and the caller must unlock a Session
48 class SHIBSP_API Session : public virtual xmltooling::Lockable
50 MAKE_NONCOPYABLE(Session);
56 * Returns the session key.
58 * @return unique ID of session
60 virtual const char* getID() const=0;
63 * Returns the session's application ID.
65 * @return unique ID of application bound to session
67 virtual const char* getApplicationID() const=0;
70 * Returns the session expiration.
72 * @return the session's expiration time or 0 for none
74 virtual time_t getExpiration() const=0;
77 * Returns the last access time of the session.
79 * @return the session's last access time
81 virtual time_t getLastAccess() const=0;
84 * Returns the address of the client associated with the session.
86 * @return the client's network address
88 virtual const char* getClientAddress() const=0;
91 * Returns the entityID of the IdP that initiated the session.
93 * @return the IdP's entityID
95 virtual const char* getEntityID() const=0;
98 * Returns the protocol family used to initiate the session.
100 * @return the protocol constant that represents the general SSO protocol used
102 virtual const char* getProtocol() const=0;
105 * Returns the UTC timestamp on the authentication event at the IdP.
107 * @return the UTC authentication timestamp
109 virtual const char* getAuthnInstant() const=0;
113 * Returns the NameID associated with a session.
115 * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
117 * @return a SAML 2.0 NameID associated with the session, if any
119 virtual const opensaml::saml2::NameID* getNameID() const=0;
123 * Returns the SessionIndex provided with the session.
125 * @return the SessionIndex from the original SSO assertion, if any
127 virtual const char* getSessionIndex() const=0;
130 * Returns a URI containing an AuthnContextClassRef provided with the session.
132 * <p>SAML 1.x AuthenticationMethods will be returned as class references.
134 * @return a URI identifying the authentication context class
136 virtual const char* getAuthnContextClassRef() const=0;
139 * Returns a URI containing an AuthnContextDeclRef provided with the session.
141 * @return a URI identifying the authentication context declaration
143 virtual const char* getAuthnContextDeclRef() const=0;
146 * Returns the resolved attributes associated with the session.
148 * @return an immutable array of attributes
150 virtual const std::vector<Attribute*>& getAttributes() const=0;
153 * Returns the resolved attributes associated with the session, indexed by ID
155 * @return an immutable map of attributes keyed by attribute ID
157 virtual const std::multimap<std::string,const Attribute*>& getIndexedAttributes() const=0;
160 * Returns the identifiers of the assertion(s) cached by the session.
162 * <p>The SSO assertion is guaranteed to be first in the set.
164 * @return an immutable array of AssertionID values
166 virtual const std::vector<const char*>& getAssertionIDs() const=0;
170 * Adds additional attributes to the session.
172 * @param attributes reference to an array of Attributes to cache (will be freed by cache)
174 virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
177 * Returns an assertion cached by the session.
179 * @param id identifier of the assertion to retrieve
180 * @return pointer to assertion, or NULL
182 virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
185 * Stores an assertion in the session.
187 * @param assertion pointer to an assertion to cache (will be freed by cache)
189 virtual void addAssertion(opensaml::Assertion* assertion)=0;
194 * Creates and manages user sessions
196 * The cache abstracts a persistent (meaning across requests) cache of
197 * instances of the Session interface. Creation of new entries and entry
198 * lookup are confined to this interface to enable the implementation to
199 * remote and/or optimize calls by implementing custom versions of the
200 * Session interface as required.
202 class SHIBSP_API SessionCache
204 MAKE_NONCOPYABLE(SessionCache);
208 virtual ~SessionCache() {}
212 * Inserts a new session into the cache and binds the session to the outgoing
215 * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
217 * @param application reference to Application that owns the Session
218 * @param httpRequest request that initiated session
219 * @param httpResponse current response to client
220 * @param expires expiration time of session
221 * @param issuer issuing metadata of assertion issuer, if known
222 * @param protocol protocol family used to initiate the session
223 * @param nameid principal identifier, normalized to SAML 2, if any
224 * @param authn_instant UTC timestamp of authentication at IdP, if known
225 * @param session_index index of session between principal and IdP, if any
226 * @param authncontext_class method/category of authentication event, if known
227 * @param authncontext_decl specifics of authentication event, if known
228 * @param tokens assertions to cache with session, if any
229 * @param attributes optional array of resolved Attributes to cache with session
232 const Application& application,
233 const xmltooling::HTTPRequest& httpRequest,
234 xmltooling::HTTPResponse& httpResponse,
236 const opensaml::saml2md::EntityDescriptor* issuer=NULL,
237 const XMLCh* protocol=NULL,
238 const opensaml::saml2::NameID* nameid=NULL,
239 const XMLCh* authn_instant=NULL,
240 const XMLCh* session_index=NULL,
241 const XMLCh* authncontext_class=NULL,
242 const XMLCh* authncontext_decl=NULL,
243 const std::vector<const opensaml::Assertion*>* tokens=NULL,
244 const std::vector<Attribute*>* attributes=NULL
248 * Determines whether the Session bound to a client request matches a set of input criteria.
250 * @param application reference to Application that owns the Session
251 * @param request request in which to locate Session
252 * @param issuer required source of session(s)
253 * @param nameid required name identifier
254 * @param indexes session indexes
255 * @return true iff the Session exists and matches the input criteria
257 virtual bool matches(
258 const Application& application,
259 const xmltooling::HTTPRequest& request,
260 const opensaml::saml2md::EntityDescriptor* issuer,
261 const opensaml::saml2::NameID& nameid,
262 const std::set<std::string>* indexes
266 * Executes a test of the cache's general health.
268 virtual void test()=0;
272 * Returns the ID of the session bound to the specified client request, if possible.
274 * @param application reference to Application that owns the Session
275 * @param request request from client containing session, or a reference to it
276 * @return ID of session, if any known, or an empty string
278 virtual std::string active(const Application& application, const xmltooling::HTTPRequest& request)=0;
281 * Locates an existing session bound to a request.
283 * <p>If the client address is supplied, then a check will be performed against
284 * the address recorded in the record.
286 * @param application reference to Application that owns the Session
287 * @param request request from client containing session, or a reference to it
288 * @param client_addr network address of client (if known)
289 * @param timeout inactivity timeout to enforce (0 for none, NULL to bypass check/update of last access)
290 * @return pointer to locked Session, or NULL
292 virtual Session* find(
293 const Application& application, const xmltooling::HTTPRequest& request, const char* client_addr=NULL, time_t* timeout=NULL
297 * Deletes an existing session bound to a request.
299 * @param application reference to Application that owns the Session
300 * @param request request from client containing session, or a reference to it
301 * @param response optional response to client enabling removal of session or reference
303 virtual void remove(const Application& application, const xmltooling::HTTPRequest& request, xmltooling::HTTPResponse* response=NULL)=0;
306 /** SessionCache implementation backed by a StorageService. */
307 #define STORAGESERVICE_SESSION_CACHE "StorageService"
310 * Registers SessionCache classes into the runtime.
312 void SHIBSP_API registerSessionCaches();
315 #endif /* __shibsp_sessioncache_h__ */