Fix backslashes in SHIBSP_PREFIX variable by manually creating it during the script...
[shibboleth/sp.git] / shibsp / SessionCache.h
index d90c878..18910bd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #define __shibsp_sessioncache_h__
 
 #include <shibsp/base.h>
-#include <saml/saml1/core/Assertions.h>
-#include <saml/saml2/metadata/Metadata.h>
+#ifndef SHIBSP_LITE
+# include <saml/saml1/core/Assertions.h>
+# include <saml/saml2/metadata/Metadata.h>
+#endif
 #include <xmltooling/Lockable.h>
+#include <xmltooling/io/HTTPRequest.h>
+#include <xmltooling/io/HTTPResponse.h>
 
 namespace shibsp {
 
     class SHIBSP_API Application;
     class SHIBSP_API Attribute;
 
+    /**
+     * Encapsulates access to a user's security session.
+     *
+     * <p>The SessionCache does not itself require locking to manage
+     * concurrency, but access to each Session is generally exclusive
+     * or at least controlled, and the caller must unlock a Session
+     * to dispose of it.
+     */
     class SHIBSP_API Session : public virtual xmltooling::Lockable
     {
         MAKE_NONCOPYABLE(Session);
@@ -41,6 +53,34 @@ namespace shibsp {
         virtual ~Session() {}
     public:
         /**
+         * Returns the session key.
+         *
+         * @return unique ID of session
+         */
+        virtual const char* getID() const=0;
+
+        /**
+         * Returns the session's application ID.
+         *
+         * @return unique ID of application bound to session
+         */
+        virtual const char* getApplicationID() const=0;
+
+        /**
+         * Returns the session expiration.
+         *
+         * @return  the session's expiration time or 0 for none
+         */
+        virtual time_t getExpiration() const=0;
+
+        /**
+         * Returns the last access time of the session.
+         *
+         * @return  the session's last access time
+         */
+        virtual time_t getLastAccess() const=0;
+
+        /**
          * Returns the address of the client associated with the session.
          * 
          * @return  the client's network address
@@ -53,7 +93,14 @@ namespace shibsp {
          * @return the IdP's entityID
          */
         virtual const char* getEntityID() const=0;
-        
+
+        /**
+         * Returns the protocol family used to initiate the session.
+         *
+         * @return the protocol constant that represents the general SSO protocol used
+         */
+        virtual const char* getProtocol() const=0;
+
         /**
          * Returns the UTC timestamp on the authentication event at the IdP.
          * 
@@ -61,14 +108,16 @@ namespace shibsp {
          */
         virtual const char* getAuthnInstant() const=0;
 
+#ifndef SHIBSP_LITE
         /**
          * Returns the NameID associated with a session.
          * 
          * <p>SAML 1.x identifiers will be promoted to the 2.0 type.
          * 
-         * @return reference to a SAML 2.0 NameID
+         * @return a SAML 2.0 NameID associated with the session, if any
          */
-        virtual const opensaml::saml2::NameID& getNameID() const=0;
+        virtual const opensaml::saml2::NameID* getNameID() const=0;
+#endif
 
         /**
          * Returns the SessionIndex provided with the session.
@@ -94,18 +143,18 @@ namespace shibsp {
         virtual const char* getAuthnContextDeclRef() const=0;
         
         /**
-         * Returns the set of resolved attributes associated with the session.
+         * Returns the resolved attributes associated with the session.
          * 
          * @return an immutable array of attributes
          */
-        virtual const std::vector<const Attribute*>& getAttributes() const=0;
-        
+        virtual const std::vector<Attribute*>& getAttributes() const=0;
+
         /**
-         * Adds additional attributes to the session.
+         * Returns the resolved attributes associated with the session, indexed by ID
          * 
-         * @param attributes    reference to an array of Attributes to cache (will be freed by cache)
+         * @return an immutable map of attributes keyed by attribute ID
          */
-        virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
+        virtual const std::multimap<std::string,const Attribute*>& getIndexedAttributes() const=0;
         
         /**
          * Returns the identifiers of the assertion(s) cached by the session.
@@ -116,20 +165,29 @@ namespace shibsp {
          */
         virtual const std::vector<const char*>& getAssertionIDs() const=0;
         
+#ifndef SHIBSP_LITE
+        /**
+         * Adds additional attributes to the session.
+         * 
+         * @param attributes    reference to an array of Attributes to cache (will be freed by cache)
+         */
+        virtual void addAttributes(const std::vector<Attribute*>& attributes)=0;
+
         /**
          * Returns an assertion cached by the session.
          * 
          * @param id    identifier of the assertion to retrieve
          * @return pointer to assertion, or NULL
          */
-        virtual const opensaml::RootObject* getAssertion(const char* id) const=0;
+        virtual const opensaml::Assertion* getAssertion(const char* id) const=0;
         
         /**
          * Stores an assertion in the session.
          * 
          * @param assertion pointer to an assertion to cache (will be freed by cache)
          */
-        virtual void addAssertion(opensaml::RootObject* assertion)=0;        
+        virtual void addAssertion(opensaml::Assertion* assertion)=0;        
+#endif
     };
     
     /**
@@ -145,88 +203,108 @@ namespace shibsp {
     {
         MAKE_NONCOPYABLE(SessionCache);
     protected:
-    
-        /**
-         * Constructor
-         * 
-         * <p>The following XML content is supported to configure the cache:
-         * <dl>
-         *  <dt>cacheTimeout</dt>
-         *  <dd>attribute containing maximum lifetime in seconds for unused sessions to remain in cache</dd>
-         * </dl>
-         * 
-         * @param e root of DOM tree to configure the cache
-         */
-        SessionCache(const DOMElement* e);
-        
-        /** maximum lifetime in seconds for unused sessions to be cached */
-        unsigned long m_cacheTimeout;
-        
+        SessionCache() {}
     public:
         virtual ~SessionCache() {}
         
+#ifndef SHIBSP_LITE
         /**
-         * Inserts a new session into the cache.
+         * Inserts a new session into the cache and binds the session to the outgoing
+         * client response.
          * 
-         * <p>The SSO token remains owned by the caller and is copied by the
-         * cache. Any Attributes supplied become the property of the cache.  
+         * <p>The SSO tokens and Attributes remain owned by the caller and are copied by the cache.
          * 
-         * @param expires           expiration time of session
          * @param application       reference to Application that owns the Session
-         * @param client_addr       network address of client
+         * @param httpRequest       request that initiated session
+         * @param httpResponse      current response to client
+         * @param expires           expiration time of session
          * @param issuer            issuing metadata of assertion issuer, if known
-         * @param authn_instant     UTC timestamp of authentication at IdP
-         * @param nameid            principal identifier, normalized to SAML 2
-         * @param session_index     index of session between principal and IdP
-         * @param authncontext_class    method/category of authentication event
-         * @param authncontext_decl specifics of authentication event 
-         * @param ssoToken          SSO assertion initiating the session, if any
-         * @param attributes        optional set of resolved Attributes to cache with session
-         * @return  newly created session's key
-         */
-        virtual std::string insert(
+         * @param protocol          protocol family used to initiate the session
+         * @param nameid            principal identifier, normalized to SAML 2, if any
+         * @param authn_instant     UTC timestamp of authentication at IdP, if known
+         * @param session_index     index of session between principal and IdP, if any
+         * @param authncontext_class    method/category of authentication event, if known
+         * @param authncontext_decl specifics of authentication event, if known
+         * @param tokens            assertions to cache with session, if any
+         * @param attributes        optional array of resolved Attributes to cache with session
+         */
+        virtual void insert(
+            const Application& application,
+            const xmltooling::HTTPRequest& httpRequest,
+            xmltooling::HTTPResponse& httpResponse,
             time_t expires,
+            const opensaml::saml2md::EntityDescriptor* issuer=NULL,
+            const XMLCh* protocol=NULL,
+            const opensaml::saml2::NameID* nameid=NULL,
+            const XMLCh* authn_instant=NULL,
+            const XMLCh* session_index=NULL,
+            const XMLCh* authncontext_class=NULL,
+            const XMLCh* authncontext_decl=NULL,
+            const std::vector<const opensaml::Assertion*>* tokens=NULL,
+            const std::vector<Attribute*>* attributes=NULL
+            )=0;
+
+        /**
+         * Determines whether the Session bound to a client request matches a set of input criteria.
+         * 
+         * @param application   reference to Application that owns the Session
+         * @param request       request in which to locate Session
+         * @param issuer        required source of session(s)
+         * @param nameid        required name identifier
+         * @param indexes       session indexes
+         * @return  true iff the Session exists and matches the input criteria
+         */
+        virtual bool matches(
             const Application& application,
-            const char* client_addr,
+            const xmltooling::HTTPRequest& request,
             const opensaml::saml2md::EntityDescriptor* issuer,
             const opensaml::saml2::NameID& nameid,
-            const char* authn_instant=NULL,
-            const char* session_index=NULL,
-            const char* authncontext_class=NULL,
-            const char* authncontext_decl=NULL,
-            const opensaml::RootObject* ssoToken=NULL,
-            const std::vector<Attribute*>* attributes=NULL
+            const std::set<std::string>* indexes
             )=0;
 
         /**
-         * Locates an existing session.
+         * Executes a test of the cache's general health.
+         */
+        virtual void test()=0;
+#endif
+
+        /**
+         * Returns the ID of the session bound to the specified client request, if possible.
+         * 
+         * @param application   reference to Application that owns the Session
+         * @param request       request from client containing session, or a reference to it
+         * @return  ID of session, if any known, or an empty string
+         */
+        virtual std::string active(const Application& application, const xmltooling::HTTPRequest& request)=0;
+
+        /**
+         * Locates an existing session bound to a request.
          * 
          * <p>If the client address is supplied, then a check will be performed against
          * the address recorded in the record.
          * 
-         * @param key           session key
          * @param application   reference to Application that owns the Session
+         * @param request       request from client containing session, or a reference to it
          * @param client_addr   network address of client (if known)
-         * @param timeout       inactivity timeout to enforce (0 for none)
+         * @param timeout       inactivity timeout to enforce (0 for none, NULL to bypass check/update of last access)
          * @return  pointer to locked Session, or NULL
          */
-        virtual Session* find(const char* key, const Application& application, const char* client_addr=NULL, time_t timeout=0)=0;
-            
+        virtual Session* find(
+            const Application& application, const xmltooling::HTTPRequest& request, const char* client_addr=NULL, time_t* timeout=NULL
+            )=0;
+
         /**
-         * Deletes an existing session.
+         * Deletes an existing session bound to a request.
          * 
-         * @param key           session key
          * @param application   reference to Application that owns the Session
-         * @param client_addr   network address of client (if known)
+         * @param request       request from client containing session, or a reference to it
+         * @param response      optional response to client enabling removal of session or reference
          */
-        virtual void remove(const char* key, const Application& application, const char* client_addr)=0;
+        virtual void remove(const Application& application, const xmltooling::HTTPRequest& request, xmltooling::HTTPResponse* response=NULL)=0;
     };
 
-    /** SessionCache implementation that delegates to a remoted version. */
-    #define REMOTED_SESSION_CACHE    "edu.internet2.middleware.shibboleth.sp.provider.RemotedSessionCache"
-
     /** SessionCache implementation backed by a StorageService. */
-    #define STORAGESERVICE_SESSION_CACHE    "edu.internet2.middleware.shibboleth.sp.provider.StorageServiceSessionCache"
+    #define STORAGESERVICE_SESSION_CACHE    "StorageService"
 
     /**
      * Registers SessionCache classes into the runtime.