2 * Copyright 2001-2009 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 "remoting/ListenerService.h"
39 #include "util/SPConstants.h"
42 #include <xmltooling/util/DateTime.h>
43 #include <xmltooling/util/NDC.h>
44 #include <xmltooling/util/XMLHelper.h>
45 #include <xercesc/util/XMLUniDefs.hpp>
48 # include <saml/SAMLConfig.h>
49 # include <xmltooling/XMLToolingConfig.h>
50 # include <xmltooling/util/StorageService.h>
51 using namespace opensaml::saml2md;
54 # include <xmltooling/util/DateTime.h>
57 using namespace shibsp;
58 using namespace opensaml;
59 using namespace xmltooling;
65 class SSCache : public SessionCacheEx
67 ,public virtual Remoted
71 SSCache(const DOMElement* e);
75 void receive(DDF& in, ostream& out);
78 const Application& application,
79 const HTTPRequest& httpRequest,
80 HTTPResponse& httpResponse,
82 const saml2md::EntityDescriptor* issuer=NULL,
83 const XMLCh* protocol=NULL,
84 const saml2::NameID* nameid=NULL,
85 const XMLCh* authn_instant=NULL,
86 const XMLCh* session_index=NULL,
87 const XMLCh* authncontext_class=NULL,
88 const XMLCh* authncontext_decl=NULL,
89 const vector<const Assertion*>* tokens=NULL,
90 const vector<Attribute*>* attributes=NULL
92 vector<string>::size_type logout(
93 const Application& application,
94 const saml2md::EntityDescriptor* issuer,
95 const saml2::NameID& nameid,
96 const set<string>* indexes,
98 vector<string>& sessions
101 const Application& application,
102 const xmltooling::HTTPRequest& request,
103 const saml2md::EntityDescriptor* issuer,
104 const saml2::NameID& nameid,
105 const set<string>* indexes
108 Session* find(const Application& application, const char* key, const char* client_addr=NULL, time_t* timeout=NULL);
109 void remove(const Application& application, const char* key);
112 string active(const Application& application, const xmltooling::HTTPRequest& request) {
113 pair<string,const char*> shib_cookie = application.getCookieNameProps("_shibsession_");
114 const char* session_id = request.getCookie(shib_cookie.first.c_str());
115 return (session_id ? session_id : "");
118 Session* find(const Application& application, const HTTPRequest& request, const char* client_addr=NULL, time_t* timeout=NULL) {
119 string id = active(application, request);
121 return find(application, id.c_str(), client_addr, timeout);
125 Session* find(const Application& application, HTTPRequest& request, const char* client_addr=NULL, time_t* timeout=NULL) {
126 string id = active(application, request);
130 Session* session = find(application, id.c_str(), client_addr, timeout);
133 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
135 pair<string,const char*> shib_cookie = application.getCookieNameProps("_shibsession_");
136 string exp(shib_cookie.second);
137 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
138 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
142 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
144 pair<string,const char*> shib_cookie = application.getCookieNameProps("_shibsession_");
145 string exp(shib_cookie.second);
146 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
147 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
154 void remove(const Application& application, const HTTPRequest& request, HTTPResponse* response=NULL) {
155 pair<string,const char*> shib_cookie = application.getCookieNameProps("_shibsession_");
156 const char* session_id = request.getCookie(shib_cookie.first.c_str());
157 if (session_id && *session_id) {
159 string exp(shib_cookie.second);
160 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
161 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
163 remove(application, session_id);
171 unsigned long m_cacheTimeout;
173 StorageService* m_storage;
174 StorageService* m_storage_lite;
179 // maintain back-mappings of NameID/SessionIndex -> session key
180 void insert(const char* key, time_t expires, const char* name, const char* index);
181 bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
184 const DOMElement* m_root; // Only valid during initialization
185 unsigned long m_inprocTimeout;
187 // inproc means we buffer sessions in memory
189 map<string,StoredSession*> m_hashtable;
191 // management of buffered sessions
192 void dormant(const char* key);
193 static void* cleanup_fn(void*);
195 CondWait* shutdown_wait;
196 Thread* cleanup_thread;
199 class StoredSession : public virtual Session
202 StoredSession(SSCache* cache, DDF& obj) : m_obj(obj),
206 m_cache(cache), m_expires(0), m_lastAccess(time(NULL)), m_lock(NULL) {
207 auto_ptr_XMLCh exp(m_obj["expires"].string());
209 DateTime iso(exp.get());
211 m_expires = iso.getEpoch();
215 const char* nameid = obj["nameid"].string();
217 // Parse and bind the document into an XMLObject.
218 istringstream instr(nameid);
219 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
220 XercesJanitor<DOMDocument> janitor(doc);
221 auto_ptr<saml2::NameID> n(saml2::NameIDBuilder::buildNameID());
222 n->unmarshall(doc->getDocumentElement(), true);
224 m_nameid = n.release();
228 m_lock = Mutex::create();
234 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
237 for_each(m_tokens.begin(), m_tokens.end(), cleanup_pair<string,Assertion>());
253 const char* getID() const {
256 const char* getApplicationID() const {
257 return m_obj["application_id"].string();
259 const char* getClientAddress() const {
260 return m_obj["client_addr"].string();
262 const char* getEntityID() const {
263 return m_obj["entity_id"].string();
265 const char* getProtocol() const {
266 return m_obj["protocol"].string();
268 const char* getAuthnInstant() const {
269 return m_obj["authn_instant"].string();
272 const saml2::NameID* getNameID() const {
276 const char* getSessionIndex() const {
277 return m_obj["session_index"].string();
279 const char* getAuthnContextClassRef() const {
280 return m_obj["authncontext_class"].string();
282 const char* getAuthnContextDeclRef() const {
283 return m_obj["authncontext_decl"].string();
285 const vector<Attribute*>& getAttributes() const {
286 if (m_attributes.empty())
287 unmarshallAttributes();
290 const multimap<string,const Attribute*>& getIndexedAttributes() const {
291 if (m_attributeIndex.empty()) {
292 if (m_attributes.empty())
293 unmarshallAttributes();
294 for (vector<Attribute*>::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) {
295 const vector<string>& aliases = (*a)->getAliases();
296 for (vector<string>::const_iterator alias = aliases.begin(); alias != aliases.end(); ++alias)
297 m_attributeIndex.insert(multimap<string,const Attribute*>::value_type(*alias, *a));
300 return m_attributeIndex;
302 const vector<const char*>& getAssertionIDs() const {
304 DDF ids = m_obj["assertions"];
305 DDF id = ids.first();
306 while (id.isstring()) {
307 m_ids.push_back(id.string());
314 void validate(const Application& application, const char* client_addr, time_t* timeout);
317 void addAttributes(const vector<Attribute*>& attributes);
318 const Assertion* getAssertion(const char* id) const;
319 void addAssertion(Assertion* assertion);
322 time_t getExpiration() const { return m_expires; }
323 time_t getLastAccess() const { return m_lastAccess; }
326 void unmarshallAttributes() const;
330 saml2::NameID* m_nameid;
331 mutable map<string,Assertion*> m_tokens;
333 mutable vector<Attribute*> m_attributes;
334 mutable multimap<string,const Attribute*> m_attributeIndex;
335 mutable vector<const char*> m_ids;
338 time_t m_expires,m_lastAccess;
342 SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)
344 return new SSCache(e);
348 Session* SessionCache::find(const Application& application, HTTPRequest& request, const char* client_addr, time_t* timeout)
350 return find(application, const_cast<const HTTPRequest&>(request), client_addr, timeout);
353 void SHIBSP_API shibsp::registerSessionCaches()
355 SPConfig::getConfig().SessionCacheManager.registerFactory(STORAGESERVICE_SESSION_CACHE, StorageServiceCacheFactory);
358 void StoredSession::unmarshallAttributes() const
360 Attribute* attribute;
361 DDF attrs = m_obj["attributes"];
362 DDF attr = attrs.first();
363 while (!attr.isnull()) {
365 attribute = Attribute::unmarshall(attr);
366 m_attributes.push_back(attribute);
367 if (m_cache->m_log.isDebugEnabled())
368 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",
369 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
371 catch (AttributeException& ex) {
372 const char* id = attr.first().name();
373 m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
379 void StoredSession::validate(const Application& application, const char* client_addr, time_t* timeout)
381 time_t now = time(NULL);
385 if (now > m_expires) {
386 m_cache->m_log.info("session expired (ID: %s)", getID());
387 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
393 if (m_cache->m_log.isDebugEnabled())
394 m_cache->m_log.debug("comparing client address %s against %s", client_addr, getClientAddress());
395 if (!XMLString::equals(getClientAddress(),client_addr)) {
396 m_cache->m_log.warn("client address mismatch");
397 throw RetryableProfileException(
398 "Your IP address ($1) does not match the address recorded at the time the session was established.",
399 params(1,client_addr)
407 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
408 DDF in("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
411 in.addmember("key").string(getID());
412 in.addmember("version").integer(m_obj["version"].integer());
414 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
415 #ifndef HAVE_GMTIME_R
416 struct tm* ptime=gmtime(timeout);
419 struct tm* ptime=gmtime_r(timeout,&res);
422 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
423 in.addmember("timeout").string(timebuf);
427 out=application.getServiceProvider().getListenerService()->send(in);
434 if (out.isstruct()) {
435 // We got an updated record back.
437 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
438 m_attributes.clear();
439 m_attributeIndex.clear();
446 if (!m_cache->m_storage)
447 throw ConfigurationException("Session touch requires a StorageService.");
449 // Do a versioned read.
452 int curver = m_obj["version"].integer();
453 int ver = m_cache->m_storage->readText(getID(), "session", &record, &lastAccess, curver);
455 m_cache->m_log.warn("unsuccessful versioned read of session (ID: %s), cache out of sync?", getID());
456 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
459 // Adjust for expiration to recover last access time and check timeout.
460 lastAccess -= m_cache->m_cacheTimeout;
461 if (*timeout > 0 && now - lastAccess >= *timeout) {
462 m_cache->m_log.info("session timed out (ID: %s)", getID());
463 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
466 // Update storage expiration, if possible.
468 m_cache->m_storage->updateContext(getID(), now + m_cache->m_cacheTimeout);
470 catch (exception& ex) {
471 m_cache->m_log.error("failed to update session expiration: %s", ex.what());
475 // We got an updated record back.
477 istringstream in(record);
480 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
481 m_attributes.clear();
482 m_attributeIndex.clear();
487 throw ConfigurationException("Session touch requires a StorageService.");
496 void StoredSession::addAttributes(const vector<Attribute*>& attributes)
499 xmltooling::NDC ndc("addAttributes");
502 if (!m_cache->m_storage)
503 throw ConfigurationException("Session modification requires a StorageService.");
505 m_cache->m_log.debug("adding attributes to session (%s)", getID());
510 DDF attrs = m_obj["attributes"];
512 attrs = m_obj.addmember("attributes").list();
513 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
514 attr = (*a)->marshall();
518 // Tentatively increment the version.
519 m_obj["version"].integer(m_obj["version"].integer()+1);
523 string record(str.str());
526 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
529 // Roll back modification to record.
530 m_obj["version"].integer(m_obj["version"].integer()-1);
531 vector<Attribute*>::size_type count = attributes.size();
533 attrs.last().destroy();
538 // Roll back modification to record.
539 m_obj["version"].integer(m_obj["version"].integer()-1);
540 vector<Attribute*>::size_type count = attributes.size();
542 attrs.last().destroy();
545 // Fatal problem with update.
546 throw IOException("Unable to update stored session.");
550 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
551 ver = m_cache->m_storage->readText(getID(), "session", &record, NULL);
553 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
554 throw IOException("Unable to read back stored session.");
559 istringstream in(record);
563 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
564 m_attributes.clear();
565 m_attributeIndex.clear();
566 newobj["version"].integer(ver);
572 } while (ver < 0); // negative indicates a sync issue so we retry
574 TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();
576 xlog->log.infoStream() <<
577 "Added the following attributes to session (ID: " <<
579 ") for (applicationId: " <<
580 m_obj["application_id"].string() <<
582 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a)
583 xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";
586 // We own them now, so clean them up.
587 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
590 const Assertion* StoredSession::getAssertion(const char* id) const
592 if (!m_cache->m_storage)
593 throw ConfigurationException("Assertion retrieval requires a StorageService.");
595 map<string,Assertion*>::const_iterator i = m_tokens.find(id);
596 if (i!=m_tokens.end())
600 if (!m_cache->m_storage->readText(getID(), id, &tokenstr, NULL))
601 throw FatalProfileException("Assertion not found in cache.");
603 // Parse and bind the document into an XMLObject.
604 istringstream instr(tokenstr);
605 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
606 XercesJanitor<DOMDocument> janitor(doc);
607 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
610 Assertion* token = dynamic_cast<Assertion*>(xmlObject.get());
612 throw FatalProfileException("Request for cached assertion returned an unknown object type.");
614 // Transfer ownership to us.
620 void StoredSession::addAssertion(Assertion* assertion)
623 xmltooling::NDC ndc("addAssertion");
626 if (!m_cache->m_storage)
627 throw ConfigurationException("Session modification requires a StorageService.");
630 throw FatalProfileException("Unknown object type passed to session for storage.");
632 auto_ptr_char id(assertion->getID());
634 m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), getID());
637 if (!m_cache->m_storage->readText(getID(), "session", NULL, &exp))
638 throw IOException("Unable to load expiration time for stored session.");
640 ostringstream tokenstr;
641 tokenstr << *assertion;
642 if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))
643 throw IOException("Attempted to insert duplicate assertion ID into session.");
647 DDF token = DDF(NULL).string(id.get());
648 m_obj["assertions"].add(token);
650 // Tentatively increment the version.
651 m_obj["version"].integer(m_obj["version"].integer()+1);
655 string record(str.str());
658 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
662 m_obj["version"].integer(m_obj["version"].integer()-1);
663 m_cache->m_storage->deleteText(getID(), id.get());
669 m_obj["version"].integer(m_obj["version"].integer()-1);
672 // Fatal problem with update.
673 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
674 m_cache->m_storage->deleteText(getID(), id.get());
675 throw IOException("Unable to update stored session.");
679 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
680 ver = m_cache->m_storage->readText(getID(), "session", &record, NULL);
682 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
683 m_cache->m_storage->deleteText(getID(), id.get());
684 throw IOException("Unable to read back stored session.");
689 istringstream in(record);
693 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
694 m_attributes.clear();
695 m_attributeIndex.clear();
696 newobj["version"].integer(ver);
702 } while (ver < 0); // negative indicates a sync issue so we retry
707 TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();
710 "Added assertion (ID: %s) to session for (applicationId: %s) with (ID: %s)",
711 id.get(), m_obj["application_id"].string(), getID()
717 SSCache::SSCache(const DOMElement* e)
718 : m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), inproc(true), m_cacheTimeout(28800),
720 m_storage(NULL), m_storage_lite(NULL),
722 m_root(e), m_inprocTimeout(900), m_lock(NULL), shutdown(false), shutdown_wait(NULL), cleanup_thread(NULL)
724 static const XMLCh cacheTimeout[] = UNICODE_LITERAL_12(c,a,c,h,e,T,i,m,e,o,u,t);
725 static const XMLCh inprocTimeout[] = UNICODE_LITERAL_13(i,n,p,r,o,c,T,i,m,e,o,u,t);
726 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
727 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);
729 SPConfig& conf = SPConfig::getConfig();
730 inproc = conf.isEnabled(SPConfig::InProcess);
733 const XMLCh* tag=e->getAttributeNS(NULL,cacheTimeout);
735 m_cacheTimeout = XMLString::parseInt(tag);
737 m_cacheTimeout=28800;
740 const XMLCh* tag=e->getAttributeNS(NULL,inprocTimeout);
742 m_inprocTimeout = XMLString::parseInt(tag);
743 if (!m_inprocTimeout)
750 if (conf.isEnabled(SPConfig::OutOfProcess)) {
751 const XMLCh* tag = e ? e->getAttributeNS(NULL,_StorageService) : NULL;
753 auto_ptr_char ssid(tag);
754 m_storage = conf.getServiceProvider()->getStorageService(ssid.get());
756 m_log.info("bound to StorageService (%s)", ssid.get());
759 throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");
761 tag = e ? e->getAttributeNS(NULL,_StorageServiceLite) : NULL;
763 auto_ptr_char ssid(tag);
764 m_storage_lite = conf.getServiceProvider()->getStorageService(ssid.get());
766 m_log.info("bound to StorageServiceLite (%s)", ssid.get());
768 if (!m_storage_lite) {
769 m_log.info("No StorageServiceLite specified. Using standard StorageService.");
770 m_storage_lite = m_storage;
775 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
777 if (!conf.isEnabled(SPConfig::OutOfProcess) && !listener)
778 throw ConfigurationException("SessionCache requires a ListenerService, but none available.");
779 m_lock = RWLock::create();
780 shutdown_wait = CondWait::create();
781 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
785 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
786 listener->regListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
787 listener->regListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
788 listener->regListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
791 m_log.info("no ListenerService available, cache remoting disabled");
800 // Shut down the cleanup thread and let it know...
802 shutdown_wait->signal();
803 cleanup_thread->join(NULL);
805 for_each(m_hashtable.begin(),m_hashtable.end(),cleanup_pair<string,StoredSession>());
807 delete shutdown_wait;
811 SPConfig& conf = SPConfig::getConfig();
812 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
813 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
814 listener->unregListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
815 listener->unregListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
816 listener->unregListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
826 auto_ptr_char temp(SAMLConfig::getConfig().generateIdentifier());
827 m_storage->createString("SessionCacheTest", temp.get(), "Test", time(NULL) + 60);
828 m_storage->deleteString("SessionCacheTest", temp.get());
831 void SSCache::insert(const char* key, time_t expires, const char* name, const char* index)
834 if (strlen(name) > 255) {
835 dup = string(name).substr(0,255);
840 DDFJanitor jobj(obj);
842 // Since we can't guarantee uniqueness, check for an existing record.
845 int ver = m_storage_lite->readText("NameID", name, &record, &recordexp);
847 // Existing record, so we need to unmarshall it.
848 istringstream in(record);
853 obj = DDF(NULL).structure();
856 if (!index || !*index)
858 DDF sessions = obj.addmember(index);
859 if (!sessions.islist())
861 DDF session = DDF(NULL).string(key);
862 sessions.add(session);
864 // Remarshall the record.
868 // Try and store it back...
870 ver = m_storage_lite->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);
872 // Out of sync, or went missing, so retry.
873 return insert(key, expires, name, index);
876 else if (!m_storage_lite->createText("NameID", name, out.str().c_str(), expires)) {
877 // Hit a dup, so just retry, hopefully hitting the other branch.
878 return insert(key, expires, name, index);
882 void SSCache::insert(
883 const Application& application,
884 const HTTPRequest& httpRequest,
885 HTTPResponse& httpResponse,
887 const saml2md::EntityDescriptor* issuer,
888 const XMLCh* protocol,
889 const saml2::NameID* nameid,
890 const XMLCh* authn_instant,
891 const XMLCh* session_index,
892 const XMLCh* authncontext_class,
893 const XMLCh* authncontext_decl,
894 const vector<const Assertion*>* tokens,
895 const vector<Attribute*>* attributes
899 xmltooling::NDC ndc("insert");
902 throw ConfigurationException("SessionCache insertion requires a StorageService.");
904 m_log.debug("creating new session");
906 time_t now = time(NULL);
907 auto_ptr_char index(session_index);
908 auto_ptr_char entity_id(issuer ? issuer->getEntityID() : NULL);
909 auto_ptr_char name(nameid ? nameid->getName() : NULL);
912 // Check for a pending logout.
913 if (strlen(name.get()) > 255)
914 const_cast<char*>(name.get())[255] = 0;
916 int ver = m_storage_lite->readText("Logout", name.get(), &pending);
919 DDFJanitor jpend(pendobj);
920 istringstream pstr(pending);
922 // IdP.SP.index contains logout expiration, if any.
923 DDF deadmenwalking = pendobj[issuer ? entity_id.get() : "_shibnull"][application.getRelyingParty(issuer)->getString("entityID").second];
924 const char* logexpstr = deadmenwalking[session_index ? index.get() : "_shibnull"].string();
925 if (!logexpstr && session_index) // we tried an exact session match, now try for NULL
926 logexpstr = deadmenwalking["_shibnull"].string();
928 auto_ptr_XMLCh dt(logexpstr);
929 DateTime dtobj(dt.get());
930 dtobj.parseDateTime();
931 time_t logexp = dtobj.getEpoch();
932 if (now - XMLToolingConfig::getConfig().clock_skew_secs < logexp)
933 throw FatalProfileException("A logout message from your identity provider has blocked your login attempt.");
938 auto_ptr_char key(SAMLConfig::getConfig().generateIdentifier());
940 // Store session properties in DDF.
941 DDF obj = DDF(key.get()).structure();
942 DDFJanitor entryobj(obj);
943 obj.addmember("version").integer(1);
944 obj.addmember("application_id").string(application.getId());
946 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
947 #ifndef HAVE_GMTIME_R
948 struct tm* ptime=gmtime(&expires);
951 struct tm* ptime=gmtime_r(&expires,&res);
954 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
955 obj.addmember("expires").string(timebuf);
957 obj.addmember("client_addr").string(httpRequest.getRemoteAddr().c_str());
959 obj.addmember("entity_id").string(entity_id.get());
961 auto_ptr_char prot(protocol);
962 obj.addmember("protocol").string(prot.get());
965 auto_ptr_char instant(authn_instant);
966 obj.addmember("authn_instant").string(instant.get());
969 obj.addmember("session_index").string(index.get());
970 if (authncontext_class) {
971 auto_ptr_char ac(authncontext_class);
972 obj.addmember("authncontext_class").string(ac.get());
974 if (authncontext_decl) {
975 auto_ptr_char ad(authncontext_decl);
976 obj.addmember("authncontext_decl").string(ad.get());
980 ostringstream namestr;
982 obj.addmember("nameid").string(namestr.str().c_str());
986 obj.addmember("assertions").list();
987 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
988 auto_ptr_char tokenid((*t)->getID());
989 DDF tokid = DDF(NULL).string(tokenid.get());
990 obj["assertions"].add(tokid);
996 DDF attrlist = obj.addmember("attributes").list();
997 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
998 attr = (*a)->marshall();
1003 ostringstream record;
1006 m_log.debug("storing new session...");
1007 if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout))
1008 throw FatalProfileException("Attempted to create a session with a duplicate key.");
1010 // Store the reverse mapping for logout.
1013 insert(key.get(), expires, name.get(), index.get());
1015 catch (exception& ex) {
1016 m_log.error("error storing back mapping of NameID for logout: %s", ex.what());
1021 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1022 ostringstream tokenstr;
1024 auto_ptr_char tokenid((*t)->getID());
1025 if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + m_cacheTimeout))
1026 throw IOException("duplicate assertion ID ($1)", params(1, tokenid.get()));
1029 catch (exception& ex) {
1030 m_log.error("error storing assertion along with session: %s", ex.what());
1034 const char* pid = obj["entity_id"].string();
1035 const char* prot = obj["protocol"].string();
1036 m_log.info("new session created: ID (%s) IdP (%s) Protocol(%s) Address (%s)",
1037 key.get(), pid ? pid : "none", prot ? prot : "none", httpRequest.getRemoteAddr().c_str());
1039 // Transaction Logging
1040 TransactionLog* xlog = application.getServiceProvider().getTransactionLog();
1041 Locker locker(xlog);
1042 xlog->log.infoStream() <<
1043 "New session (ID: " <<
1045 ") with (applicationId: " <<
1046 application.getId() <<
1047 ") for principal from (IdP: " <<
1048 (pid ? pid : "none") <<
1049 ") at (ClientAddress: " <<
1050 httpRequest.getRemoteAddr() <<
1051 ") with (NameIdentifier: " <<
1052 (nameid ? name.get() : "none") <<
1053 ") using (Protocol: " <<
1054 (prot ? prot : "none") <<
1055 ") from (AssertionID: " <<
1056 (tokens ? obj["assertions"].first().string() : "none") <<
1060 xlog->log.infoStream() <<
1061 "Cached the following attributes with session (ID: " <<
1063 ") for (applicationId: " <<
1064 application.getId() <<
1066 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a)
1067 xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";
1068 xlog->log.info("}");
1071 time_t cookieLifetime = 0;
1072 pair<string,const char*> shib_cookie = application.getCookieNameProps("_shibsession_", &cookieLifetime);
1073 string k(key.get());
1074 k += shib_cookie.second;
1076 if (cookieLifetime > 0) {
1077 cookieLifetime += now;
1078 #ifndef HAVE_GMTIME_R
1079 ptime=gmtime(&cookieLifetime);
1081 ptime=gmtime_r(&cookieLifetime,&res);
1083 char cookietimebuf[64];
1084 strftime(cookietimebuf,64,"; expires=%a, %d %b %Y %H:%M:%S GMT",ptime);
1088 httpResponse.setCookie(shib_cookie.first.c_str(), k.c_str());
1091 bool SSCache::matches(
1092 const Application& application,
1093 const xmltooling::HTTPRequest& request,
1094 const saml2md::EntityDescriptor* issuer,
1095 const saml2::NameID& nameid,
1096 const set<string>* indexes
1099 auto_ptr_char entityID(issuer ? issuer->getEntityID() : NULL);
1101 Session* session = find(application, request);
1103 Locker locker(session, false);
1104 if (XMLString::equals(session->getEntityID(), entityID.get()) && session->getNameID() &&
1105 stronglyMatches(issuer->getEntityID(), application.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1106 return (!indexes || indexes->empty() || (session->getSessionIndex() ? (indexes->count(session->getSessionIndex())>0) : false));
1110 catch (exception& ex) {
1111 m_log.error("error while matching session: %s", ex.what());
1116 vector<string>::size_type SSCache::logout(
1117 const Application& application,
1118 const saml2md::EntityDescriptor* issuer,
1119 const saml2::NameID& nameid,
1120 const set<string>* indexes,
1122 vector<string>& sessionsKilled
1126 xmltooling::NDC ndc("logout");
1130 throw ConfigurationException("SessionCache insertion requires a StorageService.");
1132 auto_ptr_char entityID(issuer ? issuer->getEntityID() : NULL);
1133 auto_ptr_char name(nameid.getName());
1135 m_log.info("request to logout sessions from (%s) for (%s)", entityID.get() ? entityID.get() : "unknown", name.get());
1137 if (strlen(name.get()) > 255)
1138 const_cast<char*>(name.get())[255] = 0;
1141 DDFJanitor jobj(obj);
1146 // Record the logout to prevent post-delivered assertions.
1147 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1148 #ifndef HAVE_GMTIME_R
1149 struct tm* ptime=gmtime(&expires);
1152 struct tm* ptime=gmtime_r(&expires,&res);
1155 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1158 ver = m_storage_lite->readText("Logout", name.get(), &record, &oldexp);
1160 istringstream lin(record);
1164 obj = DDF(NULL).structure();
1167 // Structure is keyed by the IdP and SP, with a member per session index containing the expiration.
1168 DDF root = obj.addmember(issuer ? entityID.get() : "_shibnull").addmember(application.getRelyingParty(issuer)->getString("entityID").second);
1170 for (set<string>::const_iterator x = indexes->begin(); x!=indexes->end(); ++x)
1171 root.addmember(x->c_str()).string(timebuf);
1174 root.addmember("_shibnull").string(timebuf);
1182 ver = m_storage_lite->updateText("Logout", name.get(), lout.str().c_str(), max(expires, oldexp), ver);
1184 // Out of sync, or went missing, so retry.
1185 return logout(application, issuer, nameid, indexes, expires, sessionsKilled);
1188 else if (!m_storage_lite->createText("Logout", name.get(), lout.str().c_str(), expires)) {
1189 // Hit a dup, so just retry, hopefully hitting the other branch.
1190 return logout(application, issuer, nameid, indexes, expires, sessionsKilled);
1197 // Read in potentially matching sessions.
1198 ver = m_storage_lite->readText("NameID", name.get(), &record);
1200 m_log.debug("no active sessions to logout for supplied issuer and subject");
1204 istringstream in(record);
1207 // The record contains child lists for each known session index.
1209 DDF sessions = obj.first();
1210 while (sessions.islist()) {
1211 if (!indexes || indexes->empty() || indexes->count(sessions.name())) {
1212 key = sessions.first();
1213 while (key.isstring()) {
1214 // Fetch the session for comparison.
1215 Session* session = NULL;
1217 session = find(application, key.string());
1219 catch (exception& ex) {
1220 m_log.error("error locating session (%s): %s", key.string(), ex.what());
1224 Locker locker(session, false);
1226 if (XMLString::equals(session->getEntityID(), entityID.get())) {
1228 if (stronglyMatches(issuer->getEntityID(), application.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1229 sessionsKilled.push_back(key.string());
1233 m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());
1237 m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());
1241 // Session's gone, so...
1242 sessionsKilled.push_back(key.string());
1245 key = sessions.next();
1248 // No sessions left for this index?
1249 if (sessions.first().isnull())
1252 sessions = obj.next();
1255 if (obj.first().isnull())
1258 // If possible, write back the mapping record (this isn't crucial).
1261 m_storage_lite->deleteText("NameID", name.get());
1263 else if (!sessionsKilled.empty()) {
1266 if (m_storage_lite->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)
1267 m_log.warn("logout mapping record changed behind us, leaving it alone");
1270 catch (exception& ex) {
1271 m_log.error("error updating logout mapping record: %s", ex.what());
1274 return sessionsKilled.size();
1277 bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
1279 if (!XMLString::equals(n1.getName(), n2.getName()))
1282 const XMLCh* s1 = n1.getFormat();
1283 const XMLCh* s2 = n2.getFormat();
1285 s1 = saml2::NameID::UNSPECIFIED;
1287 s2 = saml2::NameID::UNSPECIFIED;
1288 if (!XMLString::equals(s1,s2))
1291 s1 = n1.getNameQualifier();
1292 s2 = n2.getNameQualifier();
1297 if (!XMLString::equals(s1,s2))
1300 s1 = n1.getSPNameQualifier();
1301 s2 = n2.getSPNameQualifier();
1306 if (!XMLString::equals(s1,s2))
1314 Session* SSCache::find(const Application& application, const char* key, const char* client_addr, time_t* timeout)
1317 xmltooling::NDC ndc("find");
1319 StoredSession* session=NULL;
1322 m_log.debug("searching local cache for session (%s)", key);
1324 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1325 if (i!=m_hashtable.end()) {
1326 // Save off and lock the session.
1327 session = i->second;
1330 m_log.debug("session found locally, validating it for use");
1338 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1339 m_log.debug("session not found locally, remoting the search");
1340 // Remote the request.
1341 DDF in("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
1344 in.addmember("key").string(key);
1345 in.addmember("application_id").string(application.getId());
1346 if (timeout && *timeout) {
1347 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1348 #ifndef HAVE_GMTIME_R
1349 struct tm* ptime=gmtime(timeout);
1352 struct tm* ptime=gmtime_r(timeout,&res);
1355 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1356 in.addmember("timeout").string(timebuf);
1360 out=application.getServiceProvider().getListenerService()->send(in);
1361 if (!out.isstruct()) {
1363 m_log.debug("session not found in remote cache");
1367 // Wrap the results in a local entry and save it.
1368 session = new StoredSession(this, out);
1369 // The remote end has handled timeout issues, we handle address and expiration checks.
1378 // We're out of process, so we can search the storage service directly.
1381 throw ConfigurationException("SessionCache lookup requires a StorageService.");
1383 m_log.debug("searching for session (%s)", key);
1388 int ver = m_storage->readText(key, "session", &record, &lastAccess);
1392 m_log.debug("reconstituting session and checking validity");
1394 istringstream in(record);
1397 lastAccess -= m_cacheTimeout; // adjusts it back to the last time the record's timestamp was touched
1398 time_t now=time(NULL);
1400 if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {
1401 m_log.info("session timed out (ID: %s)", key);
1402 remove(application, key);
1403 const char* eid = obj["entity_id"].string();
1406 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1410 throw RetryableProfileException("Your session has expired, and you must re-authenticate.", namedparams(1, "entityID", eid2.c_str()));
1414 // Update storage expiration, if possible.
1416 m_storage->updateContext(key, now + m_cacheTimeout);
1418 catch (exception& ex) {
1419 m_log.error("failed to update session expiration: %s", ex.what());
1423 // Wrap the results in a local entry and save it.
1424 session = new StoredSession(this, obj);
1425 // We handled timeout issues, still need to handle address and expiration checks.
1428 throw ConfigurationException("SessionCache search requires a StorageService.");
1433 // Lock for writing and repeat the search to avoid duplication.
1435 SharedLock shared(m_lock, false);
1436 if (m_hashtable.count(key)) {
1437 // We're using an existing session entry.
1439 session = m_hashtable[key];
1443 m_hashtable[key]=session;
1449 if (!XMLString::equals(session->getApplicationID(), application.getId())) {
1450 m_log.error("an application (%s) tried to access another application's session", application.getId());
1455 // Verify currency and update the timestamp if indicated by caller.
1457 session->validate(application, client_addr, timeout);
1461 remove(application, key);
1468 void SSCache::remove(const Application& application, const char* key)
1471 xmltooling::NDC ndc("remove");
1473 // Take care of local copy.
1477 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1478 // Remove the session from storage directly.
1480 m_storage->deleteContext(key);
1481 m_log.info("removed session (%s)", key);
1483 TransactionLog* xlog = application.getServiceProvider().getTransactionLog();
1484 Locker locker(xlog);
1485 xlog->log.info("Destroyed session (applicationId: %s) (ID: %s)", application.getId(), key);
1487 throw ConfigurationException("SessionCache removal requires a StorageService.");
1491 // Remote the request.
1492 DDF in("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache");
1495 in.addmember("key").string(key);
1496 in.addmember("application_id").string(application.getId());
1498 DDF out = application.getServiceProvider().getListenerService()->send(in);
1503 void SSCache::dormant(const char* key)
1506 xmltooling::NDC ndc("dormant");
1509 m_log.debug("deleting local copy of session (%s)", key);
1511 // lock the cache for writing, which means we know nobody is sitting in find()
1514 // grab the entry from the table
1515 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1516 if (i==m_hashtable.end()) {
1521 // ok, remove the entry and lock it
1522 StoredSession* entry=i->second;
1523 m_hashtable.erase(key);
1529 // we can release the cache entry lock because we know we're not in the cache anymore
1535 void SSCache::cleanup()
1538 xmltooling::NDC ndc("cleanup");
1541 Mutex* mutex = Mutex::create();
1543 // Load our configuration details...
1544 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
1545 const XMLCh* tag=m_root ? m_root->getAttributeNS(NULL,cleanupInterval) : NULL;
1546 int rerun_timer = 900;
1548 rerun_timer = XMLString::parseInt(tag);
1549 if (rerun_timer <= 0)
1555 m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, m_inprocTimeout);
1558 shutdown_wait->timedwait(mutex,rerun_timer);
1562 // Ok, let's run through the cleanup process and clean out
1563 // really old sessions. This is a two-pass process. The
1564 // first pass is done holding a read-lock while we iterate over
1565 // the cache. The second pass doesn't need a lock because
1566 // the 'deletes' will lock the cache.
1568 // Pass 1: iterate over the map and find all entries that have not been
1569 // used in the allotted timeout.
1570 vector<string> stale_keys;
1571 time_t stale = time(NULL) - m_inprocTimeout;
1573 m_log.debug("cleanup thread running");
1576 for (map<string,StoredSession*>::const_iterator i=m_hashtable.begin(); i!=m_hashtable.end(); ++i) {
1577 // If the last access was BEFORE the stale timeout...
1579 time_t last=i->second->getLastAccess();
1580 i->second->unlock();
1582 stale_keys.push_back(i->first);
1586 if (!stale_keys.empty()) {
1587 m_log.info("purging %d old sessions", stale_keys.size());
1589 // Pass 2: walk through the list of stale entries and remove them from the cache
1590 for (vector<string>::const_iterator j = stale_keys.begin(); j != stale_keys.end(); ++j)
1591 dormant(j->c_str());
1594 m_log.debug("cleanup thread completed");
1597 m_log.info("cleanup thread exiting");
1604 void* SSCache::cleanup_fn(void* cache_p)
1607 // First, let's block all signals
1608 Thread::mask_all_signals();
1611 // Now run the cleanup process.
1612 reinterpret_cast<SSCache*>(cache_p)->cleanup();
1618 void SSCache::receive(DDF& in, ostream& out)
1621 xmltooling::NDC ndc("receive");
1624 if (!strcmp(in.name(),"find::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1625 const char* key=in["key"].string();
1627 throw ListenerException("Required parameters missing for session lookup.");
1629 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1631 throw ListenerException("Application not found, check configuration?");
1633 // Do an unversioned read.
1636 if (!m_storage->readText(key, "session", &record, &lastAccess)) {
1638 DDFJanitor jan(ret);
1643 // Adjust for expiration to recover last access time and check timeout.
1644 lastAccess -= m_cacheTimeout;
1645 time_t now=time(NULL);
1647 // See if we need to check for a timeout.
1648 if (in["timeout"].string()) {
1650 auto_ptr_XMLCh dt(in["timeout"].string());
1651 DateTime dtobj(dt.get());
1652 dtobj.parseDateTime();
1653 timeout = dtobj.getEpoch();
1655 if (timeout > 0 && now - lastAccess >= timeout) {
1656 m_log.info("session timed out (ID: %s)", key);
1658 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1661 // Update storage expiration, if possible.
1663 m_storage->updateContext(key, now + m_cacheTimeout);
1665 catch (exception& ex) {
1666 m_log.error("failed to update session expiration: %s", ex.what());
1670 // Send the record back.
1673 else if (!strcmp(in.name(),"touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1674 const char* key=in["key"].string();
1676 throw ListenerException("Required parameters missing for session check.");
1678 // Do a versioned read.
1681 int curver = in["version"].integer();
1682 int ver = m_storage->readText(key, "session", &record, &lastAccess, curver);
1684 m_log.warn("unsuccessful versioned read of session (ID: %s), caches out of sync?", key);
1685 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1688 // Adjust for expiration to recover last access time and check timeout.
1689 lastAccess -= m_cacheTimeout;
1690 time_t now=time(NULL);
1692 // See if we need to check for a timeout.
1694 auto_ptr_XMLCh dt(in["timeout"].string());
1696 DateTime dtobj(dt.get());
1697 dtobj.parseDateTime();
1698 timeout = dtobj.getEpoch();
1701 if (timeout > 0 && now - lastAccess >= timeout) {
1702 m_log.info("session timed out (ID: %s)", key);
1703 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1706 // Update storage expiration, if possible.
1708 m_storage->updateContext(key, now + m_cacheTimeout);
1710 catch (exception& ex) {
1711 m_log.error("failed to update session expiration: %s", ex.what());
1715 // Send the record back.
1720 DDFJanitor jan(ret);
1724 else if (!strcmp(in.name(),"remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1725 const char* key=in["key"].string();
1727 throw ListenerException("Required parameter missing for session removal.");
1729 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1731 throw ConfigurationException("Application not found, check configuration?");
1735 DDFJanitor jan(ret);