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 * StorageServiceSessionCache.cpp
20 * StorageService-based SessionCache implementation.
22 * Instead of optimizing this plugin with a buffering scheme that keeps objects around
23 * and avoids extra parsing steps, I'm assuming that systems that require such can
24 * layer their own cache plugin on top of this version either by delegating to it
25 * or using the remoting support. So this version will load sessions directly
26 * from the StorageService, instantiate enough to expose the Session API,
27 * and then delete everything when they're unlocked. All data in memory is always
28 * kept in sync with the StorageService (no lazy updates).
32 #include "Application.h"
33 #include "exceptions.h"
34 #include "ServiceProvider.h"
35 #include "SessionCacheEx.h"
36 #include "TransactionLog.h"
37 #include "attribute/Attribute.h"
38 #include "handler/RemotedHandler.h"
39 #include "remoting/ListenerService.h"
40 #include "util/SPConstants.h"
43 #include <xmltooling/io/HTTPRequest.h>
44 #include <xmltooling/io/HTTPResponse.h>
45 #include <xmltooling/util/DateTime.h>
46 #include <xmltooling/util/NDC.h>
47 #include <xmltooling/util/ParserPool.h>
48 #include <xmltooling/util/Threads.h>
49 #include <xmltooling/util/XMLHelper.h>
50 #include <xercesc/util/XMLUniDefs.hpp>
53 # include <saml/exceptions.h>
54 # include <saml/SAMLConfig.h>
55 # include <saml/saml2/core/Assertions.h>
56 # include <saml/saml2/metadata/Metadata.h>
57 # include <xmltooling/XMLToolingConfig.h>
58 # include <xmltooling/util/StorageService.h>
59 using namespace opensaml::saml2md;
62 # include <xmltooling/util/DateTime.h>
65 using namespace shibsp;
66 using namespace opensaml;
67 using namespace xmltooling;
73 class SSCache : public SessionCacheEx
75 ,public virtual Remoted
79 SSCache(const DOMElement* e);
83 void receive(DDF& in, ostream& out);
86 const Application& app,
87 const HTTPRequest& httpRequest,
88 HTTPResponse& httpResponse,
90 const saml2md::EntityDescriptor* issuer=nullptr,
91 const XMLCh* protocol=nullptr,
92 const saml2::NameID* nameid=nullptr,
93 const XMLCh* authn_instant=nullptr,
94 const XMLCh* session_index=nullptr,
95 const XMLCh* authncontext_class=nullptr,
96 const XMLCh* authncontext_decl=nullptr,
97 const vector<const Assertion*>* tokens=nullptr,
98 const vector<Attribute*>* attributes=nullptr
100 vector<string>::size_type logout(
101 const Application& app,
102 const saml2md::EntityDescriptor* issuer,
103 const saml2::NameID& nameid,
104 const set<string>* indexes,
106 vector<string>& sessions
109 const Application& app,
110 const HTTPRequest& request,
111 const saml2md::EntityDescriptor* issuer,
112 const saml2::NameID& nameid,
113 const set<string>* indexes
116 Session* find(const Application& app, const char* key, const char* client_addr=nullptr, time_t* timeout=nullptr);
117 void remove(const Application& app, const char* key);
120 string active(const Application& app, const HTTPRequest& request) {
121 if (!m_inboundHeader.empty()) {
122 string session_id = request.getHeader(m_inboundHeader.c_str());
123 if (!session_id.empty())
126 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
127 const char* session_id = request.getCookie(shib_cookie.first.c_str());
128 return (session_id ? session_id : "");
131 Session* find(const Application& app, const HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr) {
132 string id = active(app, request);
134 return find(app, id.c_str(), client_addr, timeout);
138 Session* find(const Application& app, HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr);
139 void remove(const Application& app, const HTTPRequest& request, HTTPResponse* response=nullptr);
141 unsigned long getCacheTimeout(const Application& app) {
142 // Computes offset for adjusting expiration of sessions.
143 // This can either be static, or dynamic based on the per-app session timeout.
145 return m_cacheTimeout;
146 pair<bool,unsigned int> timeout = pair<bool,unsigned int>(false, 3600);
147 const PropertySet* props = app.getPropertySet("Sessions");
149 timeout = props->getUnsignedInt("timeout");
151 timeout.second = 3600;
153 return timeout.second + m_cacheAllowance;
159 StorageService* m_storage;
160 StorageService* m_storage_lite;
165 // maintain back-mappings of NameID/SessionIndex -> session key
166 void insert(const char* key, time_t expires, const char* name, const char* index);
167 bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
169 bool m_cacheAssertions;
171 const DOMElement* m_root; // Only valid during initialization
172 unsigned long m_inprocTimeout,m_cacheTimeout,m_cacheAllowance;
173 string m_inboundHeader,m_outboundHeader;
175 // inproc means we buffer sessions in memory
177 map<string,StoredSession*> m_hashtable;
179 // management of buffered sessions
180 void dormant(const char* key);
181 static void* cleanup_fn(void*);
184 CondWait* shutdown_wait;
185 Thread* cleanup_thread;
188 class StoredSession : public virtual Session
191 StoredSession(SSCache* cache, DDF& obj) : m_obj(obj),
195 m_cache(cache), m_expires(0), m_lastAccess(time(nullptr)), m_lock(nullptr) {
196 auto_ptr_XMLCh exp(m_obj["expires"].string());
198 DateTime iso(exp.get());
200 m_expires = iso.getEpoch();
204 const char* nameid = obj["nameid"].string();
206 // Parse and bind the document into an XMLObject.
207 istringstream instr(nameid);
208 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
209 XercesJanitor<DOMDocument> janitor(doc);
210 auto_ptr<saml2::NameID> n(saml2::NameIDBuilder::buildNameID());
211 n->unmarshall(doc->getDocumentElement(), true);
213 m_nameid = n.release();
217 m_lock = Mutex::create();
223 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
226 for_each(m_tokens.begin(), m_tokens.end(), cleanup_pair<string,Assertion>());
242 const char* getID() const {
245 const char* getApplicationID() const {
246 return m_obj["application_id"].string();
248 const char* getClientAddress() const {
249 return m_obj["client_addr"].string();
251 const char* getEntityID() const {
252 return m_obj["entity_id"].string();
254 const char* getProtocol() const {
255 return m_obj["protocol"].string();
257 const char* getAuthnInstant() const {
258 return m_obj["authn_instant"].string();
261 const saml2::NameID* getNameID() const {
265 const char* getSessionIndex() const {
266 return m_obj["session_index"].string();
268 const char* getAuthnContextClassRef() const {
269 return m_obj["authncontext_class"].string();
271 const char* getAuthnContextDeclRef() const {
272 return m_obj["authncontext_decl"].string();
274 const vector<Attribute*>& getAttributes() const {
275 if (m_attributes.empty())
276 unmarshallAttributes();
279 const multimap<string,const Attribute*>& getIndexedAttributes() const {
280 if (m_attributeIndex.empty()) {
281 if (m_attributes.empty())
282 unmarshallAttributes();
283 for (vector<Attribute*>::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) {
284 const vector<string>& aliases = (*a)->getAliases();
285 for (vector<string>::const_iterator alias = aliases.begin(); alias != aliases.end(); ++alias)
286 m_attributeIndex.insert(multimap<string,const Attribute*>::value_type(*alias, *a));
289 return m_attributeIndex;
291 const vector<const char*>& getAssertionIDs() const {
293 DDF ids = m_obj["assertions"];
294 DDF id = ids.first();
295 while (id.isstring()) {
296 m_ids.push_back(id.string());
303 void validate(const Application& application, const char* client_addr, time_t* timeout);
306 void addAttributes(const vector<Attribute*>& attributes);
307 const Assertion* getAssertion(const char* id) const;
308 void addAssertion(Assertion* assertion);
311 time_t getExpiration() const { return m_expires; }
312 time_t getLastAccess() const { return m_lastAccess; }
315 void unmarshallAttributes() const;
319 saml2::NameID* m_nameid;
320 mutable map<string,Assertion*> m_tokens;
322 mutable vector<Attribute*> m_attributes;
323 mutable multimap<string,const Attribute*> m_attributeIndex;
324 mutable vector<const char*> m_ids;
327 time_t m_expires,m_lastAccess;
331 SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)
333 return new SSCache(e);
337 Session* SessionCache::find(const Application& application, HTTPRequest& request, const char* client_addr, time_t* timeout)
339 return find(application, const_cast<const HTTPRequest&>(request), client_addr, timeout);
342 void SHIBSP_API shibsp::registerSessionCaches()
344 SPConfig::getConfig().SessionCacheManager.registerFactory(STORAGESERVICE_SESSION_CACHE, StorageServiceCacheFactory);
355 void StoredSession::unmarshallAttributes() const
357 Attribute* attribute;
358 DDF attrs = m_obj["attributes"];
359 DDF attr = attrs.first();
360 while (!attr.isnull()) {
362 attribute = Attribute::unmarshall(attr);
363 m_attributes.push_back(attribute);
364 if (m_cache->m_log.isDebugEnabled())
365 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",
366 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
368 catch (AttributeException& ex) {
369 const char* id = attr.first().name();
370 m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
376 void StoredSession::validate(const Application& app, const char* client_addr, time_t* timeout)
378 time_t now = time(nullptr);
382 if (now > m_expires) {
383 m_cache->m_log.info("session expired (ID: %s)", getID());
384 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
390 if (m_cache->m_log.isDebugEnabled())
391 m_cache->m_log.debug("comparing client address %s against %s", client_addr, getClientAddress());
392 if (!XMLString::equals(getClientAddress(),client_addr)) {
393 m_cache->m_log.warn("client address mismatch");
394 throw RetryableProfileException(
395 "Your IP address ($1) does not match the address recorded at the time the session was established.",
396 params(1,client_addr)
404 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
405 DDF in("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
408 in.addmember("key").string(getID());
409 in.addmember("version").integer(m_obj["version"].integer());
410 in.addmember("application_id").string(app.getId());
412 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
413 #ifndef HAVE_GMTIME_R
414 struct tm* ptime=gmtime(timeout);
417 struct tm* ptime=gmtime_r(timeout,&res);
420 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
421 in.addmember("timeout").string(timebuf);
425 out=app.getServiceProvider().getListenerService()->send(in);
432 if (out.isstruct()) {
433 // We got an updated record back.
435 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
436 m_attributes.clear();
437 m_attributeIndex.clear();
444 if (!m_cache->m_storage)
445 throw ConfigurationException("Session touch requires a StorageService.");
447 // Do a versioned read.
450 int curver = m_obj["version"].integer();
451 int ver = m_cache->m_storage->readText(getID(), "session", &record, &lastAccess, curver);
453 m_cache->m_log.warn("unsuccessful versioned read of session (ID: %s), cache out of sync?", getID());
454 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
457 // Adjust for expiration to recover last access time and check timeout.
458 unsigned long cacheTimeout = m_cache->getCacheTimeout(app);
459 lastAccess -= cacheTimeout;
460 if (*timeout > 0 && now - lastAccess >= *timeout) {
461 m_cache->m_log.info("session timed out (ID: %s)", getID());
462 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
465 // Update storage expiration, if possible.
467 m_cache->m_storage->updateContext(getID(), now + cacheTimeout);
469 catch (exception& ex) {
470 m_cache->m_log.error("failed to update session expiration: %s", ex.what());
474 // We got an updated record back.
476 istringstream in(record);
479 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
480 m_attributes.clear();
481 m_attributeIndex.clear();
486 throw ConfigurationException("Session touch requires a StorageService.");
495 void StoredSession::addAttributes(const vector<Attribute*>& attributes)
498 xmltooling::NDC ndc("addAttributes");
501 if (!m_cache->m_storage)
502 throw ConfigurationException("Session modification requires a StorageService.");
504 m_cache->m_log.debug("adding attributes to session (%s)", getID());
509 DDF attrs = m_obj["attributes"];
511 attrs = m_obj.addmember("attributes").list();
512 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
513 attr = (*a)->marshall();
517 // Tentatively increment the version.
518 m_obj["version"].integer(m_obj["version"].integer()+1);
522 string record(str.str());
525 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
528 // Roll back modification to record.
529 m_obj["version"].integer(m_obj["version"].integer()-1);
530 vector<Attribute*>::size_type count = attributes.size();
532 attrs.last().destroy();
537 // Roll back modification to record.
538 m_obj["version"].integer(m_obj["version"].integer()-1);
539 vector<Attribute*>::size_type count = attributes.size();
541 attrs.last().destroy();
544 // Fatal problem with update.
545 throw IOException("Unable to update stored session.");
549 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
550 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
552 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
553 throw IOException("Unable to read back stored session.");
558 istringstream in(record);
562 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
563 m_attributes.clear();
564 m_attributeIndex.clear();
565 newobj["version"].integer(ver);
571 } while (ver < 0); // negative indicates a sync issue so we retry
573 TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();
575 xlog->log.infoStream() <<
576 "Added the following attributes to session (ID: " <<
578 ") for (applicationId: " <<
579 m_obj["application_id"].string() <<
581 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a)
582 xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";
585 // We own them now, so clean them up.
586 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
589 const Assertion* StoredSession::getAssertion(const char* id) const
591 if (!m_cache->m_storage)
592 throw ConfigurationException("Assertion retrieval requires a StorageService.");
594 map<string,Assertion*>::const_iterator i = m_tokens.find(id);
595 if (i!=m_tokens.end())
599 if (!m_cache->m_storage->readText(getID(), id, &tokenstr, nullptr))
600 throw FatalProfileException("Assertion not found in cache.");
602 // Parse and bind the document into an XMLObject.
603 istringstream instr(tokenstr);
604 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
605 XercesJanitor<DOMDocument> janitor(doc);
606 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
609 Assertion* token = dynamic_cast<Assertion*>(xmlObject.get());
611 throw FatalProfileException("Request for cached assertion returned an unknown object type.");
613 // Transfer ownership to us.
619 void StoredSession::addAssertion(Assertion* assertion)
622 xmltooling::NDC ndc("addAssertion");
625 if (!m_cache->m_storage)
626 throw ConfigurationException("Session modification requires a StorageService.");
629 throw FatalProfileException("Unknown object type passed to session for storage.");
631 auto_ptr_char id(assertion->getID());
633 m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), getID());
636 if (!m_cache->m_storage->readText(getID(), "session", nullptr, &exp))
637 throw IOException("Unable to load expiration time for stored session.");
639 ostringstream tokenstr;
640 tokenstr << *assertion;
641 if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))
642 throw IOException("Attempted to insert duplicate assertion ID into session.");
646 DDF token = DDF(nullptr).string(id.get());
647 m_obj["assertions"].add(token);
649 // Tentatively increment the version.
650 m_obj["version"].integer(m_obj["version"].integer()+1);
654 string record(str.str());
657 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
661 m_obj["version"].integer(m_obj["version"].integer()-1);
662 m_cache->m_storage->deleteText(getID(), id.get());
668 m_obj["version"].integer(m_obj["version"].integer()-1);
671 // Fatal problem with update.
672 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
673 m_cache->m_storage->deleteText(getID(), id.get());
674 throw IOException("Unable to update stored session.");
678 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
679 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
681 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
682 m_cache->m_storage->deleteText(getID(), id.get());
683 throw IOException("Unable to read back stored session.");
688 istringstream in(record);
692 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
693 m_attributes.clear();
694 m_attributeIndex.clear();
695 newobj["version"].integer(ver);
701 } while (ver < 0); // negative indicates a sync issue so we retry
706 TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();
709 "Added assertion (ID: %s) to session for (applicationId: %s) with (ID: %s)",
710 id.get(), m_obj["application_id"].string(), getID()
716 SessionCache::SessionCache()
720 SessionCache::~SessionCache()
724 SessionCacheEx::SessionCacheEx()
728 SessionCacheEx::~SessionCacheEx()
732 SSCache::SSCache(const DOMElement* e)
733 : m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), inproc(true),
735 m_storage(nullptr), m_storage_lite(nullptr), m_cacheAssertions(true),
737 m_root(e), m_inprocTimeout(900), m_cacheTimeout(0), m_cacheAllowance(0),
738 m_lock(nullptr), shutdown(false), shutdown_wait(nullptr), cleanup_thread(nullptr)
740 SPConfig& conf = SPConfig::getConfig();
741 inproc = conf.isEnabled(SPConfig::InProcess);
743 static const XMLCh cacheAllowance[] = UNICODE_LITERAL_14(c,a,c,h,e,A,l,l,o,w,a,n,c,e);
744 static const XMLCh cacheAssertions[] = UNICODE_LITERAL_15(c,a,c,h,e,A,s,s,e,r,t,i,o,n,s);
745 static const XMLCh cacheTimeout[] = UNICODE_LITERAL_12(c,a,c,h,e,T,i,m,e,o,u,t);
746 static const XMLCh inprocTimeout[] = UNICODE_LITERAL_13(i,n,p,r,o,c,T,i,m,e,o,u,t);
747 static const XMLCh inboundHeader[] = UNICODE_LITERAL_13(i,n,b,o,u,n,d,H,e,a,d,e,r);
748 static const XMLCh outboundHeader[] = UNICODE_LITERAL_14(o,u,t,b,o,u,n,d,H,e,a,d,e,r);
749 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
750 static const XMLCh _StorageServiceLite[] = UNICODE_LITERAL_18(S,t,o,r,a,g,e,S,e,r,v,i,c,e,L,i,t,e);
752 m_cacheTimeout = XMLHelper::getAttrInt(e, 0, cacheTimeout);
753 m_cacheAllowance = XMLHelper::getAttrInt(e, 0, cacheAllowance);
755 m_inprocTimeout = XMLHelper::getAttrInt(e, 900, inprocTimeout);
756 m_inboundHeader = XMLHelper::getAttrString(e, nullptr, inboundHeader);
757 if (!m_inboundHeader.empty())
758 RemotedHandler::addRemotedHeader(m_inboundHeader.c_str());
759 m_outboundHeader = XMLHelper::getAttrString(e, nullptr, outboundHeader);
762 if (conf.isEnabled(SPConfig::OutOfProcess)) {
763 string ssid(XMLHelper::getAttrString(e, nullptr, _StorageService));
765 m_storage = conf.getServiceProvider()->getStorageService(ssid.c_str());
767 m_log.info("bound to StorageService (%s)", ssid.c_str());
769 m_log.warn("specified StorageService (%s) not found", ssid.c_str());
772 m_storage = conf.getServiceProvider()->getStorageService(nullptr);
774 m_log.info("bound to arbitrary StorageService");
776 throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");
779 ssid = XMLHelper::getAttrString(e, nullptr, _StorageServiceLite);
781 m_storage_lite = conf.getServiceProvider()->getStorageService(ssid.c_str());
783 m_log.info("bound to 'lite' StorageService (%s)", ssid.c_str());
785 m_log.warn("specified 'lite' StorageService (%s) not found", ssid.c_str());
787 if (!m_storage_lite) {
788 m_log.info("StorageService for 'lite' use not set, using standard StorageService");
789 m_storage_lite = m_storage;
792 m_cacheAssertions = XMLHelper::getAttrBool(e, true, cacheAssertions);
796 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
798 if (!conf.isEnabled(SPConfig::OutOfProcess) && !listener)
799 throw ConfigurationException("SessionCache requires a ListenerService, but none available.");
800 m_lock = RWLock::create();
801 shutdown_wait = CondWait::create();
802 cleanup_thread = Thread::create(&cleanup_fn, this);
806 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
807 listener->regListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
808 listener->regListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
809 listener->regListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
812 m_log.info("no ListenerService available, cache remoting disabled");
821 // Shut down the cleanup thread and let it know...
823 shutdown_wait->signal();
824 cleanup_thread->join(nullptr);
826 for_each(m_hashtable.begin(),m_hashtable.end(),cleanup_pair<string,StoredSession>());
829 delete cleanup_thread;
830 delete shutdown_wait;
834 SPConfig& conf = SPConfig::getConfig();
835 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
836 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
837 listener->unregListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
838 listener->unregListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
839 listener->unregListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
849 auto_ptr_char temp(SAMLConfig::getConfig().generateIdentifier());
850 m_storage->createString("SessionCacheTest", temp.get(), "Test", time(nullptr) + 60);
851 m_storage->deleteString("SessionCacheTest", temp.get());
854 void SSCache::insert(const char* key, time_t expires, const char* name, const char* index)
857 if (strlen(name) > 255) {
858 dup = string(name).substr(0,255);
863 DDFJanitor jobj(obj);
865 // Since we can't guarantee uniqueness, check for an existing record.
868 int ver = m_storage_lite->readText("NameID", name, &record, &recordexp);
870 // Existing record, so we need to unmarshall it.
871 istringstream in(record);
876 obj = DDF(nullptr).structure();
879 if (!index || !*index)
881 DDF sessions = obj.addmember(index);
882 if (!sessions.islist())
884 DDF session = DDF(nullptr).string(key);
885 sessions.add(session);
887 // Remarshall the record.
891 // Try and store it back...
893 ver = m_storage_lite->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);
895 // Out of sync, or went missing, so retry.
896 return insert(key, expires, name, index);
899 else if (!m_storage_lite->createText("NameID", name, out.str().c_str(), expires)) {
900 // Hit a dup, so just retry, hopefully hitting the other branch.
901 return insert(key, expires, name, index);
905 void SSCache::insert(
906 const Application& app,
907 const HTTPRequest& httpRequest,
908 HTTPResponse& httpResponse,
910 const saml2md::EntityDescriptor* issuer,
911 const XMLCh* protocol,
912 const saml2::NameID* nameid,
913 const XMLCh* authn_instant,
914 const XMLCh* session_index,
915 const XMLCh* authncontext_class,
916 const XMLCh* authncontext_decl,
917 const vector<const Assertion*>* tokens,
918 const vector<Attribute*>* attributes
922 xmltooling::NDC ndc("insert");
925 throw ConfigurationException("SessionCache insertion requires a StorageService.");
927 m_log.debug("creating new session");
929 time_t now = time(nullptr);
930 auto_ptr_char index(session_index);
931 auto_ptr_char entity_id(issuer ? issuer->getEntityID() : nullptr);
932 auto_ptr_char name(nameid ? nameid->getName() : nullptr);
935 // Check for a pending logout.
937 strncpy(namebuf, name.get(), 255);
940 int ver = m_storage_lite->readText("Logout", namebuf, &pending);
943 DDFJanitor jpend(pendobj);
944 istringstream pstr(pending);
946 // IdP.SP.index contains logout expiration, if any.
947 DDF deadmenwalking = pendobj[issuer ? entity_id.get() : "_shibnull"][app.getRelyingParty(issuer)->getString("entityID").second];
948 const char* logexpstr = deadmenwalking[session_index ? index.get() : "_shibnull"].string();
949 if (!logexpstr && session_index) // we tried an exact session match, now try for nullptr
950 logexpstr = deadmenwalking["_shibnull"].string();
952 auto_ptr_XMLCh dt(logexpstr);
953 DateTime dtobj(dt.get());
954 dtobj.parseDateTime();
955 time_t logexp = dtobj.getEpoch();
956 if (now - XMLToolingConfig::getConfig().clock_skew_secs < logexp)
957 throw FatalProfileException("A logout message from your identity provider has blocked your login attempt.");
962 auto_ptr_char key(SAMLConfig::getConfig().generateIdentifier());
964 // Store session properties in DDF.
965 DDF obj = DDF(key.get()).structure();
966 DDFJanitor entryobj(obj);
967 obj.addmember("version").integer(1);
968 obj.addmember("application_id").string(app.getId());
970 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
971 #ifndef HAVE_GMTIME_R
972 struct tm* ptime=gmtime(&expires);
975 struct tm* ptime=gmtime_r(&expires,&res);
978 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
979 obj.addmember("expires").string(timebuf);
981 obj.addmember("client_addr").string(httpRequest.getRemoteAddr().c_str());
983 obj.addmember("entity_id").string(entity_id.get());
985 auto_ptr_char prot(protocol);
986 obj.addmember("protocol").string(prot.get());
989 auto_ptr_char instant(authn_instant);
990 obj.addmember("authn_instant").string(instant.get());
993 obj.addmember("session_index").string(index.get());
994 if (authncontext_class) {
995 auto_ptr_char ac(authncontext_class);
996 obj.addmember("authncontext_class").string(ac.get());
998 if (authncontext_decl) {
999 auto_ptr_char ad(authncontext_decl);
1000 obj.addmember("authncontext_decl").string(ad.get());
1004 ostringstream namestr;
1006 obj.addmember("nameid").string(namestr.str().c_str());
1009 if (tokens && m_cacheAssertions) {
1010 obj.addmember("assertions").list();
1011 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1012 auto_ptr_char tokenid((*t)->getID());
1013 DDF tokid = DDF(nullptr).string(tokenid.get());
1014 obj["assertions"].add(tokid);
1020 DDF attrlist = obj.addmember("attributes").list();
1021 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
1022 attr = (*a)->marshall();
1027 ostringstream record;
1030 m_log.debug("storing new session...");
1031 unsigned long cacheTimeout = getCacheTimeout(app);
1032 if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + cacheTimeout))
1033 throw FatalProfileException("Attempted to create a session with a duplicate key.");
1035 // Store the reverse mapping for logout.
1038 insert(key.get(), expires, name.get(), index.get());
1040 catch (exception& ex) {
1041 m_log.error("error storing back mapping of NameID for logout: %s", ex.what());
1044 if (tokens && m_cacheAssertions) {
1046 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1047 ostringstream tokenstr;
1049 auto_ptr_char tokenid((*t)->getID());
1050 if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + cacheTimeout))
1051 throw IOException("duplicate assertion ID ($1)", params(1, tokenid.get()));
1054 catch (exception& ex) {
1055 m_log.error("error storing assertion along with session: %s", ex.what());
1059 const char* pid = obj["entity_id"].string();
1060 const char* prot = obj["protocol"].string();
1061 m_log.info("new session created: ID (%s) IdP (%s) Protocol(%s) Address (%s)",
1062 key.get(), pid ? pid : "none", prot ? prot : "none", httpRequest.getRemoteAddr().c_str());
1064 // Transaction Logging
1065 string primaryAssertionID("none");
1066 if (m_cacheAssertions) {
1068 primaryAssertionID = obj["assertions"].first().string();
1071 auto_ptr_char tokenid(tokens->front()->getID());
1072 primaryAssertionID = tokenid.get();
1074 TransactionLog* xlog = app.getServiceProvider().getTransactionLog();
1075 Locker locker(xlog);
1076 xlog->log.infoStream() <<
1077 "New session (ID: " <<
1079 ") with (applicationId: " <<
1081 ") for principal from (IdP: " <<
1082 (pid ? pid : "none") <<
1083 ") at (ClientAddress: " <<
1084 httpRequest.getRemoteAddr() <<
1085 ") with (NameIdentifier: " <<
1086 (nameid ? name.get() : "none") <<
1087 ") using (Protocol: " <<
1088 (prot ? prot : "none") <<
1089 ") from (AssertionID: " <<
1090 primaryAssertionID <<
1094 xlog->log.infoStream() <<
1095 "Cached the following attributes with session (ID: " <<
1097 ") for (applicationId: " <<
1100 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a)
1101 xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";
1102 xlog->log.info("}");
1105 if (!m_outboundHeader.empty())
1106 httpResponse.setResponseHeader(m_outboundHeader.c_str(), key.get());
1108 time_t cookieLifetime = 0;
1109 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_", &cookieLifetime);
1110 string k(key.get());
1111 k += shib_cookie.second;
1113 if (cookieLifetime > 0) {
1114 cookieLifetime += now;
1115 #ifndef HAVE_GMTIME_R
1116 ptime=gmtime(&cookieLifetime);
1118 ptime=gmtime_r(&cookieLifetime,&res);
1120 char cookietimebuf[64];
1121 strftime(cookietimebuf,64,"; expires=%a, %d %b %Y %H:%M:%S GMT",ptime);
1125 httpResponse.setCookie(shib_cookie.first.c_str(), k.c_str());
1128 bool SSCache::matches(
1129 const Application& app,
1130 const xmltooling::HTTPRequest& request,
1131 const saml2md::EntityDescriptor* issuer,
1132 const saml2::NameID& nameid,
1133 const set<string>* indexes
1136 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1138 Session* session = find(app, request);
1140 Locker locker(session, false);
1141 if (XMLString::equals(session->getEntityID(), entityID.get()) && session->getNameID() &&
1142 stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1143 return (!indexes || indexes->empty() || (session->getSessionIndex() ? (indexes->count(session->getSessionIndex())>0) : false));
1147 catch (exception& ex) {
1148 m_log.error("error while matching session: %s", ex.what());
1153 vector<string>::size_type SSCache::logout(
1154 const Application& app,
1155 const saml2md::EntityDescriptor* issuer,
1156 const saml2::NameID& nameid,
1157 const set<string>* indexes,
1159 vector<string>& sessionsKilled
1163 xmltooling::NDC ndc("logout");
1167 throw ConfigurationException("SessionCache insertion requires a StorageService.");
1169 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1170 auto_ptr_char name(nameid.getName());
1172 m_log.info("request to logout sessions from (%s) for (%s)", entityID.get() ? entityID.get() : "unknown", name.get());
1174 if (strlen(name.get()) > 255)
1175 const_cast<char*>(name.get())[255] = 0;
1178 DDFJanitor jobj(obj);
1183 // Record the logout to prevent post-delivered assertions.
1184 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1185 #ifndef HAVE_GMTIME_R
1186 struct tm* ptime=gmtime(&expires);
1189 struct tm* ptime=gmtime_r(&expires,&res);
1192 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1195 ver = m_storage_lite->readText("Logout", name.get(), &record, &oldexp);
1197 istringstream lin(record);
1201 obj = DDF(nullptr).structure();
1204 // Structure is keyed by the IdP and SP, with a member per session index containing the expiration.
1205 DDF root = obj.addmember(issuer ? entityID.get() : "_shibnull").addmember(app.getRelyingParty(issuer)->getString("entityID").second);
1207 for (set<string>::const_iterator x = indexes->begin(); x!=indexes->end(); ++x)
1208 root.addmember(x->c_str()).string(timebuf);
1211 root.addmember("_shibnull").string(timebuf);
1219 ver = m_storage_lite->updateText("Logout", name.get(), lout.str().c_str(), max(expires, oldexp), ver);
1221 // Out of sync, or went missing, so retry.
1222 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1225 else if (!m_storage_lite->createText("Logout", name.get(), lout.str().c_str(), expires)) {
1226 // Hit a dup, so just retry, hopefully hitting the other branch.
1227 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1234 // Read in potentially matching sessions.
1235 ver = m_storage_lite->readText("NameID", name.get(), &record);
1237 m_log.debug("no active sessions to logout for supplied issuer and subject");
1241 istringstream in(record);
1244 // The record contains child lists for each known session index.
1246 DDF sessions = obj.first();
1247 while (sessions.islist()) {
1248 if (!indexes || indexes->empty() || indexes->count(sessions.name())) {
1249 key = sessions.first();
1250 while (key.isstring()) {
1251 // Fetch the session for comparison.
1252 Session* session = nullptr;
1254 session = find(app, key.string());
1256 catch (exception& ex) {
1257 m_log.error("error locating session (%s): %s", key.string(), ex.what());
1261 Locker locker(session, false);
1263 if (XMLString::equals(session->getEntityID(), entityID.get())) {
1265 if (stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1266 sessionsKilled.push_back(key.string());
1270 m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());
1274 m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());
1278 // Session's gone, so...
1279 sessionsKilled.push_back(key.string());
1282 key = sessions.next();
1285 // No sessions left for this index?
1286 if (sessions.first().isnull())
1289 sessions = obj.next();
1292 if (obj.first().isnull())
1295 // If possible, write back the mapping record (this isn't crucial).
1298 m_storage_lite->deleteText("NameID", name.get());
1300 else if (!sessionsKilled.empty()) {
1303 if (m_storage_lite->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)
1304 m_log.warn("logout mapping record changed behind us, leaving it alone");
1307 catch (exception& ex) {
1308 m_log.error("error updating logout mapping record: %s", ex.what());
1311 return sessionsKilled.size();
1314 bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
1316 if (!XMLString::equals(n1.getName(), n2.getName()))
1319 const XMLCh* s1 = n1.getFormat();
1320 const XMLCh* s2 = n2.getFormat();
1322 s1 = saml2::NameID::UNSPECIFIED;
1324 s2 = saml2::NameID::UNSPECIFIED;
1325 if (!XMLString::equals(s1,s2))
1328 s1 = n1.getNameQualifier();
1329 s2 = n2.getNameQualifier();
1334 if (!XMLString::equals(s1,s2))
1337 s1 = n1.getSPNameQualifier();
1338 s2 = n2.getSPNameQualifier();
1343 if (!XMLString::equals(s1,s2))
1351 Session* SSCache::find(const Application& app, const char* key, const char* client_addr, time_t* timeout)
1354 xmltooling::NDC ndc("find");
1356 StoredSession* session=nullptr;
1359 m_log.debug("searching local cache for session (%s)", key);
1361 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1362 if (i!=m_hashtable.end()) {
1363 // Save off and lock the session.
1364 session = i->second;
1367 m_log.debug("session found locally, validating it for use");
1375 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1376 m_log.debug("session not found locally, remoting the search");
1377 // Remote the request.
1378 DDF in("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
1381 in.addmember("key").string(key);
1382 in.addmember("application_id").string(app.getId());
1383 if (timeout && *timeout) {
1384 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1385 #ifndef HAVE_GMTIME_R
1386 struct tm* ptime=gmtime(timeout);
1389 struct tm* ptime=gmtime_r(timeout,&res);
1392 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1393 in.addmember("timeout").string(timebuf);
1397 out=app.getServiceProvider().getListenerService()->send(in);
1398 if (!out.isstruct()) {
1400 m_log.debug("session not found in remote cache");
1404 // Wrap the results in a local entry and save it.
1405 session = new StoredSession(this, out);
1406 // The remote end has handled timeout issues, we handle address and expiration checks.
1415 // We're out of process, so we can search the storage service directly.
1418 throw ConfigurationException("SessionCache lookup requires a StorageService.");
1420 m_log.debug("searching for session (%s)", key);
1425 int ver = m_storage->readText(key, "session", &record, &lastAccess);
1429 m_log.debug("reconstituting session and checking validity");
1431 istringstream in(record);
1434 unsigned long cacheTimeout = getCacheTimeout(app);
1435 lastAccess -= cacheTimeout; // adjusts it back to the last time the record's timestamp was touched
1436 time_t now=time(nullptr);
1438 if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {
1439 m_log.info("session timed out (ID: %s)", key);
1441 const char* eid = obj["entity_id"].string();
1444 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1448 throw RetryableProfileException("Your session has expired, and you must re-authenticate.", namedparams(1, "entityID", eid2.c_str()));
1452 // Update storage expiration, if possible.
1454 m_storage->updateContext(key, now + cacheTimeout);
1456 catch (exception& ex) {
1457 m_log.error("failed to update session expiration: %s", ex.what());
1461 // Wrap the results in a local entry and save it.
1462 session = new StoredSession(this, obj);
1463 // We handled timeout issues, still need to handle address and expiration checks.
1466 throw ConfigurationException("SessionCache search requires a StorageService.");
1471 // Lock for writing and repeat the search to avoid duplication.
1473 SharedLock shared(m_lock, false);
1474 if (m_hashtable.count(key)) {
1475 // We're using an existing session entry.
1477 session = m_hashtable[key];
1481 m_hashtable[key]=session;
1487 if (!XMLString::equals(session->getApplicationID(), app.getId())) {
1488 m_log.error("an application (%s) tried to access another application's session", app.getId());
1493 // Verify currency and update the timestamp if indicated by caller.
1495 session->validate(app, client_addr, timeout);
1506 Session* SSCache::find(const Application& app, HTTPRequest& request, const char* client_addr, time_t* timeout)
1508 string id = active(app, request);
1512 Session* session = find(app, id.c_str(), client_addr, timeout);
1515 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1517 if (!m_outboundHeader.empty())
1518 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1519 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1520 string exp(shib_cookie.second);
1521 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1522 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1525 catch (exception&) {
1526 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1528 if (!m_outboundHeader.empty())
1529 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1530 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1531 string exp(shib_cookie.second);
1532 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1533 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1540 void SSCache::remove(const Application& app, const HTTPRequest& request, HTTPResponse* response)
1543 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1545 if (!m_inboundHeader.empty())
1546 session_id = request.getHeader(m_inboundHeader.c_str());
1547 if (session_id.empty()) {
1548 const char* c = request.getCookie(shib_cookie.first.c_str());
1553 if (!session_id.empty()) {
1555 if (!m_outboundHeader.empty())
1556 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1557 string exp(shib_cookie.second);
1558 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1559 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1561 remove(app, session_id.c_str());
1565 void SSCache::remove(const Application& app, const char* key)
1568 xmltooling::NDC ndc("remove");
1570 // Take care of local copy.
1574 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1575 // Remove the session from storage directly.
1577 m_storage->deleteContext(key);
1578 m_log.info("removed session (%s)", key);
1580 TransactionLog* xlog = app.getServiceProvider().getTransactionLog();
1581 Locker locker(xlog);
1582 xlog->log.info("Destroyed session (applicationId: %s) (ID: %s)", app.getId(), key);
1584 throw ConfigurationException("SessionCache removal requires a StorageService.");
1588 // Remote the request.
1589 DDF in("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache");
1592 in.addmember("key").string(key);
1593 in.addmember("application_id").string(app.getId());
1595 DDF out = app.getServiceProvider().getListenerService()->send(in);
1600 void SSCache::dormant(const char* key)
1603 xmltooling::NDC ndc("dormant");
1606 m_log.debug("deleting local copy of session (%s)", key);
1608 // lock the cache for writing, which means we know nobody is sitting in find()
1611 // grab the entry from the table
1612 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1613 if (i==m_hashtable.end()) {
1618 // ok, remove the entry and lock it
1619 StoredSession* entry=i->second;
1620 m_hashtable.erase(key);
1626 // we can release the cache entry lock because we know we're not in the cache anymore
1632 void* SSCache::cleanup_fn(void* p)
1635 xmltooling::NDC ndc("cleanup");
1638 SSCache* pcache = reinterpret_cast<SSCache*>(p);
1641 // First, let's block all signals
1642 Thread::mask_all_signals();
1645 auto_ptr<Mutex> mutex(Mutex::create());
1647 // Load our configuration details...
1648 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
1649 const XMLCh* tag=pcache->m_root ? pcache->m_root->getAttributeNS(nullptr, cleanupInterval) : nullptr;
1650 int rerun_timer = 900;
1652 rerun_timer = XMLString::parseInt(tag);
1653 if (rerun_timer <= 0)
1659 pcache->m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, pcache->m_inprocTimeout);
1661 while (!pcache->shutdown) {
1662 pcache->shutdown_wait->timedwait(mutex.get(), rerun_timer);
1663 if (pcache->shutdown)
1666 // Ok, let's run through the cleanup process and clean out
1667 // really old sessions. This is a two-pass process. The
1668 // first pass is done holding a read-lock while we iterate over
1669 // the cache. The second pass doesn't need a lock because
1670 // the 'deletes' will lock the cache.
1672 // Pass 1: iterate over the map and find all entries that have not been
1673 // used in the allotted timeout.
1674 vector<string> stale_keys;
1675 time_t stale = time(nullptr) - pcache->m_inprocTimeout;
1677 pcache->m_log.debug("cleanup thread running");
1679 pcache->m_lock->rdlock();
1680 for (map<string,StoredSession*>::const_iterator i=pcache->m_hashtable.begin(); i!=pcache->m_hashtable.end(); ++i) {
1681 // If the last access was BEFORE the stale timeout...
1683 time_t last=i->second->getLastAccess();
1684 i->second->unlock();
1686 stale_keys.push_back(i->first);
1688 pcache->m_lock->unlock();
1690 if (!stale_keys.empty()) {
1691 pcache->m_log.info("purging %d old sessions", stale_keys.size());
1693 // Pass 2: walk through the list of stale entries and remove them from the cache
1694 for (vector<string>::const_iterator j = stale_keys.begin(); j != stale_keys.end(); ++j)
1695 pcache->dormant(j->c_str());
1698 pcache->m_log.debug("cleanup thread completed");
1701 pcache->m_log.info("cleanup thread exiting");
1709 void SSCache::receive(DDF& in, ostream& out)
1712 xmltooling::NDC ndc("receive");
1714 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1716 throw ListenerException("Application not found, check configuration?");
1718 if (!strcmp(in.name(),"find::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1719 const char* key=in["key"].string();
1721 throw ListenerException("Required parameters missing for session lookup.");
1723 // Do an unversioned read.
1726 if (!m_storage->readText(key, "session", &record, &lastAccess)) {
1728 DDFJanitor jan(ret);
1733 // Adjust for expiration to recover last access time and check timeout.
1734 unsigned long cacheTimeout = getCacheTimeout(*app);
1735 lastAccess -= cacheTimeout;
1736 time_t now=time(nullptr);
1738 // See if we need to check for a timeout.
1739 if (in["timeout"].string()) {
1741 auto_ptr_XMLCh dt(in["timeout"].string());
1742 DateTime dtobj(dt.get());
1743 dtobj.parseDateTime();
1744 timeout = dtobj.getEpoch();
1746 if (timeout > 0 && now - lastAccess >= timeout) {
1747 m_log.info("session timed out (ID: %s)", key);
1749 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1752 // Update storage expiration, if possible.
1754 m_storage->updateContext(key, now + cacheTimeout);
1756 catch (exception& ex) {
1757 m_log.error("failed to update session expiration: %s", ex.what());
1761 // Send the record back.
1764 else if (!strcmp(in.name(),"touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1765 const char* key=in["key"].string();
1767 throw ListenerException("Required parameters missing for session check.");
1769 // Do a versioned read.
1772 int curver = in["version"].integer();
1773 int ver = m_storage->readText(key, "session", &record, &lastAccess, curver);
1775 m_log.warn("unsuccessful versioned read of session (ID: %s), caches out of sync?", key);
1776 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1779 // Adjust for expiration to recover last access time and check timeout.
1780 unsigned long cacheTimeout = getCacheTimeout(*app);
1781 lastAccess -= cacheTimeout;
1782 time_t now=time(nullptr);
1784 // See if we need to check for a timeout.
1786 auto_ptr_XMLCh dt(in["timeout"].string());
1788 DateTime dtobj(dt.get());
1789 dtobj.parseDateTime();
1790 timeout = dtobj.getEpoch();
1793 if (timeout > 0 && now - lastAccess >= timeout) {
1794 m_log.info("session timed out (ID: %s)", key);
1795 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1798 // Update storage expiration, if possible.
1800 m_storage->updateContext(key, now + cacheTimeout);
1802 catch (exception& ex) {
1803 m_log.error("failed to update session expiration: %s", ex.what());
1807 // Send the record back.
1812 DDFJanitor jan(ret);
1816 else if (!strcmp(in.name(),"remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1817 const char* key=in["key"].string();
1819 throw ListenerException("Required parameter missing for session removal.");
1823 DDFJanitor jan(ret);