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 * StorageServiceSessionCache.cpp
24 * StorageService-based SessionCache implementation.
26 * Instead of optimizing this plugin with a buffering scheme that keeps objects around
27 * and avoids extra parsing steps, I'm assuming that systems that require such can
28 * layer their own cache plugin on top of this version either by delegating to it
29 * or using the remoting support. So this version will load sessions directly
30 * from the StorageService, instantiate enough to expose the Session API,
31 * and then delete everything when they're unlocked. All data in memory is always
32 * kept in sync with the StorageService (no lazy updates).
36 #include "Application.h"
37 #include "exceptions.h"
38 #include "ServiceProvider.h"
39 #include "SessionCacheEx.h"
40 #include "TransactionLog.h"
41 #include "attribute/Attribute.h"
42 #include "handler/RemotedHandler.h"
43 #include "remoting/ListenerService.h"
44 #include "util/SPConstants.h"
47 #include <xmltooling/io/HTTPRequest.h>
48 #include <xmltooling/io/HTTPResponse.h>
49 #include <xmltooling/util/DateTime.h>
50 #include <xmltooling/util/NDC.h>
51 #include <xmltooling/util/ParserPool.h>
52 #include <xmltooling/util/Threads.h>
53 #include <xmltooling/util/XMLHelper.h>
54 #include <xercesc/util/XMLUniDefs.hpp>
57 # include <saml/exceptions.h>
58 # include <saml/SAMLConfig.h>
59 # include <saml/saml2/core/Assertions.h>
60 # include <saml/saml2/metadata/Metadata.h>
61 # include <xmltooling/XMLToolingConfig.h>
62 # include <xmltooling/util/StorageService.h>
63 using namespace opensaml::saml2md;
66 # include <xmltooling/util/DateTime.h>
69 using namespace shibsp;
70 using namespace opensaml;
71 using namespace xmltooling;
77 class SSCache : public SessionCacheEx
79 ,public virtual Remoted
83 SSCache(const DOMElement* e);
87 void receive(DDF& in, ostream& out);
90 const Application& app,
91 const HTTPRequest& httpRequest,
92 HTTPResponse& httpResponse,
94 const EntityDescriptor* issuer=nullptr,
95 const XMLCh* protocol=nullptr,
96 const saml2::NameID* nameid=nullptr,
97 const XMLCh* authn_instant=nullptr,
98 const XMLCh* session_index=nullptr,
99 const XMLCh* authncontext_class=nullptr,
100 const XMLCh* authncontext_decl=nullptr,
101 const vector<const Assertion*>* tokens=nullptr,
102 const vector<Attribute*>* attributes=nullptr
105 insert(dummy, app, httpRequest, httpResponse, expires, issuer, protocol, nameid,
106 authn_instant, session_index, authncontext_class, authncontext_decl, tokens, attributes);
111 const Application& app,
112 const HTTPRequest& httpRequest,
113 HTTPResponse& httpResponse,
115 const EntityDescriptor* issuer=nullptr,
116 const XMLCh* protocol=nullptr,
117 const saml2::NameID* nameid=nullptr,
118 const XMLCh* authn_instant=nullptr,
119 const XMLCh* session_index=nullptr,
120 const XMLCh* authncontext_class=nullptr,
121 const XMLCh* authncontext_decl=nullptr,
122 const vector<const Assertion*>* tokens=nullptr,
123 const vector<Attribute*>* attributes=nullptr
125 vector<string>::size_type logout(
126 const Application& app,
127 const EntityDescriptor* issuer,
128 const saml2::NameID& nameid,
129 const set<string>* indexes,
131 vector<string>& sessions
134 const Application& app,
135 const HTTPRequest& request,
136 const EntityDescriptor* issuer,
137 const saml2::NameID& nameid,
138 const set<string>* indexes
141 Session* find(const Application& app, const char* key, const char* client_addr=nullptr, time_t* timeout=nullptr);
142 void remove(const Application& app, const char* key);
145 string active(const Application& app, const HTTPRequest& request) {
146 if (!m_inboundHeader.empty()) {
147 string session_id = request.getHeader(m_inboundHeader.c_str());
148 if (!session_id.empty())
151 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
152 const char* session_id = request.getCookie(shib_cookie.first.c_str());
153 return (session_id ? session_id : "");
156 Session* find(const Application& app, const HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr) {
157 string id = active(app, request);
159 return find(app, id.c_str(), client_addr, timeout);
163 Session* find(const Application& app, HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr);
164 void remove(const Application& app, const HTTPRequest& request, HTTPResponse* response=nullptr);
166 unsigned long getCacheTimeout(const Application& app) {
167 // Computes offset for adjusting expiration of sessions.
168 // This can either be static, or dynamic based on the per-app session timeout or lifetime.
170 return m_cacheTimeout;
171 pair<bool,unsigned int> timeout = pair<bool,unsigned int>(false, 3600);
172 const PropertySet* props = app.getPropertySet("Sessions");
174 timeout = props->getUnsignedInt("timeout");
176 timeout.second = 3600;
178 // As long as one of the two factors is set, add them together.
179 if (timeout.second > 0 || m_cacheAllowance > 0)
180 return timeout.second + m_cacheAllowance;
182 // If timeouts are off, and there's no cache slop set, then use the lifetime.
183 timeout = pair<bool,unsigned int>(false, 28800);
185 timeout = props->getUnsignedInt("lifetime");
186 if (!timeout.first || timeout.second == 0)
187 timeout.second = 28800;
189 return timeout.second;
195 StorageService* m_storage;
196 StorageService* m_storage_lite;
201 // maintain back-mappings of NameID/SessionIndex -> session key
202 void insert(const char* key, time_t expires, const char* name, const char* index);
203 bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
204 LogoutEvent* newLogoutEvent(const Application& app) const;
206 bool m_cacheAssertions;
208 const DOMElement* m_root; // Only valid during initialization
209 unsigned long m_inprocTimeout,m_cacheTimeout,m_cacheAllowance;
210 string m_inboundHeader,m_outboundHeader;
212 // inproc means we buffer sessions in memory
214 map<string,StoredSession*> m_hashtable;
216 // management of buffered sessions
217 void dormant(const char* key);
218 static void* cleanup_fn(void*);
221 CondWait* shutdown_wait;
222 Thread* cleanup_thread;
225 class StoredSession : public virtual Session
228 StoredSession(SSCache* cache, DDF& obj) : m_obj(obj),
232 m_cache(cache), m_expires(0), m_lastAccess(time(nullptr)), m_lock(nullptr) {
233 auto_ptr_XMLCh exp(m_obj["expires"].string());
235 DateTime iso(exp.get());
237 m_expires = iso.getEpoch();
241 const char* nameid = obj["nameid"].string();
243 // Parse and bind the document into an XMLObject.
244 istringstream instr(nameid);
245 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
246 XercesJanitor<DOMDocument> janitor(doc);
247 auto_ptr<saml2::NameID> n(saml2::NameIDBuilder::buildNameID());
248 n->unmarshall(doc->getDocumentElement(), true);
250 m_nameid = n.release();
254 m_lock = Mutex::create();
260 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
263 for_each(m_tokens.begin(), m_tokens.end(), cleanup_pair<string,Assertion>());
279 const char* getID() const {
282 const char* getApplicationID() const {
283 return m_obj["application_id"].string();
285 const char* getClientAddress() const {
286 return m_obj["client_addr"].string();
288 const char* getEntityID() const {
289 return m_obj["entity_id"].string();
291 const char* getProtocol() const {
292 return m_obj["protocol"].string();
294 const char* getAuthnInstant() const {
295 return m_obj["authn_instant"].string();
298 const saml2::NameID* getNameID() const {
302 const char* getSessionIndex() const {
303 return m_obj["session_index"].string();
305 const char* getAuthnContextClassRef() const {
306 return m_obj["authncontext_class"].string();
308 const char* getAuthnContextDeclRef() const {
309 return m_obj["authncontext_decl"].string();
311 const vector<Attribute*>& getAttributes() const {
312 if (m_attributes.empty())
313 unmarshallAttributes();
316 const multimap<string,const Attribute*>& getIndexedAttributes() const {
317 if (m_attributeIndex.empty()) {
318 if (m_attributes.empty())
319 unmarshallAttributes();
320 for (vector<Attribute*>::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) {
321 const vector<string>& aliases = (*a)->getAliases();
322 for (vector<string>::const_iterator alias = aliases.begin(); alias != aliases.end(); ++alias)
323 m_attributeIndex.insert(multimap<string,const Attribute*>::value_type(*alias, *a));
326 return m_attributeIndex;
328 const vector<const char*>& getAssertionIDs() const {
330 DDF ids = m_obj["assertions"];
331 DDF id = ids.first();
332 while (id.isstring()) {
333 m_ids.push_back(id.string());
340 void validate(const Application& application, const char* client_addr, time_t* timeout);
343 void addAttributes(const vector<Attribute*>& attributes);
344 const Assertion* getAssertion(const char* id) const;
345 void addAssertion(Assertion* assertion);
348 time_t getExpiration() const { return m_expires; }
349 time_t getLastAccess() const { return m_lastAccess; }
352 void unmarshallAttributes() const;
356 saml2::NameID* m_nameid;
357 mutable map<string,Assertion*> m_tokens;
359 mutable vector<Attribute*> m_attributes;
360 mutable multimap<string,const Attribute*> m_attributeIndex;
361 mutable vector<const char*> m_ids;
364 time_t m_expires,m_lastAccess;
368 SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)
370 return new SSCache(e);
374 Session* SessionCache::find(const Application& application, HTTPRequest& request, const char* client_addr, time_t* timeout)
376 return find(application, const_cast<const HTTPRequest&>(request), client_addr, timeout);
379 void SHIBSP_API shibsp::registerSessionCaches()
381 SPConfig::getConfig().SessionCacheManager.registerFactory(STORAGESERVICE_SESSION_CACHE, StorageServiceCacheFactory);
392 void StoredSession::unmarshallAttributes() const
394 Attribute* attribute;
395 DDF attrs = m_obj["attributes"];
396 DDF attr = attrs.first();
397 while (!attr.isnull()) {
399 attribute = Attribute::unmarshall(attr);
400 m_attributes.push_back(attribute);
401 if (m_cache->m_log.isDebugEnabled())
402 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",
403 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
405 catch (AttributeException& ex) {
406 const char* id = attr.first().name();
407 m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
413 void StoredSession::validate(const Application& app, const char* client_addr, time_t* timeout)
415 time_t now = time(nullptr);
419 if (now > m_expires) {
420 m_cache->m_log.info("session expired (ID: %s)", getID());
421 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
427 if (!XMLString::equals(getClientAddress(),client_addr)) {
428 m_cache->m_log.warn("client address mismatch, client (%s), session (%s)", client_addr, getClientAddress());
429 throw RetryableProfileException(
430 "Your IP address ($1) does not match the address recorded at the time the session was established.",
431 params(1,client_addr)
439 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
440 DDF in("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
443 in.addmember("key").string(getID());
444 in.addmember("version").integer(m_obj["version"].integer());
445 in.addmember("application_id").string(app.getId());
447 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
448 #ifndef HAVE_GMTIME_R
449 struct tm* ptime=gmtime(timeout);
452 struct tm* ptime=gmtime_r(timeout,&res);
455 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
456 in.addmember("timeout").string(timebuf);
460 out=app.getServiceProvider().getListenerService()->send(in);
467 if (out.isstruct()) {
468 // We got an updated record back.
470 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
471 m_attributes.clear();
472 m_attributeIndex.clear();
479 if (!m_cache->m_storage)
480 throw ConfigurationException("Session touch requires a StorageService.");
482 // Do a versioned read.
485 int curver = m_obj["version"].integer();
486 int ver = m_cache->m_storage->readText(getID(), "session", &record, &lastAccess, curver);
488 m_cache->m_log.warn("unsuccessful versioned read of session (ID: %s), cache out of sync?", getID());
489 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
492 // Adjust for expiration to recover last access time and check timeout.
493 unsigned long cacheTimeout = m_cache->getCacheTimeout(app);
494 lastAccess -= cacheTimeout;
495 if (*timeout > 0 && now - lastAccess >= *timeout) {
496 m_cache->m_log.info("session timed out (ID: %s)", getID());
497 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
500 // Update storage expiration, if possible.
502 m_cache->m_storage->updateContext(getID(), now + cacheTimeout);
504 catch (exception& ex) {
505 m_cache->m_log.error("failed to update session expiration: %s", ex.what());
509 // We got an updated record back.
511 istringstream in(record);
514 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
515 m_attributes.clear();
516 m_attributeIndex.clear();
521 throw ConfigurationException("Session touch requires a StorageService.");
530 void StoredSession::addAttributes(const vector<Attribute*>& attributes)
533 xmltooling::NDC ndc("addAttributes");
536 if (!m_cache->m_storage)
537 throw ConfigurationException("Session modification requires a StorageService.");
539 m_cache->m_log.debug("adding attributes to session (%s)", getID());
544 DDF attrs = m_obj["attributes"];
546 attrs = m_obj.addmember("attributes").list();
547 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
548 attr = (*a)->marshall();
552 // Tentatively increment the version.
553 m_obj["version"].integer(m_obj["version"].integer()+1);
557 string record(str.str());
560 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
563 // Roll back modification to record.
564 m_obj["version"].integer(m_obj["version"].integer()-1);
565 vector<Attribute*>::size_type count = attributes.size();
567 attrs.last().destroy();
572 // Roll back modification to record.
573 m_obj["version"].integer(m_obj["version"].integer()-1);
574 vector<Attribute*>::size_type count = attributes.size();
576 attrs.last().destroy();
579 // Fatal problem with update.
580 throw IOException("Unable to update stored session.");
584 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
585 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
587 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
588 throw IOException("Unable to read back stored session.");
593 istringstream in(record);
597 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
598 m_attributes.clear();
599 m_attributeIndex.clear();
600 newobj["version"].integer(ver);
606 } while (ver < 0); // negative indicates a sync issue so we retry
608 // We own them now, so clean them up.
609 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
612 const Assertion* StoredSession::getAssertion(const char* id) const
614 if (!m_cache->m_storage)
615 throw ConfigurationException("Assertion retrieval requires a StorageService.");
617 map<string,Assertion*>::const_iterator i = m_tokens.find(id);
618 if (i!=m_tokens.end())
622 if (!m_cache->m_storage->readText(getID(), id, &tokenstr, nullptr))
623 throw FatalProfileException("Assertion not found in cache.");
625 // Parse and bind the document into an XMLObject.
626 istringstream instr(tokenstr);
627 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
628 XercesJanitor<DOMDocument> janitor(doc);
629 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
632 Assertion* token = dynamic_cast<Assertion*>(xmlObject.get());
634 throw FatalProfileException("Request for cached assertion returned an unknown object type.");
636 // Transfer ownership to us.
642 void StoredSession::addAssertion(Assertion* assertion)
645 xmltooling::NDC ndc("addAssertion");
648 if (!m_cache->m_storage)
649 throw ConfigurationException("Session modification requires a StorageService.");
651 throw FatalProfileException("Unknown object type passed to session for storage.");
653 auto_ptr_char id(assertion->getID());
654 if (!id.get() || !*id.get())
655 throw IOException("Assertion did not carry an ID.");
656 else if (strlen(id.get()) > m_cache->m_storage->getCapabilities().getKeySize())
657 throw IOException("Assertion ID ($1) exceeds allowable storage key size.", params(1, id.get()));
659 m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), getID());
662 if (!m_cache->m_storage->readText(getID(), "session", nullptr, &exp))
663 throw IOException("Unable to load expiration time for stored session.");
665 ostringstream tokenstr;
666 tokenstr << *assertion;
667 if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))
668 throw IOException("Attempted to insert duplicate assertion ID into session.");
672 DDF token = DDF(nullptr).string(id.get());
673 m_obj["assertions"].add(token);
675 // Tentatively increment the version.
676 m_obj["version"].integer(m_obj["version"].integer()+1);
680 string record(str.str());
683 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
687 m_obj["version"].integer(m_obj["version"].integer()-1);
688 m_cache->m_storage->deleteText(getID(), id.get());
694 m_obj["version"].integer(m_obj["version"].integer()-1);
697 // Fatal problem with update.
698 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
699 m_cache->m_storage->deleteText(getID(), id.get());
700 throw IOException("Unable to update stored session.");
704 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
705 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
707 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
708 m_cache->m_storage->deleteText(getID(), id.get());
709 throw IOException("Unable to read back stored session.");
714 istringstream in(record);
718 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
719 m_attributes.clear();
720 m_attributeIndex.clear();
721 newobj["version"].integer(ver);
727 } while (ver < 0); // negative indicates a sync issue so we retry
735 SessionCache::SessionCache()
739 SessionCache::~SessionCache()
745 void SessionCache::insert(
747 const Application& app,
748 const HTTPRequest& httpRequest,
749 HTTPResponse& httpResponse,
751 const EntityDescriptor* issuer,
752 const XMLCh* protocol,
753 const saml2::NameID* nameid,
754 const XMLCh* authn_instant,
755 const XMLCh* session_index,
756 const XMLCh* authncontext_class,
757 const XMLCh* authncontext_decl,
758 const vector<const Assertion*>* tokens,
759 const vector<Attribute*>* attributes
761 return insert(app, httpRequest, httpResponse, expires, issuer, protocol, nameid,
762 authn_instant, session_index, authncontext_class, authncontext_decl, tokens, attributes);
767 SessionCacheEx::SessionCacheEx()
771 SessionCacheEx::~SessionCacheEx()
775 SSCache::SSCache(const DOMElement* e)
776 : m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), inproc(true),
778 m_storage(nullptr), m_storage_lite(nullptr), m_cacheAssertions(true),
780 m_root(e), m_inprocTimeout(900), m_cacheTimeout(0), m_cacheAllowance(0),
781 m_lock(nullptr), shutdown(false), shutdown_wait(nullptr), cleanup_thread(nullptr)
783 SPConfig& conf = SPConfig::getConfig();
784 inproc = conf.isEnabled(SPConfig::InProcess);
786 static const XMLCh cacheAllowance[] = UNICODE_LITERAL_14(c,a,c,h,e,A,l,l,o,w,a,n,c,e);
787 static const XMLCh cacheAssertions[] = UNICODE_LITERAL_15(c,a,c,h,e,A,s,s,e,r,t,i,o,n,s);
788 static const XMLCh cacheTimeout[] = UNICODE_LITERAL_12(c,a,c,h,e,T,i,m,e,o,u,t);
789 static const XMLCh inprocTimeout[] = UNICODE_LITERAL_13(i,n,p,r,o,c,T,i,m,e,o,u,t);
790 static const XMLCh inboundHeader[] = UNICODE_LITERAL_13(i,n,b,o,u,n,d,H,e,a,d,e,r);
791 static const XMLCh outboundHeader[] = UNICODE_LITERAL_14(o,u,t,b,o,u,n,d,H,e,a,d,e,r);
792 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
793 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);
795 m_cacheTimeout = XMLHelper::getAttrInt(e, 0, cacheTimeout);
796 m_cacheAllowance = XMLHelper::getAttrInt(e, 0, cacheAllowance);
798 m_inprocTimeout = XMLHelper::getAttrInt(e, 900, inprocTimeout);
799 m_inboundHeader = XMLHelper::getAttrString(e, nullptr, inboundHeader);
800 if (!m_inboundHeader.empty())
801 RemotedHandler::addRemotedHeader(m_inboundHeader.c_str());
802 m_outboundHeader = XMLHelper::getAttrString(e, nullptr, outboundHeader);
805 if (conf.isEnabled(SPConfig::OutOfProcess)) {
806 string ssid(XMLHelper::getAttrString(e, nullptr, _StorageService));
808 m_storage = conf.getServiceProvider()->getStorageService(ssid.c_str());
810 m_log.info("bound to StorageService (%s)", ssid.c_str());
812 m_log.warn("specified StorageService (%s) not found", ssid.c_str());
815 m_storage = conf.getServiceProvider()->getStorageService(nullptr);
817 m_log.info("bound to arbitrary StorageService");
819 throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");
822 ssid = XMLHelper::getAttrString(e, nullptr, _StorageServiceLite);
824 m_storage_lite = conf.getServiceProvider()->getStorageService(ssid.c_str());
826 m_log.info("bound to 'lite' StorageService (%s)", ssid.c_str());
828 m_log.warn("specified 'lite' StorageService (%s) not found", ssid.c_str());
830 if (!m_storage_lite) {
831 m_log.info("StorageService for 'lite' use not set, using standard StorageService");
832 m_storage_lite = m_storage;
835 m_cacheAssertions = XMLHelper::getAttrBool(e, true, cacheAssertions);
839 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
841 if (!conf.isEnabled(SPConfig::OutOfProcess) && !listener)
842 throw ConfigurationException("SessionCache requires a ListenerService, but none available.");
843 m_lock = RWLock::create();
844 shutdown_wait = CondWait::create();
845 cleanup_thread = Thread::create(&cleanup_fn, this);
849 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
850 listener->regListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
851 listener->regListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
852 listener->regListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
855 m_log.info("no ListenerService available, cache remoting disabled");
864 // Shut down the cleanup thread and let it know...
866 shutdown_wait->signal();
867 cleanup_thread->join(nullptr);
869 for_each(m_hashtable.begin(),m_hashtable.end(),cleanup_pair<string,StoredSession>());
872 delete cleanup_thread;
873 delete shutdown_wait;
877 SPConfig& conf = SPConfig::getConfig();
878 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
879 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
880 listener->unregListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
881 listener->unregListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
882 listener->unregListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
892 auto_ptr_char temp(SAMLConfig::getConfig().generateIdentifier());
893 m_storage->createString("SessionCacheTest", temp.get(), "Test", time(nullptr) + 60);
894 m_storage->deleteString("SessionCacheTest", temp.get());
897 void SSCache::insert(const char* key, time_t expires, const char* name, const char* index)
900 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
901 if (strlen(name) > storageLimit) {
902 dup = string(name).substr(0, storageLimit);
907 DDFJanitor jobj(obj);
909 // Since we can't guarantee uniqueness, check for an existing record.
912 int ver = m_storage_lite->readText("NameID", name, &record, &recordexp);
914 // Existing record, so we need to unmarshall it.
915 istringstream in(record);
920 obj = DDF(nullptr).structure();
923 if (!index || !*index)
925 DDF sessions = obj.addmember(index);
926 if (!sessions.islist())
928 DDF session = DDF(nullptr).string(key);
929 sessions.add(session);
931 // Remarshall the record.
935 // Try and store it back...
937 ver = m_storage_lite->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);
939 // Out of sync, or went missing, so retry.
940 return insert(key, expires, name, index);
943 else if (!m_storage_lite->createText("NameID", name, out.str().c_str(), expires)) {
944 // Hit a dup, so just retry, hopefully hitting the other branch.
945 return insert(key, expires, name, index);
949 void SSCache::insert(
951 const Application& app,
952 const HTTPRequest& httpRequest,
953 HTTPResponse& httpResponse,
955 const saml2md::EntityDescriptor* issuer,
956 const XMLCh* protocol,
957 const saml2::NameID* nameid,
958 const XMLCh* authn_instant,
959 const XMLCh* session_index,
960 const XMLCh* authncontext_class,
961 const XMLCh* authncontext_decl,
962 const vector<const Assertion*>* tokens,
963 const vector<Attribute*>* attributes
967 xmltooling::NDC ndc("insert");
970 throw ConfigurationException("SessionCache insertion requires a StorageService.");
972 m_log.debug("creating new session");
974 time_t now = time(nullptr);
975 auto_ptr_char index(session_index);
976 auto_ptr_char entity_id(issuer ? issuer->getEntityID() : nullptr);
977 auto_ptr_char name(nameid ? nameid->getName() : nullptr);
979 if (name.get() && *name.get()) {
980 // Check for a pending logout.
981 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
982 string namebuf = name.get();
983 if (namebuf.length() > storageLimit)
984 namebuf = namebuf.substr(0, storageLimit);
986 int ver = m_storage_lite->readText("Logout", namebuf.c_str(), &pending);
989 DDFJanitor jpend(pendobj);
990 istringstream pstr(pending);
992 // IdP.SP.index contains logout expiration, if any.
993 DDF deadmenwalking = pendobj[issuer ? entity_id.get() : "_shibnull"][app.getRelyingParty(issuer)->getString("entityID").second];
994 const char* logexpstr = deadmenwalking[session_index ? index.get() : "_shibnull"].string();
995 if (!logexpstr && session_index) // we tried an exact session match, now try for nullptr
996 logexpstr = deadmenwalking["_shibnull"].string();
998 auto_ptr_XMLCh dt(logexpstr);
999 DateTime dtobj(dt.get());
1000 dtobj.parseDateTime();
1001 time_t logexp = dtobj.getEpoch();
1002 if (now - XMLToolingConfig::getConfig().clock_skew_secs < logexp)
1003 throw FatalProfileException("A logout message from your identity provider has blocked your login attempt.");
1008 XMLCh* widekey = SAMLConfig::getConfig().generateIdentifier();
1009 auto_ptr_char key(widekey);
1010 XMLString::release(&widekey);
1012 // Store session properties in DDF.
1013 DDF obj = DDF(key.get()).structure();
1014 DDFJanitor entryobj(obj);
1015 obj.addmember("version").integer(1);
1016 obj.addmember("application_id").string(app.getId());
1018 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1019 #ifndef HAVE_GMTIME_R
1020 struct tm* ptime=gmtime(&expires);
1023 struct tm* ptime=gmtime_r(&expires,&res);
1026 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1027 obj.addmember("expires").string(timebuf);
1029 obj.addmember("client_addr").string(httpRequest.getRemoteAddr().c_str());
1031 obj.addmember("entity_id").string(entity_id.get());
1033 auto_ptr_char prot(protocol);
1034 obj.addmember("protocol").string(prot.get());
1036 if (authn_instant) {
1037 auto_ptr_char instant(authn_instant);
1038 obj.addmember("authn_instant").string(instant.get());
1041 obj.addmember("session_index").string(index.get());
1042 if (authncontext_class) {
1043 auto_ptr_char ac(authncontext_class);
1044 obj.addmember("authncontext_class").string(ac.get());
1046 if (authncontext_decl) {
1047 auto_ptr_char ad(authncontext_decl);
1048 obj.addmember("authncontext_decl").string(ad.get());
1052 ostringstream namestr;
1054 obj.addmember("nameid").string(namestr.str().c_str());
1057 if (tokens && m_cacheAssertions) {
1058 obj.addmember("assertions").list();
1059 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1060 auto_ptr_char tokenid((*t)->getID());
1061 DDF tokid = DDF(nullptr).string(tokenid.get());
1062 obj["assertions"].add(tokid);
1068 DDF attrlist = obj.addmember("attributes").list();
1069 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
1070 attr = (*a)->marshall();
1075 ostringstream record;
1078 m_log.debug("storing new session...");
1079 unsigned long cacheTimeout = getCacheTimeout(app);
1080 if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + cacheTimeout))
1081 throw FatalProfileException("Attempted to create a session with a duplicate key.");
1083 // Store the reverse mapping for logout.
1086 insert(key.get(), expires, name.get(), index.get());
1088 catch (exception& ex) {
1089 m_log.error("error storing back mapping of NameID for logout: %s", ex.what());
1092 if (tokens && m_cacheAssertions) {
1094 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1095 ostringstream tokenstr;
1097 auto_ptr_char tokenid((*t)->getID());
1098 if (!tokenid.get() || !*tokenid.get() || strlen(tokenid.get()) > m_storage->getCapabilities().getKeySize())
1099 throw IOException("Assertion ID is missing or exceeds key size of storage service.");
1100 else if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + cacheTimeout))
1101 throw IOException("Duplicate assertion ID ($1)", params(1, tokenid.get()));
1104 catch (exception& ex) {
1105 m_log.error("error storing assertion along with session: %s", ex.what());
1109 const char* pid = obj["entity_id"].string();
1110 const char* prot = obj["protocol"].string();
1111 m_log.info("new session created: ID (%s) IdP (%s) Protocol(%s) Address (%s)",
1112 key.get(), pid ? pid : "none", prot ? prot : "none", httpRequest.getRemoteAddr().c_str());
1114 if (!m_outboundHeader.empty())
1115 httpResponse.setResponseHeader(m_outboundHeader.c_str(), key.get());
1117 time_t cookieLifetime = 0;
1118 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_", &cookieLifetime);
1119 string k(key.get());
1120 k += shib_cookie.second;
1122 if (cookieLifetime > 0) {
1123 cookieLifetime += now;
1124 #ifndef HAVE_GMTIME_R
1125 ptime=gmtime(&cookieLifetime);
1127 ptime=gmtime_r(&cookieLifetime,&res);
1129 char cookietimebuf[64];
1130 strftime(cookietimebuf,64,"; expires=%a, %d %b %Y %H:%M:%S GMT",ptime);
1134 httpResponse.setCookie(shib_cookie.first.c_str(), k.c_str());
1135 sessionID = key.get();
1138 bool SSCache::matches(
1139 const Application& app,
1140 const xmltooling::HTTPRequest& request,
1141 const saml2md::EntityDescriptor* issuer,
1142 const saml2::NameID& nameid,
1143 const set<string>* indexes
1146 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1148 Session* session = find(app, request);
1150 Locker locker(session, false);
1151 if (XMLString::equals(session->getEntityID(), entityID.get()) && session->getNameID() &&
1152 stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1153 return (!indexes || indexes->empty() || (session->getSessionIndex() ? (indexes->count(session->getSessionIndex())>0) : false));
1157 catch (exception& ex) {
1158 m_log.error("error while matching session: %s", ex.what());
1163 vector<string>::size_type SSCache::logout(
1164 const Application& app,
1165 const saml2md::EntityDescriptor* issuer,
1166 const saml2::NameID& nameid,
1167 const set<string>* indexes,
1169 vector<string>& sessionsKilled
1173 xmltooling::NDC ndc("logout");
1177 throw ConfigurationException("SessionCache logout requires a StorageService.");
1179 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1180 auto_ptr_char name(nameid.getName());
1182 m_log.info("request to logout sessions from (%s) for (%s)", entityID.get() ? entityID.get() : "unknown", name.get());
1184 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
1185 if (strlen(name.get()) > storageLimit)
1186 const_cast<char*>(name.get())[storageLimit] = 0;
1189 DDFJanitor jobj(obj);
1194 // Record the logout to prevent post-delivered assertions.
1195 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1196 #ifndef HAVE_GMTIME_R
1197 struct tm* ptime=gmtime(&expires);
1200 struct tm* ptime=gmtime_r(&expires,&res);
1203 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1206 ver = m_storage_lite->readText("Logout", name.get(), &record, &oldexp);
1208 istringstream lin(record);
1212 obj = DDF(nullptr).structure();
1215 // Structure is keyed by the IdP and SP, with a member per session index containing the expiration.
1216 DDF root = obj.addmember(issuer ? entityID.get() : "_shibnull").addmember(app.getRelyingParty(issuer)->getString("entityID").second);
1218 for (set<string>::const_iterator x = indexes->begin(); x!=indexes->end(); ++x)
1219 root.addmember(x->c_str()).string(timebuf);
1222 root.addmember("_shibnull").string(timebuf);
1230 ver = m_storage_lite->updateText("Logout", name.get(), lout.str().c_str(), max(expires, oldexp), ver);
1232 // Out of sync, or went missing, so retry.
1233 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1236 else if (!m_storage_lite->createText("Logout", name.get(), lout.str().c_str(), expires)) {
1237 // Hit a dup, so just retry, hopefully hitting the other branch.
1238 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1245 // Read in potentially matching sessions.
1246 ver = m_storage_lite->readText("NameID", name.get(), &record);
1248 m_log.debug("no active sessions to logout for supplied issuer and subject");
1252 istringstream in(record);
1255 // The record contains child lists for each known session index.
1257 DDF sessions = obj.first();
1258 while (sessions.islist()) {
1259 if (!indexes || indexes->empty() || indexes->count(sessions.name())) {
1260 key = sessions.first();
1261 while (key.isstring()) {
1262 // Fetch the session for comparison.
1263 Session* session = nullptr;
1265 session = find(app, key.string());
1267 catch (exception& ex) {
1268 m_log.error("error locating session (%s): %s", key.string(), ex.what());
1272 Locker locker(session, false);
1274 if (XMLString::equals(session->getEntityID(), entityID.get())) {
1276 if (stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1277 sessionsKilled.push_back(key.string());
1281 m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());
1285 m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());
1289 // Session's gone, so...
1290 sessionsKilled.push_back(key.string());
1293 key = sessions.next();
1296 // No sessions left for this index?
1297 if (sessions.first().isnull())
1300 sessions = obj.next();
1303 if (obj.first().isnull())
1306 // If possible, write back the mapping record (this isn't crucial).
1309 m_storage_lite->deleteText("NameID", name.get());
1311 else if (!sessionsKilled.empty()) {
1314 if (m_storage_lite->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)
1315 m_log.warn("logout mapping record changed behind us, leaving it alone");
1318 catch (exception& ex) {
1319 m_log.error("error updating logout mapping record: %s", ex.what());
1322 return sessionsKilled.size();
1325 bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
1327 if (!XMLString::equals(n1.getName(), n2.getName()))
1330 const XMLCh* s1 = n1.getFormat();
1331 const XMLCh* s2 = n2.getFormat();
1333 s1 = saml2::NameID::UNSPECIFIED;
1335 s2 = saml2::NameID::UNSPECIFIED;
1336 if (!XMLString::equals(s1,s2))
1339 s1 = n1.getNameQualifier();
1340 s2 = n2.getNameQualifier();
1345 if (!XMLString::equals(s1,s2))
1348 s1 = n1.getSPNameQualifier();
1349 s2 = n2.getSPNameQualifier();
1354 if (!XMLString::equals(s1,s2))
1360 LogoutEvent* SSCache::newLogoutEvent(const Application& app) const
1362 if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
1365 auto_ptr<TransactionLog::Event> event(SPConfig::getConfig().EventManager.newPlugin(LOGOUT_EVENT, nullptr));
1366 LogoutEvent* logout_event = dynamic_cast<LogoutEvent*>(event.get());
1368 logout_event->m_app = &app;
1370 return logout_event;
1373 m_log.warn("unable to audit event, log event object was of an incorrect type");
1376 catch (exception& ex) {
1377 m_log.warn("exception auditing event: %s", ex.what());
1384 Session* SSCache::find(const Application& app, const char* key, const char* client_addr, time_t* timeout)
1387 xmltooling::NDC ndc("find");
1389 StoredSession* session=nullptr;
1392 m_log.debug("searching local cache for session (%s)", key);
1394 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1395 if (i!=m_hashtable.end()) {
1396 // Save off and lock the session.
1397 session = i->second;
1400 m_log.debug("session found locally, validating it for use");
1408 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1409 m_log.debug("session not found locally, remoting the search");
1410 // Remote the request.
1411 DDF in("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
1414 in.addmember("key").string(key);
1415 in.addmember("application_id").string(app.getId());
1416 if (timeout && *timeout) {
1417 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1418 #ifndef HAVE_GMTIME_R
1419 struct tm* ptime=gmtime(timeout);
1422 struct tm* ptime=gmtime_r(timeout,&res);
1425 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1426 in.addmember("timeout").string(timebuf);
1430 out=app.getServiceProvider().getListenerService()->send(in);
1431 if (!out.isstruct()) {
1433 m_log.debug("session not found in remote cache");
1437 // Wrap the results in a local entry and save it.
1438 session = new StoredSession(this, out);
1439 // The remote end has handled timeout issues, we handle address and expiration checks.
1448 // We're out of process, so we can search the storage service directly.
1451 throw ConfigurationException("SessionCache lookup requires a StorageService.");
1453 m_log.debug("searching for session (%s)", key);
1458 int ver = m_storage->readText(key, "session", &record, &lastAccess);
1462 m_log.debug("reconstituting session and checking validity");
1464 istringstream in(record);
1467 unsigned long cacheTimeout = getCacheTimeout(app);
1468 lastAccess -= cacheTimeout; // adjusts it back to the last time the record's timestamp was touched
1469 time_t now=time(nullptr);
1471 if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {
1472 m_log.info("session timed out (ID: %s)", key);
1473 auto_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1474 if (logout_event.get()) {
1475 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1476 logout_event->m_sessions.push_back(key);
1477 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1480 const char* eid = obj["entity_id"].string();
1483 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1487 throw RetryableProfileException("Your session has expired, and you must re-authenticate.", namedparams(1, "entityID", eid2.c_str()));
1491 // Update storage expiration, if possible.
1493 m_storage->updateContext(key, now + cacheTimeout);
1495 catch (exception& ex) {
1496 m_log.error("failed to update session expiration: %s", ex.what());
1500 // Wrap the results in a local entry and save it.
1501 session = new StoredSession(this, obj);
1502 // We handled timeout issues, still need to handle address and expiration checks.
1505 throw ConfigurationException("SessionCache search requires a StorageService.");
1510 // Lock for writing and repeat the search to avoid duplication.
1512 SharedLock shared(m_lock, false);
1513 if (m_hashtable.count(key)) {
1514 // We're using an existing session entry.
1516 session = m_hashtable[key];
1520 m_hashtable[key]=session;
1526 if (!XMLString::equals(session->getApplicationID(), app.getId())) {
1527 m_log.error("an application (%s) tried to access another application's session", app.getId());
1532 // Verify currency and update the timestamp if indicated by caller.
1534 session->validate(app, client_addr, timeout);
1538 auto_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1539 if (logout_event.get()) {
1540 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1541 logout_event->m_session = session;
1542 logout_event->m_sessions.push_back(session->getID());
1543 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1554 Session* SSCache::find(const Application& app, HTTPRequest& request, const char* client_addr, time_t* timeout)
1556 string id = active(app, request);
1560 Session* session = find(app, id.c_str(), client_addr, timeout);
1563 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1565 if (!m_outboundHeader.empty())
1566 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1567 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1568 string exp(shib_cookie.second);
1569 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1570 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1573 catch (exception&) {
1574 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1576 if (!m_outboundHeader.empty())
1577 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1578 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1579 string exp(shib_cookie.second);
1580 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1581 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1588 void SSCache::remove(const Application& app, const HTTPRequest& request, HTTPResponse* response)
1591 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1593 if (!m_inboundHeader.empty())
1594 session_id = request.getHeader(m_inboundHeader.c_str());
1595 if (session_id.empty()) {
1596 const char* c = request.getCookie(shib_cookie.first.c_str());
1601 if (!session_id.empty()) {
1603 if (!m_outboundHeader.empty())
1604 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1605 string exp(shib_cookie.second);
1606 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1607 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1609 remove(app, session_id.c_str());
1613 void SSCache::remove(const Application& app, const char* key)
1616 xmltooling::NDC ndc("remove");
1618 // Take care of local copy.
1622 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1623 // Remove the session from storage directly.
1625 m_storage->deleteContext(key);
1626 m_log.info("removed session (%s)", key);
1628 throw ConfigurationException("SessionCache removal requires a StorageService.");
1632 // Remote the request.
1633 DDF in("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache");
1636 in.addmember("key").string(key);
1637 in.addmember("application_id").string(app.getId());
1639 DDF out = app.getServiceProvider().getListenerService()->send(in);
1644 void SSCache::dormant(const char* key)
1647 xmltooling::NDC ndc("dormant");
1650 m_log.debug("deleting local copy of session (%s)", key);
1652 // lock the cache for writing, which means we know nobody is sitting in find()
1655 // grab the entry from the table
1656 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1657 if (i==m_hashtable.end()) {
1662 // ok, remove the entry and lock it
1663 StoredSession* entry=i->second;
1664 m_hashtable.erase(key);
1670 // we can release the cache entry lock because we know we're not in the cache anymore
1676 void* SSCache::cleanup_fn(void* p)
1679 xmltooling::NDC ndc("cleanup");
1682 SSCache* pcache = reinterpret_cast<SSCache*>(p);
1685 // First, let's block all signals
1686 Thread::mask_all_signals();
1689 auto_ptr<Mutex> mutex(Mutex::create());
1691 // Load our configuration details...
1692 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
1693 const XMLCh* tag=pcache->m_root ? pcache->m_root->getAttributeNS(nullptr, cleanupInterval) : nullptr;
1694 int rerun_timer = 900;
1696 rerun_timer = XMLString::parseInt(tag);
1697 if (rerun_timer <= 0)
1703 pcache->m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, pcache->m_inprocTimeout);
1705 while (!pcache->shutdown) {
1706 pcache->shutdown_wait->timedwait(mutex.get(), rerun_timer);
1707 if (pcache->shutdown)
1710 // Ok, let's run through the cleanup process and clean out
1711 // really old sessions. This is a two-pass process. The
1712 // first pass is done holding a read-lock while we iterate over
1713 // the cache. The second pass doesn't need a lock because
1714 // the 'deletes' will lock the cache.
1716 // Pass 1: iterate over the map and find all entries that have not been
1717 // used in the allotted timeout.
1718 vector<string> stale_keys;
1719 time_t stale = time(nullptr) - pcache->m_inprocTimeout;
1721 pcache->m_log.debug("cleanup thread running");
1723 pcache->m_lock->rdlock();
1724 for (map<string,StoredSession*>::const_iterator i=pcache->m_hashtable.begin(); i!=pcache->m_hashtable.end(); ++i) {
1725 // If the last access was BEFORE the stale timeout...
1727 time_t last=i->second->getLastAccess();
1728 i->second->unlock();
1730 stale_keys.push_back(i->first);
1732 pcache->m_lock->unlock();
1734 if (!stale_keys.empty()) {
1735 pcache->m_log.info("purging %d old sessions", stale_keys.size());
1737 // Pass 2: walk through the list of stale entries and remove them from the cache
1738 for (vector<string>::const_iterator j = stale_keys.begin(); j != stale_keys.end(); ++j)
1739 pcache->dormant(j->c_str());
1742 pcache->m_log.debug("cleanup thread completed");
1745 pcache->m_log.info("cleanup thread exiting");
1753 void SSCache::receive(DDF& in, ostream& out)
1756 xmltooling::NDC ndc("receive");
1758 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1760 throw ListenerException("Application not found, check configuration?");
1762 if (!strcmp(in.name(),"find::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1763 const char* key=in["key"].string();
1765 throw ListenerException("Required parameters missing for session lookup.");
1767 // Do an unversioned read.
1770 if (!m_storage->readText(key, "session", &record, &lastAccess)) {
1772 DDFJanitor jan(ret);
1777 // Adjust for expiration to recover last access time and check timeout.
1778 unsigned long cacheTimeout = getCacheTimeout(*app);
1779 lastAccess -= cacheTimeout;
1780 time_t now=time(nullptr);
1782 // See if we need to check for a timeout.
1783 if (in["timeout"].string()) {
1785 auto_ptr_XMLCh dt(in["timeout"].string());
1786 DateTime dtobj(dt.get());
1787 dtobj.parseDateTime();
1788 timeout = dtobj.getEpoch();
1790 if (timeout > 0 && now - lastAccess >= timeout) {
1791 m_log.info("session timed out (ID: %s)", key);
1792 auto_ptr<LogoutEvent> logout_event(newLogoutEvent(*app));
1793 if (logout_event.get()) {
1794 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1795 logout_event->m_sessions.push_back(key);
1796 app->getServiceProvider().getTransactionLog()->write(*logout_event);
1799 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1802 // Update storage expiration, if possible.
1804 m_storage->updateContext(key, now + cacheTimeout);
1806 catch (exception& ex) {
1807 m_log.error("failed to update session expiration: %s", ex.what());
1811 // Send the record back.
1814 else if (!strcmp(in.name(),"touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1815 const char* key=in["key"].string();
1817 throw ListenerException("Required parameters missing for session check.");
1819 // Do a versioned read.
1822 int curver = in["version"].integer();
1823 int ver = m_storage->readText(key, "session", &record, &lastAccess, curver);
1825 m_log.warn("unsuccessful versioned read of session (ID: %s), caches out of sync?", key);
1826 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1829 // Adjust for expiration to recover last access time and check timeout.
1830 unsigned long cacheTimeout = getCacheTimeout(*app);
1831 lastAccess -= cacheTimeout;
1832 time_t now=time(nullptr);
1834 // See if we need to check for a timeout.
1836 auto_ptr_XMLCh dt(in["timeout"].string());
1838 DateTime dtobj(dt.get());
1839 dtobj.parseDateTime();
1840 timeout = dtobj.getEpoch();
1843 if (timeout > 0 && now - lastAccess >= timeout) {
1844 m_log.info("session timed out (ID: %s)", key);
1845 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1848 // Update storage expiration, if possible.
1850 m_storage->updateContext(key, now + cacheTimeout);
1852 catch (exception& ex) {
1853 m_log.error("failed to update session expiration: %s", ex.what());
1857 // Send the record back.
1862 DDFJanitor jan(ret);
1866 else if (!strcmp(in.name(),"remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1867 const char* key=in["key"].string();
1869 throw ListenerException("Required parameter missing for session removal.");
1873 DDFJanitor jan(ret);