Improve consistency of cache API methods.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / AssertionLookup.cpp
index 5b8f006..8ae7e10 100644 (file)
@@ -24,7 +24,7 @@
 #include "Application.h"
 #include "exceptions.h"
 #include "ServiceProvider.h"
-#include "SessionCache.h"
+#include "SessionCacheEx.h"
 #include "handler/AbstractHandler.h"
 #include "handler/RemotedHandler.h"
 #include "util/SPConstants.h"
@@ -33,7 +33,6 @@ using namespace shibspconstants;
 using namespace shibsp;
 using namespace opensaml;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 
 namespace shibsp {
@@ -43,7 +42,17 @@ namespace shibsp {
     #pragma warning( disable : 4250 )
 #endif
 
-    class SHIBSP_API AssertionLookup : public AbstractHandler, public RemotedHandler 
+    class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
+    {
+    public:
+        short acceptNode(const DOMNode* node) const {
+            return FILTER_REJECT;
+        }
+    };
+
+    static SHIBSP_DLLLOCAL Blocker g_Blocker;
+
+    class SHIBSP_API AssertionLookup : public AbstractHandler, public RemotedHandler
     {
     public:
         AssertionLookup(const DOMElement* e, const char* appId);
@@ -52,6 +61,10 @@ namespace shibsp {
         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
         void receive(DDF& in, ostream& out);
 
+        const char* getType() const {
+            return "AssertionLookup";
+        }
+
     private:
         pair<bool,long> processMessage(const Application& application, HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
 
@@ -70,7 +83,7 @@ namespace shibsp {
 };
 
 AssertionLookup::AssertionLookup(const DOMElement* e, const char* appId)
-    : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".AssertionLookup"))
+    : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".AssertionLookup"), &g_Blocker)
 {
     setAddress("run::AssertionLookup");
     if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
@@ -97,9 +110,9 @@ pair<bool,long> AssertionLookup::run(SPRequest& request, bool isHandler) const
     SPConfig& conf = SPConfig::getConfig();
     if (conf.isEnabled(SPConfig::InProcess)) {
         if (m_acl.count(request.getRemoteAddr()) == 0) {
-            m_log.error("request for assertion lookup blocked from invalid address (%s)", request.getRemoteAddr());
+            m_log.error("request for assertion lookup blocked from invalid address (%s)", request.getRemoteAddr().c_str());
             istringstream msg("Assertion Lookup Blocked");
-            return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
+            return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_UNAUTHORIZED));
         }
     }
     
@@ -110,10 +123,9 @@ pair<bool,long> AssertionLookup::run(SPRequest& request, bool isHandler) const
         }
         else {
             // When not out of process, we remote all the message processing.
-            DDF out,in = wrap(request, NULL, true);
+            DDF out,in = wrap(request);
             DDFJanitor jin(in), jout(out);
             
-            in.addmember("application_id").string(request.getApplication().getId());
             out=request.getServiceProvider().getListenerService()->send(in);
             return unwrap(request, out);
         }
@@ -164,8 +176,14 @@ pair<bool,long> AssertionLookup::processMessage(const Application& application,
 
     m_log.debug("processing assertion lookup request (session: %s, assertion: %s)", key, ID);
 
+    SessionCacheEx* cache = dynamic_cast<SessionCacheEx*>(application.getServiceProvider().getSessionCache());
+    if (!cache) {
+        m_log.error("session cache does not support extended API");
+        throw FatalProfileException("Session cache does not support assertion lookup.");
+    }
+
     // The cache will either silently pass a session or NULL back, or throw an exception out.
-    Session* session = application.getServiceProvider().getSessionCache()->find(key, application);
+    Session* session = cache->find(application, ID);
     if (!session) {
         m_log.error("valid session (%s) not found for assertion lookup", key);
         throw FatalProfileException("Session key not found.");
@@ -179,11 +197,11 @@ pair<bool,long> AssertionLookup::processMessage(const Application& application,
         throw FatalProfileException("Assertion not found.");
     }
 
-    stringstream s;\r
-    s << *assertion;\r
+    stringstream s;
+    s << *assertion;
     httpResponse.setContentType("application/samlassertion+xml");
     return make_pair(true, httpResponse.sendResponse(s));
 #else
-    return make_pair(false,0);
+    return make_pair(false,0L);
 #endif
 }