2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file shibsp/SessionCache.h
24 * Caches and manages user sessions.
27 #ifndef __shibsp_sessioncache_h__
28 #define __shibsp_sessioncache_h__
30 #include <shibsp/base.h>
37 #include <xmltooling/Lockable.h>
39 namespace xmltooling {
40 class XMLTOOL_API HTTPRequest;
41 class XMLTOOL_API HTTPResponse;
47 class SAML_API Assertion;
49 class SAML_API NameID;
56 class SHIBSP_API Application;
57 class SHIBSP_API Attribute;
60 * Encapsulates access to a user's security session.
62 * <p>The SessionCache does not itself require locking to manage
63 * concurrency, but access to each Session is generally exclusive
64 * or at least controlled, and the caller must unlock a Session
67 class SHIBSP_API Session : public virtual xmltooling::Lockable
69 MAKE_NONCOPYABLE(Session);
75 * Returns the session key.
77 * @return unique ID of session
79 virtual const char* getID() const=0;
82 * Returns the session's application ID.
84 * @return unique ID of application bound to session
86 virtual const char* getApplicationID() const=0;
89 * Returns the session expiration.
91 * @return the session's expiration time or 0 for none
93 virtual time_t getExpiration() const=0;
96 * Returns the last access time of the session.
98 * @return the session's last access time
100 virtual time_t getLastAccess() const=0;
103 * Returns the address of the client associated with the session.
105 * @return the client's network address
107 virtual const char* getClientAddress() const=0;
110 * Returns the entityID of the IdP that initiated the session.
112 * @return the IdP's entityID
114 virtual const char* getEntityID() const=0;
117 * Returns the protocol family used to initiate the session.
119 * @return the protocol constant that represents the general SSO protocol used
121 virtual const char* getProtocol() const=0;
124 * Returns the UTC timestamp on the authentication event at the IdP.
126 * @return the UTC authentication timestamp
128 virtual const char* getAuthnInstant() const=0;
132 * Returns the NameID associated with a session.
134 * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
136 * @return a SAML 2.0 NameID associated with the session, if any
138 virtual const opensaml::saml2::NameID* getNameID() const=0;
142 * Returns the SessionIndex provided with the session.
144 * @return the SessionIndex from the original SSO assertion, if any
146 virtual const char* getSessionIndex() const=0;
149 * Returns a URI containing an AuthnContextClassRef provided with the session.
151 * <p>SAML 1.x AuthenticationMethods will be returned as class references.
153 * @return a URI identifying the authentication context class
155 virtual const char* getAuthnContextClassRef() const=0;
158 * Returns a URI containing an AuthnContextDeclRef provided with the session.
160 * @return a URI identifying the authentication context declaration
162 virtual const char* getAuthnContextDeclRef() const=0;
165 * Returns the resolved attributes associated with the session.
167 * @return an immutable array of attributes
169 virtual const std::vector<Attribute*>& getAttributes() const=0;
172 * Returns the resolved attributes associated with the session, indexed by ID
174 * @return an immutable map of attributes keyed by attribute ID
176 virtual const std::multimap<std::string,const Attribute*>& getIndexedAttributes() const=0;
179 * Returns the identifiers of the assertion(s) cached by the session.
181 * <p>The SSO assertion is guaranteed to be first in the set.
183 * @return an immutable array of AssertionID values
185 virtual const std::vector<const char*>& getAssertionIDs() const=0;
189 * Adds additional attributes to the session.
191 * @param attributes reference to an array of Attributes to cache (will be freed by cache)
193 virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
196 * Returns an assertion cached by the session.
198 * @param id identifier of the assertion to retrieve
199 * @return pointer to assertion, or nullptr
201 virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
204 * Stores an assertion in the session.
206 * @param assertion pointer to an assertion to cache (will be freed by cache)
208 virtual void addAssertion(opensaml::Assertion* assertion)=0;
213 * Creates and manages user sessions
215 * The cache abstracts a persistent (meaning across requests) cache of
216 * instances of the Session interface. Creation of new entries and entry
217 * lookup are confined to this interface to enable the implementation to
218 * remote and/or optimize calls by implementing custom versions of the
219 * Session interface as required.
221 class SHIBSP_API SessionCache
223 MAKE_NONCOPYABLE(SessionCache);
227 virtual ~SessionCache();
231 * Inserts a new session into the cache and binds the session to the outgoing
234 * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
236 * @param application reference to Application that owns the Session
237 * @param httpRequest request that initiated session
238 * @param httpResponse current response to client
239 * @param expires expiration time of session
240 * @param issuer issuing metadata of assertion issuer, if known
241 * @param protocol protocol family used to initiate the session
242 * @param nameid principal identifier, normalized to SAML 2, if any
243 * @param authn_instant UTC timestamp of authentication at IdP, if known
244 * @param session_index index of session between principal and IdP, if any
245 * @param authncontext_class method/category of authentication event, if known
246 * @param authncontext_decl specifics of authentication event, if known
247 * @param tokens assertions to cache with session, if any
248 * @param attributes optional array of resolved Attributes to cache with session
251 const Application& application,
252 const xmltooling::HTTPRequest& httpRequest,
253 xmltooling::HTTPResponse& httpResponse,
255 const opensaml::saml2md::EntityDescriptor* issuer=nullptr,
256 const XMLCh* protocol=nullptr,
257 const opensaml::saml2::NameID* nameid=nullptr,
258 const XMLCh* authn_instant=nullptr,
259 const XMLCh* session_index=nullptr,
260 const XMLCh* authncontext_class=nullptr,
261 const XMLCh* authncontext_decl=nullptr,
262 const std::vector<const opensaml::Assertion*>* tokens=nullptr,
263 const std::vector<Attribute*>* attributes=nullptr
267 * Determines whether the Session bound to a client request matches a set of input criteria.
269 * @param application reference to Application that owns the Session
270 * @param request request in which to locate Session
271 * @param issuer required source of session(s)
272 * @param nameid required name identifier
273 * @param indexes session indexes
274 * @return true iff the Session exists and matches the input criteria
276 virtual bool matches(
277 const Application& application,
278 const xmltooling::HTTPRequest& request,
279 const opensaml::saml2md::EntityDescriptor* issuer,
280 const opensaml::saml2::NameID& nameid,
281 const std::set<std::string>* indexes
285 * Executes a test of the cache's general health.
287 virtual void test()=0;
291 * Returns the ID of the session bound to the specified client request, if possible.
293 * @param application reference to Application that owns the Session
294 * @param request request from client containing session, or a reference to it
295 * @return ID of session, if any known, or an empty string
297 virtual std::string active(const Application& application, const xmltooling::HTTPRequest& request)=0;
300 * Locates an existing session bound to a request.
302 * <p>If the client address is supplied, then a check will be performed against
303 * the address recorded in the record.
305 * @param application reference to Application that owns the Session
306 * @param request request from client bound to session
307 * @param client_addr network address of client (if known)
308 * @param timeout inactivity timeout to enforce (0 for none, nullptr to bypass check/update of last access)
309 * @return pointer to locked Session, or nullptr
311 virtual Session* find(
312 const Application& application,
313 const xmltooling::HTTPRequest& request,
314 const char* client_addr=nullptr,
315 time_t* timeout=nullptr
319 * Locates an existing session bound to a request.
321 * <p>If the client address is supplied, then a check will be performed against
322 * the address recorded in the record.
324 * <p>If a bound session is found to have expired, be invalid, etc., and if the request
325 * can be used to "clear" the session from subsequent client requests, then it may be cleared.
327 * @param application reference to Application that owns the Session
328 * @param request request from client bound to session
329 * @param client_addr network address of client (if known)
330 * @param timeout inactivity timeout to enforce (0 for none, nullptr to bypass check/update of last access)
331 * @return pointer to locked Session, or nullptr
333 virtual Session* find(
334 const Application& application,
335 xmltooling::HTTPRequest& request,
336 const char* client_addr=nullptr,
337 time_t* timeout=nullptr
341 * Deletes an existing session bound to a request.
343 * @param application reference to Application that owns the Session
344 * @param request request from client containing session, or a reference to it
345 * @param response optional response to client enabling removal of session or reference
347 virtual void remove(const Application& application, const xmltooling::HTTPRequest& request, xmltooling::HTTPResponse* response=nullptr)=0;
350 /** SessionCache implementation backed by a StorageService. */
351 #define STORAGESERVICE_SESSION_CACHE "StorageService"
354 * Registers SessionCache classes into the runtime.
356 void SHIBSP_API registerSessionCaches();
359 #endif /* __shibsp_sessioncache_h__ */