Attribute lookup, port ACL code and mainline SP code to Session/Attribute API.
[shibboleth/sp.git] / shibsp / impl / StorageServiceSessionCache.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * StorageServiceSessionCache.cpp\r
19  * \r
20  * StorageService-based SessionCache implementation.\r
21  * \r
22  * Instead of optimizing this plugin with a buffering scheme that keeps objects around\r
23  * and avoids extra parsing steps, I'm assuming that systems that require such can\r
24  * layer their own cache plugin on top of this version either by delegating to it\r
25  * or using the remoting support. So this version will load sessions directly\r
26  * from the StorageService, instantiate enough to expose the Session API,\r
27  * and then delete everything when they're unlocked. All data in memory is always\r
28  * kept in sync with the StorageService (no lazy updates).\r
29  */\r
30 \r
31 #include "internal.h"\r
32 #include "Application.h"\r
33 #include "exceptions.h"\r
34 #include "ServiceProvider.h"\r
35 #include "SessionCache.h"\r
36 #include "TransactionLog.h"\r
37 #include "attribute/Attribute.h"\r
38 #include "remoting/ListenerService.h"\r
39 #include "util/SPConstants.h"\r
40 \r
41 #include <log4cpp/Category.hh>\r
42 #include <saml/SAMLConfig.h>\r
43 #include <xmltooling/util/NDC.h>\r
44 #include <xmltooling/util/StorageService.h>\r
45 #include <xmltooling/util/XMLHelper.h>\r
46 #include <xercesc/util/XMLUniDefs.hpp>\r
47 \r
48 using namespace shibsp;\r
49 using namespace opensaml::saml2md;\r
50 using namespace opensaml;\r
51 using namespace xmltooling;\r
52 using namespace log4cpp;\r
53 using namespace std;\r
54 \r
55 namespace shibsp {\r
56 \r
57     class SSCache;\r
58     class StoredSession : public virtual Session\r
59     {\r
60     public:\r
61         StoredSession(SSCache* cache, DDF& obj) : m_obj(obj), m_cache(cache) {\r
62             const char* nameid = obj["nameid"].string();\r
63             if (!nameid)\r
64                 throw FatalProfileException("NameID missing from cached session.");\r
65             \r
66             // Parse and bind the document into an XMLObject.\r
67             istringstream instr(nameid);\r
68             DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr); \r
69             XercesJanitor<DOMDocument> janitor(doc);\r
70             auto_ptr<saml2::NameID> n(saml2::NameIDBuilder::buildNameID());\r
71             n->unmarshall(doc->getDocumentElement(), true);\r
72             janitor.release();\r
73             m_nameid = n.release();\r
74         }\r
75         \r
76         ~StoredSession();\r
77         \r
78         Lockable* lock() {\r
79             return this;\r
80         }\r
81         void unlock() {\r
82             delete this;\r
83         }\r
84         \r
85         const char* getClientAddress() const {\r
86             return m_obj["client_addr"].string();\r
87         }\r
88         const char* getEntityID() const {\r
89             return m_obj["entity_id"].string();\r
90         }\r
91         const char* getAuthnInstant() const {\r
92             return m_obj["authn_instant"].string();\r
93         }\r
94         const opensaml::saml2::NameID& getNameID() const {\r
95             return *m_nameid;\r
96         }\r
97         const char* getSessionIndex() const {\r
98             return m_obj["session_index"].string();\r
99         }\r
100         const char* getAuthnContextClassRef() const {\r
101             return m_obj["authncontext_class"].string();\r
102         }\r
103         const char* getAuthnContextDeclRef() const {\r
104             return m_obj["authncontext_decl"].string();\r
105         }\r
106         const map<string,const Attribute*>& getAttributes() const {\r
107             if (m_attributes.empty())\r
108                 unmarshallAttributes();\r
109             return m_attributes;\r
110         }\r
111         const vector<const char*>& getAssertionIDs() const {\r
112             if (m_ids.empty()) {\r
113                 DDF id = m_obj["assertions"].first();\r
114                 while (id.isstring()) {\r
115                     m_ids.push_back(id.string());\r
116                     id = id.next();\r
117                 }\r
118             }\r
119             return m_ids;\r
120         }\r
121         \r
122         void addAttributes(const vector<Attribute*>& attributes);\r
123         const RootObject* getAssertion(const char* id) const;\r
124         void addAssertion(RootObject* assertion);\r
125 \r
126     private:\r
127         void unmarshallAttributes() const;\r
128 \r
129         DDF m_obj;\r
130         saml2::NameID* m_nameid;\r
131         mutable map<string,const Attribute*> m_attributes;\r
132         mutable vector<const char*> m_ids;\r
133         mutable map<string,RootObject*> m_tokens;\r
134         SSCache* m_cache;\r
135     };\r
136     \r
137     class SSCache : public SessionCache, public virtual Remoted\r
138     {\r
139     public:\r
140         SSCache(const DOMElement* e);\r
141         ~SSCache() {}\r
142     \r
143         void receive(DDF& in, ostream& out);\r
144         \r
145         string insert(\r
146             time_t expires,\r
147             const Application& application,\r
148             const char* client_addr,\r
149             const saml2md::EntityDescriptor* issuer,\r
150             const saml2::NameID& nameid,\r
151             const char* authn_instant=NULL,\r
152             const char* session_index=NULL,\r
153             const char* authncontext_class=NULL,\r
154             const char* authncontext_decl=NULL,\r
155             const RootObject* ssoToken=NULL,\r
156             const vector<Attribute*>* attributes=NULL\r
157             );\r
158         Session* find(const char* key, const Application& application, const char* client_addr=NULL, time_t timeout=0);\r
159         void remove(const char* key, const Application& application, const char* client_addr);\r
160 \r
161         Category& m_log;\r
162         StorageService* m_storage;\r
163     };\r
164 \r
165     SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)\r
166     {\r
167         return new SSCache(e);\r
168     }\r
169 \r
170     static const XMLCh _StorageService[] =   UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);\r
171 }\r
172 \r
173 StoredSession::~StoredSession()\r
174 {\r
175     m_obj.destroy();\r
176     delete m_nameid;\r
177     for_each(m_attributes.begin(), m_attributes.end(), cleanup_const_pair<string,Attribute>());\r
178     for_each(m_tokens.begin(), m_tokens.end(), cleanup_pair<string,RootObject>());\r
179 }\r
180 \r
181 void StoredSession::unmarshallAttributes() const\r
182 {\r
183     Attribute* attribute;\r
184     DDF attr = m_obj["attributes"].first();\r
185     while (!attr.isnull()) {\r
186         try {\r
187             attribute = Attribute::unmarshall(attr);\r
188             m_attributes[attribute->getId()] = attribute;\r
189             if (m_cache->m_log.isDebugEnabled())\r
190                 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",\r
191                     attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");\r
192         }\r
193         catch (AttributeException& ex) {\r
194             const char* id = attr.first().name();\r
195             m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());\r
196         }\r
197         attr = attr.next();\r
198     }\r
199 }\r
200 \r
201 void StoredSession::addAttributes(const vector<Attribute*>& attributes)\r
202 {\r
203 #ifdef _DEBUG\r
204     xmltooling::NDC ndc("addAttributes");\r
205 #endif\r
206 \r
207     m_cache->m_log.debug("adding attributes to session (%s)", m_obj.name());\r
208     \r
209     int ver;\r
210     do {\r
211         DDF attr;\r
212         DDF attrs = m_obj["attributes"];\r
213         if (!attrs.islist())\r
214             attrs = m_obj.addmember("attributes").list();\r
215         for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {\r
216             attr = (*a)->marshall();\r
217             attrs.add(attr);\r
218         }\r
219         \r
220         // Tentatively increment the version.\r
221         m_obj["version"].integer(m_obj["version"].integer()+1);\r
222         \r
223         ostringstream str;\r
224         str << m_obj;\r
225         string record(str.str()); \r
226 \r
227         try {\r
228             ver = m_cache->m_storage->updateText(m_obj.name(), "session", record.c_str(), 0, m_obj["version"].integer()-1);\r
229         }\r
230         catch (exception&) {\r
231             // Roll back modification to record.\r
232             m_obj["version"].integer(m_obj["version"].integer()-1);\r
233             vector<Attribute*>::size_type count = attributes.size();\r
234             while (count--)\r
235                 attrs.last().destroy();            \r
236             throw;\r
237         }\r
238 \r
239         if (ver <= 0) {\r
240             // Roll back modification to record.\r
241             m_obj["version"].integer(m_obj["version"].integer()-1);\r
242             vector<Attribute*>::size_type count = attributes.size();\r
243             while (count--)\r
244                 attrs.last().destroy();            \r
245         }\r
246         if (!ver) {\r
247             // Fatal problem with update.\r
248             throw IOException("Unable to update stored session.");\r
249         }\r
250         else if (ver < 0) {\r
251             // Out of sync.\r
252             m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");\r
253             ver = m_cache->m_storage->readText(m_obj.name(), "session", &record, NULL);\r
254             if (!ver) {\r
255                 m_cache->m_log.error("readText failed on StorageService for session (%s)", m_obj.name());\r
256                 throw IOException("Unable to read back stored session.");\r
257             }\r
258             \r
259             // Reset object.\r
260             DDF newobj;\r
261             istringstream in(record);\r
262             in >> newobj;\r
263 \r
264             m_ids.clear();\r
265             for_each(m_attributes.begin(), m_attributes.end(), cleanup_const_pair<string,Attribute>());\r
266             m_attributes.clear();\r
267             newobj["version"].integer(ver);\r
268             m_obj.destroy();\r
269             m_obj = newobj;\r
270 \r
271             ver = -1;\r
272         }\r
273     } while (ver < 0);  // negative indicates a sync issue so we retry\r
274 \r
275     TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();\r
276     Locker locker(xlog);\r
277     xlog->log.infoStream() <<\r
278         "Added the following attributes to session (ID: " <<\r
279             m_obj.name() <<\r
280         ") for (applicationId: " <<\r
281             m_obj["application_id"].string() <<\r
282         ") {";\r
283     for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a)\r
284         xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";\r
285     xlog->log.info("}");\r
286 \r
287     // We own them now, so clean them up.\r
288     for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());\r
289 }\r
290 \r
291 const RootObject* StoredSession::getAssertion(const char* id) const\r
292 {\r
293     map<string,RootObject*>::const_iterator i = m_tokens.find(id);\r
294     if (i!=m_tokens.end())\r
295         return i->second;\r
296     \r
297     string tokenstr;\r
298     if (!m_cache->m_storage->readText(m_obj.name(), id, &tokenstr, NULL))\r
299         throw FatalProfileException("Assertion not found in cache.");\r
300 \r
301     // Parse and bind the document into an XMLObject.\r
302     istringstream instr(tokenstr);\r
303     DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr); \r
304     XercesJanitor<DOMDocument> janitor(doc);\r
305     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));\r
306     janitor.release();\r
307     \r
308     RootObject* token = dynamic_cast<RootObject*>(xmlObject.get());\r
309     if (!token || !token->isAssertion())\r
310         throw FatalProfileException("Request for cached assertion returned an unknown object type.");\r
311 \r
312     // Transfer ownership to us.\r
313     xmlObject.release();\r
314     m_tokens[id]=token;\r
315     return token;\r
316 }\r
317 \r
318 void StoredSession::addAssertion(RootObject* assertion)\r
319 {\r
320 #ifdef _DEBUG\r
321     xmltooling::NDC ndc("addAssertion");\r
322 #endif\r
323     \r
324     if (!assertion || !assertion->isAssertion())\r
325         throw FatalProfileException("Unknown object type passed to session for storage.");\r
326 \r
327     auto_ptr_char id(assertion->getID());\r
328 \r
329     m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), m_obj.name());\r
330 \r
331     time_t exp;\r
332     if (!m_cache->m_storage->readText(m_obj.name(), "session", NULL, &exp))\r
333         throw IOException("Unable to load expiration time for stored session.");\r
334 \r
335     ostringstream tokenstr;\r
336     tokenstr << *assertion;\r
337     m_cache->m_storage->createText(m_obj.name(), id.get(), tokenstr.str().c_str(), exp);\r
338     \r
339     int ver;\r
340     do {\r
341         DDF token = DDF(NULL).string(id.get());\r
342         m_obj["assertions"].add(token);\r
343 \r
344         // Tentatively increment the version.\r
345         m_obj["version"].integer(m_obj["version"].integer()+1);\r
346     \r
347         ostringstream str;\r
348         str << m_obj;\r
349         string record(str.str()); \r
350 \r
351         try {\r
352             ver = m_cache->m_storage->updateText(m_obj.name(), "session", record.c_str(), 0, m_obj["version"].integer()-1);\r
353         }\r
354         catch (exception&) {\r
355             token.destroy();\r
356             m_obj["version"].integer(m_obj["version"].integer()-1);\r
357             m_cache->m_storage->deleteText(m_obj.name(), id.get());\r
358             throw;\r
359         }\r
360 \r
361         if (ver <= 0) {\r
362             token.destroy();\r
363             m_obj["version"].integer(m_obj["version"].integer()-1);\r
364         }            \r
365         if (!ver) {\r
366             // Fatal problem with update.\r
367             m_cache->m_log.error("updateText failed on StorageService for session (%s)", m_obj.name());\r
368             m_cache->m_storage->deleteText(m_obj.name(), id.get());\r
369             throw IOException("Unable to update stored session.");\r
370         }\r
371         else if (ver < 0) {\r
372             // Out of sync.\r
373             m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");\r
374             ver = m_cache->m_storage->readText(m_obj.name(), "session", &record, NULL);\r
375             if (!ver) {\r
376                 m_cache->m_log.error("readText failed on StorageService for session (%s)", m_obj.name());\r
377                 m_cache->m_storage->deleteText(m_obj.name(), id.get());\r
378                 throw IOException("Unable to read back stored session.");\r
379             }\r
380             \r
381             // Reset object.\r
382             DDF newobj;\r
383             istringstream in(record);\r
384             in >> newobj;\r
385 \r
386             m_ids.clear();\r
387             for_each(m_attributes.begin(), m_attributes.end(), cleanup_const_pair<string,Attribute>());\r
388             m_attributes.clear();\r
389             newobj["version"].integer(ver);\r
390             m_obj.destroy();\r
391             m_obj = newobj;\r
392             \r
393             ver = -1;\r
394         }\r
395     } while (ver < 0); // negative indicates a sync issue so we retry\r
396 \r
397     m_ids.clear();\r
398     delete assertion;\r
399 \r
400     TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();\r
401     Locker locker(xlog);\r
402     xlog->log.info(\r
403         "Added assertion (ID: %s) to session for (applicationId: %s) with (ID: %s)",\r
404         id.get(), m_obj["application_id"].string(), m_obj.name()\r
405         );\r
406 }\r
407 \r
408 SSCache::SSCache(const DOMElement* e)\r
409     : SessionCache(e), m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), m_storage(NULL)\r
410 {\r
411     SPConfig& conf = SPConfig::getConfig();\r
412     const XMLCh* tag = e ? e->getAttributeNS(NULL,_StorageService) : NULL;\r
413     if (tag && *tag) {\r
414         auto_ptr_char ssid(tag);\r
415         m_storage = conf.getServiceProvider()->getStorageService(ssid.get());\r
416         if (m_storage)\r
417             m_log.info("bound to StorageService (%s)", ssid.get());\r
418         else\r
419             throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");\r
420     }\r
421 \r
422     ListenerService* listener=conf.getServiceProvider()->getListenerService(false);\r
423     if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {\r
424         listener->regListener("insert::"REMOTED_SESSION_CACHE,this);\r
425         listener->regListener("find::"REMOTED_SESSION_CACHE,this);\r
426         listener->regListener("remove::"REMOTED_SESSION_CACHE,this);\r
427     }\r
428     else {\r
429         m_log.info("no ListenerService available, cache remoting is disabled");\r
430     }\r
431 }\r
432 \r
433 string SSCache::insert(\r
434     time_t expires,\r
435     const Application& application,\r
436     const char* client_addr,\r
437     const saml2md::EntityDescriptor* issuer,\r
438     const saml2::NameID& nameid,\r
439     const char* authn_instant,\r
440     const char* session_index,\r
441     const char* authncontext_class,\r
442     const char* authncontext_decl,\r
443     const RootObject* ssoToken,\r
444     const vector<Attribute*>* attributes\r
445     )\r
446 {\r
447 #ifdef _DEBUG\r
448     xmltooling::NDC ndc("insert");\r
449 #endif\r
450 \r
451     m_log.debug("creating new session");\r
452 \r
453     auto_ptr_char key(SAMLConfig::getConfig().generateIdentifier());\r
454 \r
455     // Store session properties in DDF.\r
456     DDF obj = DDF(key.get()).structure();\r
457     obj.addmember("version").integer(1);\r
458     obj.addmember("application_id").string(application.getId());\r
459     if (expires > 0) {\r
460         // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.  \r
461 #ifndef HAVE_GMTIME_R\r
462         struct tm* ptime=gmtime(&expires);\r
463 #else\r
464         struct tm res;\r
465         struct tm* ptime=gmtime_r(&expires,&res);\r
466 #endif\r
467         char timebuf[32];\r
468         strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);\r
469         obj.addmember("expires").string(timebuf);\r
470     }\r
471     obj.addmember("client_addr").string(client_addr);\r
472     if (issuer) {\r
473         auto_ptr_char entity_id(issuer->getEntityID());\r
474         obj.addmember("entity_id").string(entity_id.get());\r
475     }\r
476     if (authn_instant)\r
477         obj.addmember("authn_instant").string(authn_instant);\r
478     if (session_index)\r
479         obj.addmember("session_index").string(session_index);\r
480     if (authncontext_class)\r
481         obj.addmember("authncontext_class").string(authncontext_class);\r
482     if (authncontext_decl)\r
483         obj.addmember("authncontext_decl").string(authncontext_decl);\r
484 \r
485     ostringstream namestr;\r
486     namestr << nameid;\r
487     obj.addmember("nameid").string(namestr.str().c_str());\r
488     \r
489     string tokenstr;\r
490     if (ssoToken) {\r
491         ostringstream tstr;\r
492         tstr << *ssoToken;\r
493         tokenstr = tstr.str();\r
494         auto_ptr_char tokenid(ssoToken->getID());\r
495         DDF tokid = DDF(NULL).string(tokenid.get());\r
496         obj.addmember("assertions").list().add(tokid);\r
497     }\r
498     \r
499     if (attributes) {\r
500         DDF attr;\r
501         DDF attrlist = obj.addmember("attributes").list();\r
502         for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {\r
503             attr = (*a)->marshall();\r
504             attrlist.add(attr);\r
505         }\r
506     }\r
507     \r
508     ostringstream record;\r
509     record << obj;\r
510     \r
511     m_log.debug("storing new session...");\r
512     time_t now = time(NULL);\r
513     m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout);\r
514     if (ssoToken) {\r
515         try {\r
516             m_storage->createText(key.get(), obj["assertions"].first().string(), tokenstr.c_str(), now + m_cacheTimeout);\r
517         }\r
518         catch (exception& ex) {\r
519             m_log.error("error storing assertion along with session: %s", ex.what());\r
520         }\r
521     }\r
522 \r
523     const char* pid = obj["entity_id"].string();\r
524     m_log.debug("new session created: SessionID (%s) IdP (%s) Address (%s)", key.get(), pid ? pid : "none", client_addr);\r
525 \r
526     // Transaction Logging\r
527     auto_ptr_char name(nameid.getName());\r
528     TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();\r
529     Locker locker(xlog);\r
530     xlog->log.infoStream() <<\r
531         "New session (ID: " <<\r
532             key.get() <<\r
533         ") with (applicationId: " <<\r
534             application.getId() <<\r
535         ") for principal from (IdP: " <<\r
536             (pid ? pid : "none") <<\r
537         ") at (ClientAddress: " <<\r
538             client_addr <<\r
539         ") with (NameIdentifier: " <<\r
540             name.get() <<\r
541         ")";\r
542     \r
543     if (attributes) {\r
544         xlog->log.infoStream() <<\r
545             "Cached the following attributes with session (ID: " <<\r
546                 key.get() <<\r
547             ") for (applicationId: " <<\r
548                 application.getId() <<\r
549             ") {";\r
550         for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a)\r
551             xlog->log.infoStream() << "\t" << (*a)->getId() << " (" << (*a)->valueCount() << " values)";\r
552         xlog->log.info("}");\r
553         for_each(attributes->begin(), attributes->end(), xmltooling::cleanup<Attribute>());\r
554     }\r
555 \r
556     return key.get();\r
557 }\r
558 \r
559 Session* SSCache::find(const char* key, const Application& application, const char* client_addr, time_t timeout)\r
560 {\r
561 #ifdef _DEBUG\r
562     xmltooling::NDC ndc("find");\r
563 #endif\r
564 \r
565     m_log.debug("searching for session (%s)", key);\r
566     \r
567     time_t lastAccess;\r
568     string record;\r
569     int ver = m_storage->readText(key, "session", &record, &lastAccess);\r
570     if (!ver)\r
571         return NULL;\r
572     \r
573     m_log.debug("reconstituting session and checking validity");\r
574     \r
575     DDF obj;\r
576     istringstream in(record);\r
577     in >> obj;\r
578     \r
579     if (!XMLString::equals(obj["application_id"].string(), application.getId())) {\r
580         m_log.error("an application (%s) tried to access another application's session", application.getId());\r
581         obj.destroy();\r
582         return NULL;\r
583     }\r
584 \r
585     if (client_addr) {\r
586         if (m_log.isDebugEnabled())\r
587             m_log.debug("comparing client address %s against %s", client_addr, obj["client_addr"].string());\r
588         if (strcmp(obj["client_addr"].string(),client_addr)) {\r
589             m_log.warn("client address mismatch");\r
590             remove(key, application, client_addr);\r
591             RetryableProfileException ex(\r
592                 "Your IP address ($1) does not match the address recorded at the time the session was established.",\r
593                 params(1,client_addr)\r
594                 );\r
595             string eid(obj["entity_id"].string());\r
596             obj.destroy();\r
597             if (eid.empty())\r
598                 throw ex;\r
599             MetadataProvider* m=application.getMetadataProvider();\r
600             Locker locker(m);\r
601             annotateException(&ex,m->getEntityDescriptor(eid.c_str(),false)); // throws it\r
602         }\r
603     }\r
604 \r
605     lastAccess -= m_cacheTimeout;   // adjusts it back to the last time the record's timestamp was touched\r
606     time_t now=time(NULL);\r
607     \r
608     if (timeout > 0 && now - lastAccess >= timeout) {\r
609         m_log.info("session timed out (ID: %s)", key);\r
610         remove(key, application, client_addr);\r
611         RetryableProfileException ex("Your session has expired, and you must re-authenticate.");\r
612         string eid(obj["entity_id"].string());\r
613         obj.destroy();\r
614         if (eid.empty())\r
615             throw ex;\r
616         MetadataProvider* m=application.getMetadataProvider();\r
617         Locker locker(m);\r
618         annotateException(&ex,m->getEntityDescriptor(eid.c_str(),false)); // throws it\r
619     }\r
620     \r
621     auto_ptr_XMLCh exp(obj["expires"].string());\r
622     if (exp.get()) {\r
623         DateTime iso(exp.get());\r
624         iso.parseDateTime();\r
625         if (now > iso.getEpoch()) {\r
626             m_log.info("session expired (ID: %s)", key);\r
627             remove(key, application, client_addr);\r
628             RetryableProfileException ex("Your session has expired, and you must re-authenticate.");\r
629             string eid(obj["entity_id"].string());\r
630             obj.destroy();\r
631             if (eid.empty())\r
632                 throw ex;\r
633             MetadataProvider* m=application.getMetadataProvider();\r
634             Locker locker(m);\r
635             annotateException(&ex,m->getEntityDescriptor(eid.c_str(),false)); // throws it\r
636         }\r
637     }\r
638     \r
639     // Update storage expiration, if possible.\r
640     try {\r
641         m_storage->updateContext(key, now + m_cacheTimeout);\r
642     }\r
643     catch (exception& ex) {\r
644         m_log.error("failed to update session expiration: %s", ex.what());\r
645     }\r
646 \r
647     // Finally build the Session object.\r
648     try {\r
649         return new StoredSession(this, obj);\r
650     }\r
651     catch (exception&) {\r
652         obj.destroy();\r
653         throw;\r
654     }\r
655 }\r
656 \r
657 void SSCache::remove(const char* key, const Application& application, const char* client_addr)\r
658 {\r
659 #ifdef _DEBUG\r
660     xmltooling::NDC ndc("remove");\r
661 #endif\r
662 \r
663     m_log.debug("removing session (%s)", key);\r
664 \r
665     m_storage->deleteContext(key);\r
666 \r
667     TransactionLog* xlog = SPConfig::getConfig().getServiceProvider()->getTransactionLog();\r
668     Locker locker(xlog);\r
669     xlog->log.info("Destroyed session (applicationId: %s) (ID: %s)", application.getId(), key);\r
670 }\r
671 \r
672 void SSCache::receive(DDF& in, ostream& out)\r
673 {\r
674 #ifdef _DEBUG\r
675     xmltooling::NDC ndc("receive");\r
676 #endif\r
677 \r
678     if (!strcmp(in.name(),"insert::"REMOTED_SESSION_CACHE)) {\r
679         auto_ptr_char key(SAMLConfig::getConfig().generateIdentifier());\r
680         in.name(key.get());\r
681 \r
682         DDF token = in["token"].remove();\r
683         DDFJanitor tjan(token);\r
684         \r
685         m_log.debug("storing new session...");\r
686         ostringstream record;\r
687         record << in;\r
688         time_t now = time(NULL);\r
689         m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout);\r
690         if (token.isstring()) {\r
691             try {\r
692                 m_storage->createText(key.get(), in["assertions"].first().string(), token.string(), now + m_cacheTimeout);\r
693             }\r
694             catch (IOException& ex) {\r
695                 m_log.error("error storing assertion along with session: %s", ex.what());\r
696             }\r
697         }\r
698         const char* pid = in["entity_id"].string();\r
699         m_log.debug("new session created: SessionID (%s) IdP (%s) Address (%s)", key.get(), pid ? pid : "none", in["client_addr"].string());\r
700     \r
701         DDF ret = DDF(NULL).structure();\r
702         DDFJanitor jan(ret);\r
703         ret.addmember("key").string(key.get());\r
704         out << ret;\r
705     }\r
706     else if (!strcmp(in.name(),"find::"REMOTED_SESSION_CACHE)) {\r
707         const char* key=in["key"].string();\r
708         if (!key)\r
709             throw ListenerException("Required parameters missing for session removal.");\r
710 \r
711         // Do an unversioned read.\r
712         string record;\r
713         time_t lastAccess;\r
714         if (!m_storage->readText(key, "session", &record, &lastAccess)) {\r
715             DDF ret(NULL);\r
716             DDFJanitor jan(ret);\r
717             out << ret;\r
718             return;\r
719         }\r
720 \r
721         // Adjust for expiration to recover last access time and check timeout.\r
722         lastAccess -= m_cacheTimeout;\r
723         time_t now=time(NULL);\r
724 \r
725         // See if we need to check for a timeout.\r
726         time_t timeout = 0;\r
727         auto_ptr_XMLCh dt(in["timeout"].string());\r
728         if (dt.get()) {\r
729             DateTime dtobj(dt.get());\r
730             dtobj.parseDateTime();\r
731             timeout = dtobj.getEpoch();\r
732         }\r
733                 \r
734         if (timeout > 0 && now - lastAccess >= timeout) {\r
735             m_log.info("session timed out (ID: %s)", key);\r
736             remove(key,*(SPConfig::getConfig().getServiceProvider()->getApplication("default")),NULL);\r
737             throw RetryableProfileException("Your session has expired, and you must re-authenticate.");\r
738         } \r
739 \r
740         // Update storage expiration, if possible.\r
741         try {\r
742             m_storage->updateContext(key, now + m_cacheTimeout);\r
743         }\r
744         catch (exception& ex) {\r
745             m_log.error("failed to update session expiration: %s", ex.what());\r
746         }\r
747             \r
748         // Send the record back.\r
749         out << record;\r
750     }\r
751     else if (!strcmp(in.name(),"touch::"REMOTED_SESSION_CACHE)) {\r
752         const char* key=in["key"].string();\r
753         if (!key)\r
754             throw ListenerException("Required parameters missing for session check.");\r
755 \r
756         // Do a versioned read.\r
757         string record;\r
758         time_t lastAccess;\r
759         int curver = in["version"].integer();\r
760         int ver = m_storage->readText(key, "session", &record, &lastAccess, curver);\r
761         if (ver == 0) {\r
762             m_log.warn("unsuccessful versioned read of session (ID: %s), caches out of sync?", key);\r
763             throw RetryableProfileException("Your session has expired, and you must re-authenticate.");\r
764         }\r
765 \r
766         // Adjust for expiration to recover last access time and check timeout.\r
767         lastAccess -= m_cacheTimeout;\r
768         time_t now=time(NULL);\r
769 \r
770         // See if we need to check for a timeout.\r
771         time_t timeout = 0;\r
772         auto_ptr_XMLCh dt(in["timeout"].string());\r
773         if (dt.get()) {\r
774             DateTime dtobj(dt.get());\r
775             dtobj.parseDateTime();\r
776             timeout = dtobj.getEpoch();\r
777         }\r
778                 \r
779         if (timeout > 0 && now - lastAccess >= timeout) {\r
780             m_log.info("session timed out (ID: %s)", key);\r
781             throw RetryableProfileException("Your session has expired, and you must re-authenticate.");\r
782         } \r
783 \r
784         // Update storage expiration, if possible.\r
785         try {\r
786             m_storage->updateContext(key, now + m_cacheTimeout);\r
787         }\r
788         catch (exception& ex) {\r
789             m_log.error("failed to update session expiration: %s", ex.what());\r
790         }\r
791             \r
792         if (ver > curver) {\r
793             // Send the record back.\r
794             out << record;\r
795         }\r
796         else {\r
797             DDF ret(NULL);\r
798             DDFJanitor jan(ret);\r
799             out << ret;\r
800         }\r
801     }\r
802     else if (!strcmp(in.name(),"remove::"REMOTED_SESSION_CACHE)) {\r
803         const char* key=in["key"].string();\r
804         if (!key)\r
805             throw ListenerException("Required parameter missing for session removal.");\r
806         \r
807         remove(key,*(SPConfig::getConfig().getServiceProvider()->getApplication("default")),NULL);\r
808         DDF ret(NULL);\r
809         DDFJanitor jan(ret);\r
810         out << ret;\r
811     }\r
812     else if (!strcmp(in.name(),"getAssertion::"REMOTED_SESSION_CACHE)) {\r
813         const char* key=in["key"].string();\r
814         const char* id=in["id"].string();\r
815         if (!key || !id)\r
816             throw ListenerException("Required parameters missing for assertion retrieval.");\r
817         string token;\r
818         if (!m_storage->readText(key, id, &token, NULL))\r
819             throw FatalProfileException("Assertion not found in cache.");\r
820         out << token;\r
821     }\r
822 }\r