2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * RemotedSessionCache.cpp
\r
20 * SessionCache implementation that delegates to a remoted version.
\r
23 #include "internal.h"
\r
24 #include "Application.h"
\r
25 #include "exceptions.h"
\r
26 #include "ServiceProvider.h"
\r
27 #include "SessionCache.h"
\r
28 #include "TransactionLog.h"
\r
29 #include "attribute/Attribute.h"
\r
30 #include "remoting/ListenerService.h"
\r
31 #include "util/SPConstants.h"
\r
34 #include <log4cpp/Category.hh>
\r
35 #include <xmltooling/XMLToolingConfig.h>
\r
36 #include <xmltooling/util/NDC.h>
\r
37 #include <xmltooling/util/XMLHelper.h>
\r
39 using namespace shibsp;
\r
40 using namespace opensaml::saml2md;
\r
41 using namespace opensaml;
\r
42 using namespace xmltooling;
\r
43 using namespace log4cpp;
\r
44 using namespace std;
\r
49 class RemotedSession : public virtual Session
\r
52 RemotedSession(RemotedCache* cache, const Application& app, DDF& obj)
\r
53 : m_appId(app.getId()), m_version(obj["version"].integer()), m_obj(obj),
\r
54 m_nameid(NULL), m_expires(0), m_lastAccess(time(NULL)), m_cache(cache), m_lock(NULL) {
\r
55 const char* nameid = obj["nameid"].string();
\r
57 throw FatalProfileException("NameID missing from remotely cached session.");
\r
59 // Parse and bind the document into an XMLObject.
\r
60 istringstream instr(nameid);
\r
61 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
\r
62 XercesJanitor<DOMDocument> janitor(doc);
\r
63 auto_ptr<saml2::NameID> n(saml2::NameIDBuilder::buildNameID());
\r
64 n->unmarshall(doc->getDocumentElement(), true);
\r
67 // TODO: Process attributes...
\r
69 auto_ptr_XMLCh exp(m_obj["expires"].string());
\r
71 DateTime iso(exp.get());
\r
72 iso.parseDateTime();
\r
73 m_expires = iso.getEpoch();
\r
76 m_nameid = n.release();
\r
77 m_lock = Mutex::create();
\r
84 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
\r
85 for_each(m_tokens.begin(), m_tokens.end(), xmltooling::cleanup_pair<string,RootObject>());
\r
96 const char* getClientAddress() const {
\r
97 return m_obj["client_addr"].string();
\r
99 const char* getEntityID() const {
\r
100 return m_obj["entity_id"].string();
\r
102 const char* getAuthnInstant() const {
\r
103 return m_obj["authn_instant"].string();
\r
105 const opensaml::saml2::NameID& getNameID() const {
\r
108 const char* getSessionIndex() const {
\r
109 return m_obj["session_index"].string();
\r
111 const char* getAuthnContextClassRef() const {
\r
112 return m_obj["authncontext_class"].string();
\r
114 const char* getAuthnContextDeclRef() const {
\r
115 return m_obj["authncontext_decl"].string();
\r
117 const vector<const Attribute*>& getAttributes() const {
\r
118 return m_attributes;
\r
120 const vector<const char*>& getAssertionIDs() const {
\r
121 if (m_ids.empty()) {
\r
122 DDF id = m_obj["assertions"].first();
\r
123 while (id.isstring()) {
\r
124 m_ids.push_back(id.name());
\r
131 const RootObject* getAssertion(const char* id) const;
\r
133 void addAttributes(const vector<Attribute*>& attributes) {
\r
134 throw ConfigurationException("addAttributes method not implemented by this session cache plugin.");
\r
136 void addAssertion(RootObject* assertion) {
\r
137 throw ConfigurationException("addAttributes method not implemented by this session cache plugin.");
\r
140 time_t expires() const { return m_expires; }
\r
141 time_t lastAccess() const { return m_lastAccess; }
\r
142 void validate(const Application& application, const char* client_addr, time_t timeout, bool local=true);
\r
148 saml2::NameID* m_nameid;
\r
149 vector<const Attribute*> m_attributes;
\r
150 mutable vector<const char*> m_ids;
\r
151 mutable map<string,RootObject*> m_tokens;
\r
152 time_t m_expires,m_lastAccess;
\r
153 RemotedCache* m_cache;
\r
157 class RemotedCache : public SessionCache
\r
160 RemotedCache(const DOMElement* e);
\r
165 const Application& application,
\r
166 const char* client_addr,
\r
167 const saml2md::EntityDescriptor* issuer,
\r
168 const saml2::NameID& nameid,
\r
169 const char* authn_instant=NULL,
\r
170 const char* session_index=NULL,
\r
171 const char* authncontext_class=NULL,
\r
172 const char* authncontext_decl=NULL,
\r
173 const RootObject* ssoToken=NULL,
\r
174 const vector<Attribute*>* attributes=NULL
\r
176 Session* find(const char* key, const Application& application, const char* client_addr=NULL, time_t timeout=0);
\r
177 void remove(const char* key, const Application& application, const char* client_addr);
\r
183 const DOMElement* m_root; // Only valid during initialization
\r
185 map<string,RemotedSession*> m_hashtable;
\r
187 void dormant(const char* key);
\r
188 static void* cleanup_fn(void*);
\r
190 CondWait* shutdown_wait;
\r
191 Thread* cleanup_thread;
\r
194 SessionCache* SHIBSP_DLLLOCAL RemotedCacheFactory(const DOMElement* const & e)
\r
196 return new RemotedCache(e);
\r
200 const RootObject* RemotedSession::getAssertion(const char* id) const
\r
202 map<string,RootObject*>::const_iterator i = m_tokens.find(id);
\r
203 if (i!=m_tokens.end())
\r
206 const char* tokenstr = m_obj["assertions"][id].string();
\r
210 // Parse and bind the document into an XMLObject.
\r
211 istringstream instr(tokenstr);
\r
212 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
\r
213 XercesJanitor<DOMDocument> janitor(doc);
\r
214 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
\r
217 RootObject* token = dynamic_cast<RootObject*>(xmlObject.get());
\r
218 if (!token || !token->isAssertion())
\r
219 throw FatalProfileException("Cached assertion was of an unknown object type.");
\r
221 // Transfer ownership to us.
\r
222 xmlObject.release();
\r
223 m_tokens[id]=token;
\r
227 void RemotedSession::validate(const Application& application, const char* client_addr, time_t timeout, bool local)
\r
229 // Basic expiration?
\r
230 time_t now = time(NULL);
\r
231 if (now > m_expires) {
\r
232 m_cache->m_log.info("session expired (ID: %s)", m_obj.name());
\r
233 RetryableProfileException ex("Your session has expired, and you must re-authenticate.");
\r
234 if (!getEntityID())
\r
236 MetadataProvider* m=application.getMetadataProvider();
\r
238 annotateException(&ex,m->getEntityDescriptor(getEntityID(),false)); // throws it
\r
243 if (m_cache->m_log.isDebugEnabled())
\r
244 m_cache->m_log.debug("comparing client address %s against %s", client_addr, getClientAddress());
\r
245 if (strcmp(getClientAddress(),client_addr)) {
\r
246 m_cache->m_log.warn("client address mismatch");
\r
247 RetryableProfileException ex(
\r
248 "Your IP address ($1) does not match the address recorded at the time the session was established.",
\r
249 params(1,client_addr)
\r
251 if (!getEntityID())
\r
253 MetadataProvider* m=application.getMetadataProvider();
\r
255 annotateException(&ex,m->getEntityDescriptor(getEntityID(),false)); // throws it
\r
262 DDF in("touch::"REMOTED_SESSION_CACHE), out;
\r
263 DDFJanitor jin(in);
\r
265 in.addmember("key").string(m_obj.name());
\r
266 in.addmember("application_id").string(m_appId.c_str());
\r
267 in.addmember("version").integer(m_obj["version"].integer());
\r
269 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
\r
270 #ifndef HAVE_GMTIME_R
\r
271 struct tm* ptime=gmtime(&timeout);
\r
274 struct tm* ptime=gmtime_r(&timeout,&res);
\r
277 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
\r
278 in.addmember("timeout").string(timebuf);
\r
282 out=SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
283 if (out.isstruct()) {
\r
284 // We got an updated record back.
\r
286 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
\r
287 m_attributes.clear();
\r
291 // TODO: handle attributes
\r
299 m_lastAccess = now;
\r
302 RemotedCache::RemotedCache(const DOMElement* e)
\r
303 : SessionCache(e), m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), m_root(e), m_lock(NULL), shutdown(false)
\r
305 if (!SPConfig::getConfig().getServiceProvider()->getListenerService())
\r
306 throw ConfigurationException("RemotedCacheService requires a ListenerService, but none available.");
\r
308 m_lock = RWLock::create();
\r
309 shutdown_wait = CondWait::create();
\r
310 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
\r
313 RemotedCache::~RemotedCache()
\r
315 // Shut down the cleanup thread and let it know...
\r
317 shutdown_wait->signal();
\r
318 cleanup_thread->join(NULL);
\r
320 for_each(m_hashtable.begin(),m_hashtable.end(),xmltooling::cleanup_pair<string,RemotedSession>());
\r
322 delete shutdown_wait;
\r
325 string RemotedCache::insert(
\r
327 const Application& application,
\r
328 const char* client_addr,
\r
329 const saml2md::EntityDescriptor* issuer,
\r
330 const saml2::NameID& nameid,
\r
331 const char* authn_instant,
\r
332 const char* session_index,
\r
333 const char* authncontext_class,
\r
334 const char* authncontext_decl,
\r
335 const RootObject* ssoToken,
\r
336 const vector<Attribute*>* attributes
\r
339 DDF in("insert::"REMOTED_SESSION_CACHE);
\r
340 DDFJanitor jin(in);
\r
343 #ifndef HAVE_GMTIME_R
\r
344 struct tm* ptime=gmtime(&expires);
\r
347 struct tm* ptime=gmtime_r(&expires,&res);
\r
350 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
\r
351 in.addmember("expires").string(timebuf);
\r
353 in.addmember("application_id").string(application.getId());
\r
354 in.addmember("client_addr").string(client_addr);
\r
356 auto_ptr_char provid(issuer->getEntityID());
\r
357 in.addmember("entity_id").string(provid.get());
\r
360 in.addmember("authn_instant").string(authn_instant);
\r
362 in.addmember("session_index").string(session_index);
\r
363 if (authncontext_class)
\r
364 in.addmember("authncontext_class").string(authncontext_class);
\r
365 if (authncontext_decl)
\r
366 in.addmember("authncontext_decl").string(authncontext_decl);
\r
368 ostringstream namestr;
\r
370 in.addmember("nameid").string(namestr.str().c_str());
\r
373 ostringstream tokenstr;
\r
374 tokenstr << *ssoToken;
\r
375 auto_ptr_char tokenid(ssoToken->getID());
\r
376 in.addmember("assertions").list().add(DDF(tokenid.get()).string(tokenstr.str().c_str()));
\r
381 DDF attrs = in.addmember("attributes").list();
\r
382 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
\r
383 attr = (*a)->marshall();
\r
388 DDF out=SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
389 DDFJanitor jout(out);
\r
390 if (out["key"].isstring()) {
\r
391 for_each(attributes->begin(), attributes->end(), xmltooling::cleanup<Attribute>());
\r
393 // Transaction Logging
\r
394 auto_ptr_char name(nameid.getName());
\r
395 const char* pid = in["entity_id"].string();
\r
396 TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();
\r
397 Locker locker(xlog);
\r
398 xlog->log.infoStream() <<
\r
399 "New session (ID: " <<
\r
400 out["key"].string() <<
\r
401 ") with (applicationId: " <<
\r
402 application.getId() <<
\r
403 ") for principal from (IdP: " <<
\r
404 (pid ? pid : "none") <<
\r
405 ") at (ClientAddress: " <<
\r
407 ") with (NameIdentifier: " <<
\r
411 return out["key"].string();
\r
413 throw RetryableProfileException("A remoted cache insertion operation did not return a usable session key.");
\r
416 Session* RemotedCache::find(const char* key, const Application& application, const char* client_addr, time_t timeout)
\r
419 xmltooling::NDC ndc("find");
\r
422 bool localValidation = false;
\r
423 RemotedSession* session=NULL;
\r
424 m_log.debug("searching local cache for session (%s)", key);
\r
426 map<string,RemotedSession*>::const_iterator i=m_hashtable.find(key);
\r
427 if (i==m_hashtable.end()) {
\r
429 m_log.debug("session not found locally, searching remote cache");
\r
431 DDF in("find::"REMOTED_SESSION_CACHE), out;
\r
432 DDFJanitor jin(in);
\r
434 in.addmember("key").string(key);
\r
435 in.addmember("application_id").string(application.getId());
\r
437 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
\r
438 #ifndef HAVE_GMTIME_R
\r
439 struct tm* ptime=gmtime(&timeout);
\r
442 struct tm* ptime=gmtime_r(&timeout,&res);
\r
445 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
\r
446 in.addmember("timeout").string(timebuf);
\r
450 out=SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
451 if (!out.isstruct()) {
\r
453 m_log.debug("session not found in remote cache");
\r
457 // Wrap the results in a local entry and save it.
\r
458 session = new RemotedSession(this, application, out);
\r
459 // The remote end has handled timeout issues, we handle address and expiration checks.
\r
460 localValidation = true;
\r
467 // Lock for writing and repeat the search to avoid duplication.
\r
469 SharedLock shared(m_lock, false);
\r
470 if (m_hashtable.count(key)) {
\r
472 // We're using an existing session entry, so we have to switch back to full validation.
\r
473 localValidation = false;
\r
474 session = m_hashtable[key];
\r
478 m_hashtable[key]=session;
\r
483 // Save off and lock the session.
\r
484 session = i->second;
\r
488 m_log.debug("session found locally, validating it for use");
\r
491 // Verify currency and update the timestamp. If the local switch is false, we also update the access time.
\r
493 session->validate(application, client_addr, timeout, localValidation);
\r
503 void RemotedCache::remove(const char* key, const Application& application, const char* client_addr)
\r
505 // Take care of local copy.
\r
509 DDF in("remove::"REMOTED_SESSION_CACHE);
\r
510 DDFJanitor jin(in);
\r
512 in.addmember("key").string(key);
\r
513 in.addmember("application_id").string(application.getId());
\r
514 in.addmember("client_addr").string(client_addr);
\r
516 DDF out = SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
520 void RemotedCache::dormant(const char* key)
\r
523 xmltooling::NDC ndc("dormant");
\r
526 m_log.debug("deleting local copy of session (%s)", key);
\r
528 // lock the cache for writing, which means we know nobody is sitting in find()
\r
531 // grab the entry from the table
\r
532 map<string,RemotedSession*>::const_iterator i=m_hashtable.find(key);
\r
533 if (i==m_hashtable.end()) {
\r
538 // ok, remove the entry and lock it
\r
539 RemotedSession* entry=i->second;
\r
540 m_hashtable.erase(key);
\r
543 // unlock the cache
\r
546 // we can release the cache entry lock because we know we're not in the cache anymore
\r
552 void RemotedCache::cleanup()
\r
555 xmltooling::NDC ndc("cleanup");
\r
558 int rerun_timer = 0;
\r
559 Mutex* mutex = Mutex::create();
\r
561 // Load our configuration details...
\r
562 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
\r
563 const XMLCh* tag=m_root->getAttributeNS(NULL,cleanupInterval);
\r
565 rerun_timer = XMLString::parseInt(tag);
\r
567 if (rerun_timer <= 0)
\r
568 rerun_timer = 300; // rerun every 5 minutes
\r
572 m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, m_cacheTimeout);
\r
574 while (!shutdown) {
\r
575 shutdown_wait->timedwait(mutex,rerun_timer);
\r
579 // Ok, let's run through the cleanup process and clean out
\r
580 // really old sessions. This is a two-pass process. The
\r
581 // first pass is done holding a read-lock while we iterate over
\r
582 // the cache. The second pass doesn't need a lock because
\r
583 // the 'deletes' will lock the cache.
\r
585 // Pass 1: iterate over the map and find all entries that have not been
\r
587 vector<string> stale_keys;
\r
588 time_t stale = time(NULL) - m_cacheTimeout;
\r
591 for (map<string,RemotedSession*>::const_iterator i=m_hashtable.begin(); i!=m_hashtable.end(); ++i) {
\r
592 // If the last access was BEFORE the stale timeout...
\r
594 time_t last=i->second->lastAccess();
\r
595 i->second->unlock();
\r
597 stale_keys.push_back(i->first);
\r
601 if (!stale_keys.empty()) {
\r
602 m_log.info("purging %d old sessions", stale_keys.size());
\r
604 // Pass 2: walk through the list of stale entries and remove them from the cache
\r
605 for (vector<string>::const_iterator j = stale_keys.begin(); j != stale_keys.end(); ++j)
\r
606 dormant(j->c_str());
\r
610 m_log.info("cleanup thread exiting");
\r
614 Thread::exit(NULL);
\r
617 void* RemotedCache::cleanup_fn(void* cache_p)
\r
619 RemotedCache* cache = reinterpret_cast<RemotedCache*>(cache_p);
\r
622 // First, let's block all signals
\r
623 Thread::mask_all_signals();
\r
626 // Now run the cleanup process.
\r
631 /* These are currently unimplemented.
\r
633 void RemotedSession::addAttributes(const vector<Attribute*>& attributes)
\r
635 DDF in("addAttributes::"REMOTED_SESSION_CACHE);
\r
636 DDFJanitor jin(in);
\r
638 in.addmember("key").string(m_key.c_str());
\r
639 in.addmember("application_id").string(m_appId.c_str());
\r
642 DDF attrs = in.addmember("attributes").list();
\r
643 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
\r
644 attr = (*a)->marshall();
\r
648 attr=SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
649 DDFJanitor jout(attr);
\r
651 // Transfer ownership to us.
\r
652 m_attributes.insert(m_attributes.end(), attributes.begin(), attributes.end());
\r
655 void RemotedSession::addAssertion(RootObject* assertion)
\r
657 if (!assertion || !assertion->isAssertion())
\r
658 throw FatalProfileException("Unknown object type passed to session cache for storage.");
\r
660 DDF in("addAssertion::"REMOTED_SESSION_CACHE);
\r
661 DDFJanitor jin(in);
\r
663 in.addmember("key").string(m_key.c_str());
\r
664 in.addmember("application_id").string(m_appId.c_str());
\r
668 string token(os.str());
\r
669 auto_ptr_char tokenid(assertion->getID());
\r
670 in.addmember("assertion_id").string(tokenid.get());
\r
671 in.addmember("assertion").string(token.c_str());
\r
673 DDF out = SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
\r
676 // Add to local record and token map.
\r
677 // Next attempt to find and lock session will refresh from remote store anyway.
\r
678 m_obj["assertions"].addmember(tokenid.get()).string(token.c_str());
\r
680 m_tokens[tokenid.get()] = assertion;
\r