2 * Copyright 2001-2007 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.
20 * Handler for looking assertions in SessionCache
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
30 #include "util/SPConstants.h"
32 using namespace shibspconstants;
33 using namespace shibsp;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
41 #if defined (_MSC_VER)
42 #pragma warning( push )
43 #pragma warning( disable : 4250 )
46 class SHIBSP_API AssertionLookup : public AbstractHandler, public RemotedHandler
49 AssertionLookup(const DOMElement* e, const char* appId);
50 virtual ~AssertionLookup() {}
52 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
53 void receive(DDF& in, ostream& out);
56 pair<bool,long> processMessage(const Application& application, HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
61 #if defined (_MSC_VER)
62 #pragma warning( pop )
65 Handler* SHIBSP_DLLLOCAL AssertionLookupFactory(const pair<const DOMElement*,const char*>& p)
67 return new AssertionLookup(p.first, p.second);
72 AssertionLookup::AssertionLookup(const DOMElement* e, const char* appId)
73 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".AssertionLookup"))
75 setAddress("run::AssertionLookup");
76 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
77 pair<bool,const char*> acl = getString("exportACL");
79 m_acl.insert("127.0.0.1");
82 string aclbuf=acl.second;
84 for (unsigned int i=0; i < aclbuf.length(); i++) {
85 if (aclbuf.at(i)==' ') {
86 m_acl.insert(aclbuf.substr(j, i-j));
90 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
94 pair<bool,long> AssertionLookup::run(SPRequest& request, bool isHandler) const
97 SPConfig& conf = SPConfig::getConfig();
98 if (conf.isEnabled(SPConfig::InProcess)) {
99 if (m_acl.count(request.getRemoteAddr()) == 0) {
100 m_log.error("request for assertion lookup blocked from invalid address (%s)", request.getRemoteAddr());
101 istringstream msg("Assertion Lookup Blocked");
102 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
107 if (conf.isEnabled(SPConfig::OutOfProcess)) {
108 // When out of process, we run natively and directly process the message.
109 return processMessage(request.getApplication(), request, request);
112 // When not out of process, we remote all the message processing.
113 DDF out,in = wrap(request, NULL, true);
114 DDFJanitor jin(in), jout(out);
116 out=request.getServiceProvider().getListenerService()->send(in);
117 return unwrap(request, out);
120 catch (exception& ex) {
121 m_log.error("error while processing request: %s", ex.what());
122 istringstream msg("Assertion Lookup Failed");
123 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
127 void AssertionLookup::receive(DDF& in, ostream& out)
130 const char* aid=in["application_id"].string();
131 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
133 // Something's horribly wrong.
134 m_log.error("couldn't find application (%s) for assertion lookup", aid ? aid : "(missing)");
135 throw ConfigurationException("Unable to locate application for assertion lookup, deleted?");
138 // Unpack the request.
139 auto_ptr<HTTPRequest> req(getRequest(in));
140 //m_log.debug("found %d client certificates", req->getClientCertificates().size());
142 // Wrap a response shim.
144 DDFJanitor jout(ret);
145 auto_ptr<HTTPResponse> resp(getResponse(ret));
147 // Since we're remoted, the result should either be a throw, a false/0 return,
148 // which we just return as an empty structure, or a response/redirect,
149 // which we capture in the facade and send back.
150 processMessage(*app, *req.get(), *resp.get());
154 pair<bool,long> AssertionLookup::processMessage(const Application& application, HTTPRequest& httpRequest, HTTPResponse& httpResponse) const
157 const char* key = httpRequest.getParameter("key");
158 const char* ID = httpRequest.getParameter("ID");
159 if (!key || !*key || !ID || !*ID) {
160 m_log.error("assertion lookup request failed, missing required parameters");
161 throw FatalProfileException("Missing key or ID parameters.");
164 m_log.debug("processing assertion lookup request (session: %s, assertion: %s)", key, ID);
166 // The cache will either silently pass a session or NULL back, or throw an exception out.
167 Session* session = application.getServiceProvider().getSessionCache()->find(key, application);
169 m_log.error("valid session (%s) not found for assertion lookup", key);
170 throw FatalProfileException("Session key not found.");
173 Locker locker(session, false);
175 const Assertion* assertion = session->getAssertion(ID);
177 m_log.error("assertion (%s) not found in session (%s)", ID, key);
178 throw FatalProfileException("Assertion not found.");
183 httpResponse.setContentType("application/samlassertion+xml");
184 return make_pair(true, httpResponse.sendResponse(s));
186 return make_pair(false,0);