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>
29 # include <saml/saml1/core/Assertions.h>
30 # include <saml/saml2/metadata/Metadata.h>
32 #include <xmltooling/Lockable.h>
36 class SHIBSP_API Application;
37 class SHIBSP_API Attribute;
39 class SHIBSP_API Session : public virtual xmltooling::Lockable
41 MAKE_NONCOPYABLE(Session);
47 * Returns the session key.
49 * @return unique ID of session
51 virtual const char* getID() const=0;
54 * Returns the address of the client associated with the session.
56 * @return the client's network address
58 virtual const char* getClientAddress() const=0;
61 * Returns the entityID of the IdP that initiated the session.
63 * @return the IdP's entityID
65 virtual const char* getEntityID() const=0;
68 * Returns the UTC timestamp on the authentication event at the IdP.
70 * @return the UTC authentication timestamp
72 virtual const char* getAuthnInstant() const=0;
76 * Returns the NameID associated with a session.
78 * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
80 * @return a SAML 2.0 NameID associated with the session, if any
82 virtual const opensaml::saml2::NameID* getNameID() const=0;
86 * Returns the SessionIndex provided with the session.
88 * @return the SessionIndex from the original SSO assertion, if any
90 virtual const char* getSessionIndex() const=0;
93 * Returns a URI containing an AuthnContextClassRef provided with the session.
95 * <p>SAML 1.x AuthenticationMethods will be returned as class references.
97 * @return a URI identifying the authentication context class
99 virtual const char* getAuthnContextClassRef() const=0;
102 * Returns a URI containing an AuthnContextDeclRef provided with the session.
104 * @return a URI identifying the authentication context declaration
106 virtual const char* getAuthnContextDeclRef() const=0;
109 * Returns the resolved attributes associated with the session.
111 * @return an immutable map of attributes keyed by attribute ID
113 virtual const std::multimap<std::string,Attribute*>& getAttributes() const=0;
116 * Returns the identifiers of the assertion(s) cached by the session.
118 * <p>The SSO assertion is guaranteed to be first in the set.
120 * @return an immutable array of AssertionID values
122 virtual const std::vector<const char*>& getAssertionIDs() const=0;
126 * Adds additional attributes to the session.
128 * @param attributes reference to an array of Attributes to cache (will be freed by cache)
130 virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
133 * Returns an assertion cached by the session.
135 * @param id identifier of the assertion to retrieve
136 * @return pointer to assertion, or NULL
138 virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
141 * Stores an assertion in the session.
143 * @param assertion pointer to an assertion to cache (will be freed by cache)
145 virtual void addAssertion(opensaml::Assertion* assertion)=0;
150 * Creates and manages user sessions
152 * The cache abstracts a persistent (meaning across requests) cache of
153 * instances of the Session interface. Creation of new entries and entry
154 * lookup are confined to this interface to enable the implementation to
155 * remote and/or optimize calls by implementing custom versions of the
156 * Session interface as required.
158 class SHIBSP_API SessionCache
160 MAKE_NONCOPYABLE(SessionCache);
166 * <p>The following XML content is supported to configure the cache:
168 * <dt>cacheTimeout</dt>
169 * <dd>attribute containing maximum lifetime in seconds for unused sessions to remain in cache</dd>
172 * @param e root of DOM tree to configure the cache
174 SessionCache(const xercesc::DOMElement* e);
176 /** maximum lifetime in seconds for unused sessions to be cached */
177 unsigned long m_cacheTimeout;
180 virtual ~SessionCache() {}
184 * Inserts a new session into the cache.
186 * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
188 * @param expires expiration time of session
189 * @param application reference to Application that owns the Session
190 * @param client_addr network address of client
191 * @param issuer issuing metadata of assertion issuer, if known
192 * @param nameid principal identifier, normalized to SAML 2, if any
193 * @param authn_instant UTC timestamp of authentication at IdP, if known
194 * @param session_index index of session between principal and IdP, if any
195 * @param authncontext_class method/category of authentication event, if known
196 * @param authncontext_decl specifics of authentication event, if known
197 * @param tokens assertions to cache with session, if any
198 * @param attributes optional map of resolved Attributes to cache with session
199 * @return newly created session's key
201 virtual std::string insert(
203 const Application& application,
204 const char* client_addr=NULL,
205 const opensaml::saml2md::EntityDescriptor* issuer=NULL,
206 const opensaml::saml2::NameID* nameid=NULL,
207 const char* authn_instant=NULL,
208 const char* session_index=NULL,
209 const char* authncontext_class=NULL,
210 const char* authncontext_decl=NULL,
211 const std::vector<const opensaml::Assertion*>* tokens=NULL,
212 const std::multimap<std::string,Attribute*>* attributes=NULL
216 * Locates an existing session or sessions by subject identifier.
218 * @param issuer source of session(s)
219 * @param nameid name identifier associated with the session(s) to locate
220 * @param index index of session, or NULL for all sessions associated with other parameters
221 * @param application reference to Application that owns the session(s)
222 * @param sessions on exit, contains the IDs of the matching sessions
225 const opensaml::saml2md::EntityDescriptor& issuer,
226 const opensaml::saml2::NameID& nameid,
228 const Application& application,
229 std::vector<std::string>& sessions
233 * Deletes an existing session or sessions.
235 * @param issuer source of session(s)
236 * @param nameid name identifier associated with the session(s) to terminate
237 * @param index index of session, or NULL for all sessions associated with other parameters
238 * @param application reference to Application that owns the session(s)
239 * @param sessions on exit, contains the IDs of the matching sessions removed
242 const opensaml::saml2md::EntityDescriptor& issuer,
243 const opensaml::saml2::NameID& nameid,
245 const Application& application,
246 std::vector<std::string>& sessions
251 * Locates an existing session.
253 * <p>If the client address is supplied, then a check will be performed against
254 * the address recorded in the record.
256 * @param key session key
257 * @param application reference to Application that owns the Session
258 * @param client_addr network address of client (if known)
259 * @param timeout inactivity timeout to enforce (0 for none, NULL to bypass check/update of last access)
260 * @return pointer to locked Session, or NULL
262 virtual Session* find(
263 const char* key, const Application& application, const char* client_addr=NULL, time_t* timeout=NULL
267 * Deletes an existing session.
269 * @param key session key
270 * @param application reference to Application that owns the Session
272 virtual void remove(const char* key, const Application& application)=0;
276 /** SessionCache implementation backed by a StorageService. */
277 #define STORAGESERVICE_SESSION_CACHE "StorageService"
280 /** SessionCache implementation for lite builds that delegates to a remoted version. */
281 #define REMOTED_SESSION_CACHE "Remoted"
284 * Registers SessionCache classes into the runtime.
286 void SHIBSP_API registerSessionCaches();
289 #endif /* __shibsp_sessioncache_h__ */