2 * Copyright 2001-2010 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>
33 #include <xmltooling/Lockable.h>
35 namespace xmltooling {
36 class XMLTOOL_API HTTPRequest;
37 class XMLTOOL_API HTTPResponse;
43 class SAML_API Assertion;
45 class SAML_API NameID;
52 class SHIBSP_API Application;
53 class SHIBSP_API Attribute;
56 * Encapsulates access to a user's security session.
58 * <p>The SessionCache does not itself require locking to manage
59 * concurrency, but access to each Session is generally exclusive
60 * or at least controlled, and the caller must unlock a Session
63 class SHIBSP_API Session : public virtual xmltooling::Lockable
65 MAKE_NONCOPYABLE(Session);
71 * Returns the session key.
73 * @return unique ID of session
75 virtual const char* getID() const=0;
78 * Returns the session's application ID.
80 * @return unique ID of application bound to session
82 virtual const char* getApplicationID() const=0;
85 * Returns the session expiration.
87 * @return the session's expiration time or 0 for none
89 virtual time_t getExpiration() const=0;
92 * Returns the last access time of the session.
94 * @return the session's last access time
96 virtual time_t getLastAccess() const=0;
99 * Returns the address of the client associated with the session.
101 * @return the client's network address
103 virtual const char* getClientAddress() const=0;
106 * Returns the entityID of the IdP that initiated the session.
108 * @return the IdP's entityID
110 virtual const char* getEntityID() const=0;
113 * Returns the protocol family used to initiate the session.
115 * @return the protocol constant that represents the general SSO protocol used
117 virtual const char* getProtocol() const=0;
120 * Returns the UTC timestamp on the authentication event at the IdP.
122 * @return the UTC authentication timestamp
124 virtual const char* getAuthnInstant() const=0;
128 * Returns the NameID associated with a session.
130 * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
132 * @return a SAML 2.0 NameID associated with the session, if any
134 virtual const opensaml::saml2::NameID* getNameID() const=0;
138 * Returns the SessionIndex provided with the session.
140 * @return the SessionIndex from the original SSO assertion, if any
142 virtual const char* getSessionIndex() const=0;
145 * Returns a URI containing an AuthnContextClassRef provided with the session.
147 * <p>SAML 1.x AuthenticationMethods will be returned as class references.
149 * @return a URI identifying the authentication context class
151 virtual const char* getAuthnContextClassRef() const=0;
154 * Returns a URI containing an AuthnContextDeclRef provided with the session.
156 * @return a URI identifying the authentication context declaration
158 virtual const char* getAuthnContextDeclRef() const=0;
161 * Returns the resolved attributes associated with the session.
163 * @return an immutable array of attributes
165 virtual const std::vector<Attribute*>& getAttributes() const=0;
168 * Returns the resolved attributes associated with the session, indexed by ID
170 * @return an immutable map of attributes keyed by attribute ID
172 virtual const std::multimap<std::string,const Attribute*>& getIndexedAttributes() const=0;
175 * Returns the identifiers of the assertion(s) cached by the session.
177 * <p>The SSO assertion is guaranteed to be first in the set.
179 * @return an immutable array of AssertionID values
181 virtual const std::vector<const char*>& getAssertionIDs() const=0;
185 * Adds additional attributes to the session.
187 * @param attributes reference to an array of Attributes to cache (will be freed by cache)
189 virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
192 * Returns an assertion cached by the session.
194 * @param id identifier of the assertion to retrieve
195 * @return pointer to assertion, or nullptr
197 virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
200 * Stores an assertion in the session.
202 * @param assertion pointer to an assertion to cache (will be freed by cache)
204 virtual void addAssertion(opensaml::Assertion* assertion)=0;
209 * Creates and manages user sessions
211 * The cache abstracts a persistent (meaning across requests) cache of
212 * instances of the Session interface. Creation of new entries and entry
213 * lookup are confined to this interface to enable the implementation to
214 * remote and/or optimize calls by implementing custom versions of the
215 * Session interface as required.
217 class SHIBSP_API SessionCache
219 MAKE_NONCOPYABLE(SessionCache);
223 virtual ~SessionCache();
227 * Inserts a new session into the cache and binds the session to the outgoing
230 * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
232 * @param application reference to Application that owns the Session
233 * @param httpRequest request that initiated session
234 * @param httpResponse current response to client
235 * @param expires expiration time of session
236 * @param issuer issuing metadata of assertion issuer, if known
237 * @param protocol protocol family used to initiate the session
238 * @param nameid principal identifier, normalized to SAML 2, if any
239 * @param authn_instant UTC timestamp of authentication at IdP, if known
240 * @param session_index index of session between principal and IdP, if any
241 * @param authncontext_class method/category of authentication event, if known
242 * @param authncontext_decl specifics of authentication event, if known
243 * @param tokens assertions to cache with session, if any
244 * @param attributes optional array of resolved Attributes to cache with session
247 const Application& application,
248 const xmltooling::HTTPRequest& httpRequest,
249 xmltooling::HTTPResponse& httpResponse,
251 const opensaml::saml2md::EntityDescriptor* issuer=nullptr,
252 const XMLCh* protocol=nullptr,
253 const opensaml::saml2::NameID* nameid=nullptr,
254 const XMLCh* authn_instant=nullptr,
255 const XMLCh* session_index=nullptr,
256 const XMLCh* authncontext_class=nullptr,
257 const XMLCh* authncontext_decl=nullptr,
258 const std::vector<const opensaml::Assertion*>* tokens=nullptr,
259 const std::vector<Attribute*>* attributes=nullptr
263 * Determines whether the Session bound to a client request matches a set of input criteria.
265 * @param application reference to Application that owns the Session
266 * @param request request in which to locate Session
267 * @param issuer required source of session(s)
268 * @param nameid required name identifier
269 * @param indexes session indexes
270 * @return true iff the Session exists and matches the input criteria
272 virtual bool matches(
273 const Application& application,
274 const xmltooling::HTTPRequest& request,
275 const opensaml::saml2md::EntityDescriptor* issuer,
276 const opensaml::saml2::NameID& nameid,
277 const std::set<std::string>* indexes
281 * Executes a test of the cache's general health.
283 virtual void test()=0;
287 * Returns the ID of the session bound to the specified client request, if possible.
289 * @param application reference to Application that owns the Session
290 * @param request request from client containing session, or a reference to it
291 * @return ID of session, if any known, or an empty string
293 virtual std::string active(const Application& application, const xmltooling::HTTPRequest& request)=0;
296 * Locates an existing session bound to a request.
298 * <p>If the client address is supplied, then a check will be performed against
299 * the address recorded in the record.
301 * @param application reference to Application that owns the Session
302 * @param request request from client bound to session
303 * @param client_addr network address of client (if known)
304 * @param timeout inactivity timeout to enforce (0 for none, nullptr to bypass check/update of last access)
305 * @return pointer to locked Session, or nullptr
307 virtual Session* find(
308 const Application& application,
309 const xmltooling::HTTPRequest& request,
310 const char* client_addr=nullptr,
311 time_t* timeout=nullptr
315 * Locates an existing session bound to a request.
317 * <p>If the client address is supplied, then a check will be performed against
318 * the address recorded in the record.
320 * <p>If a bound session is found to have expired, be invalid, etc., and if the request
321 * can be used to "clear" the session from subsequent client requests, then it may be cleared.
323 * @param application reference to Application that owns the Session
324 * @param request request from client bound to session
325 * @param client_addr network address of client (if known)
326 * @param timeout inactivity timeout to enforce (0 for none, nullptr to bypass check/update of last access)
327 * @return pointer to locked Session, or nullptr
329 virtual Session* find(
330 const Application& application,
331 xmltooling::HTTPRequest& request,
332 const char* client_addr=nullptr,
333 time_t* timeout=nullptr
337 * Deletes an existing session bound to a request.
339 * @param application reference to Application that owns the Session
340 * @param request request from client containing session, or a reference to it
341 * @param response optional response to client enabling removal of session or reference
343 virtual void remove(const Application& application, const xmltooling::HTTPRequest& request, xmltooling::HTTPResponse* response=nullptr)=0;
346 /** SessionCache implementation backed by a StorageService. */
347 #define STORAGESERVICE_SESSION_CACHE "StorageService"
350 * Registers SessionCache classes into the runtime.
352 void SHIBSP_API registerSessionCaches();
355 #endif /* __shibsp_sessioncache_h__ */