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.
18 * TransformSessionInitiator.cpp
20 * Support for mapping input into an entityID using a transform.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
30 #include "handler/SessionInitiator.h"
31 #include "util/SPConstants.h"
34 # include <saml/saml2/metadata/Metadata.h>
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/util/URLEncoder.h>
38 #include <xercesc/util/XMLUniDefs.hpp>
39 #include <xercesc/util/regx/RegularExpression.hpp>
41 using namespace shibsp;
42 using namespace opensaml::saml2md;
43 using namespace opensaml;
44 using namespace xmltooling;
49 #if defined (_MSC_VER)
50 #pragma warning( push )
51 #pragma warning( disable : 4250 )
54 class SHIBSP_DLLLOCAL TransformSINodeFilter : public DOMNodeFilter
57 short acceptNode(const DOMNode* node) const {
62 static SHIBSP_DLLLOCAL TransformSINodeFilter g_TSINFilter;
65 static const XMLCh force[] = UNICODE_LITERAL_5(f,o,r,c,e);
66 static const XMLCh match[] = UNICODE_LITERAL_5(m,a,t,c,h);
67 static const XMLCh Regex[] = UNICODE_LITERAL_5(R,e,g,e,x);
68 static const XMLCh Subst[] = UNICODE_LITERAL_5(S,u,b,s,t);
71 class SHIBSP_DLLLOCAL TransformSessionInitiator : public SessionInitiator, public AbstractHandler, public RemotedHandler
74 TransformSessionInitiator(const DOMElement* e, const char* appId)
75 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Transform"), &g_TSINFilter), m_appId(appId) {
76 // If Location isn't set, defer address registration until the setParent call.
77 pair<bool,const char*> loc = getString("Location");
79 string address = m_appId + loc.second + "::run::TransformSI";
80 setAddress(address.c_str());
84 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
85 m_alwaysRun = getBool("alwaysRun").second;
86 e = XMLHelper::getFirstChildElement(e);
88 if (e->hasChildNodes()) {
89 const XMLCh* flag = e->getAttributeNS(NULL, force);
92 if (XMLString::equals(e->getLocalName(), Subst)) {
93 auto_ptr_char temp(e->getFirstChild()->getNodeValue());
94 m_subst.push_back(pair<bool,string>((*flag==chDigit_1 || *flag==chLatin_t), temp.get()));
96 else if (XMLString::equals(e->getLocalName(), Regex) && e->hasAttributeNS(NULL, match)) {
97 auto_ptr_char m(e->getAttributeNS(NULL, match));
98 auto_ptr_char repl(e->getFirstChild()->getNodeValue());
99 m_regex.push_back(make_pair((*flag==chDigit_1 || *flag==chLatin_t), pair<string,string>(m.get(), repl.get())));
102 m_log.warn("Unknown element found in Transform SessionInitiator configuration, check for errors.");
105 e = XMLHelper::getNextSiblingElement(e);
111 virtual ~TransformSessionInitiator() {}
113 void setParent(const PropertySet* parent);
114 void receive(DDF& in, ostream& out);
115 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
118 void doRequest(const Application& application, string& entityID) const;
122 vector< pair<bool, string> > m_subst;
123 vector< pair< bool, pair<string,string> > > m_regex;
127 #if defined (_MSC_VER)
128 #pragma warning( pop )
131 SessionInitiator* SHIBSP_DLLLOCAL TransformSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
133 return new TransformSessionInitiator(p.first, p.second);
138 void TransformSessionInitiator::setParent(const PropertySet* parent)
140 DOMPropertySet::setParent(parent);
141 pair<bool,const char*> loc = getString("Location");
143 string address = m_appId + loc.second + "::run::TransformSI";
144 setAddress(address.c_str());
147 m_log.warn("no Location property in Transform SessionInitiator (or parent), can't register as remoted handler");
151 pair<bool,long> TransformSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
153 // We have to have a candidate name to function.
154 if (entityID.empty())
155 return make_pair(false,0L);
158 const Application& app=request.getApplication();
160 m_log.debug("attempting to transform input (%s) into a valid entityID", entityID.c_str());
162 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess))
163 doRequest(app, entityID);
166 DDF out,in = DDF(m_address.c_str()).structure();
167 DDFJanitor jin(in), jout(out);
168 in.addmember("application_id").string(app.getId());
169 in.addmember("entity_id").string(entityID.c_str());
171 // Remote the processing.
172 out = request.getServiceProvider().getListenerService()->send(in);
174 entityID = out.string();
177 return make_pair(false,0L);
180 void TransformSessionInitiator::receive(DDF& in, ostream& out)
183 const char* aid=in["application_id"].string();
184 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
186 // Something's horribly wrong.
187 m_log.error("couldn't find application (%s) to generate AuthnRequest", aid ? aid : "(missing)");
188 throw ConfigurationException("Unable to locate application for new session, deleted?");
191 const char* entityID = in["entity_id"].string();
193 throw ConfigurationException("No entityID parameter supplied to remoted SessionInitiator.");
195 string copy(entityID);
196 doRequest(*app, copy);
197 DDF ret = DDF(NULL).string(copy.c_str());
198 DDFJanitor jout(ret);
202 void TransformSessionInitiator::doRequest(const Application& application, string& entityID) const
205 MetadataProvider* m=application.getMetadataProvider();
208 MetadataProvider::Criteria mc(entityID.c_str(), &IDPSSODescriptor::ELEMENT_QNAME);
209 pair<const EntityDescriptor*,const RoleDescriptor*> entity;
211 // First check the original value, it might be valid already.
212 entity = m->getEntityDescriptor(mc);
217 m_log.debug("attempting transform of (%s)", entityID.c_str());
219 // Guess not, try each subst.
221 for (vector< pair<bool,string> >::const_iterator t = m_subst.begin(); t != m_subst.end(); ++t) {
222 string::size_type pos = t->second.find("$entityID");
223 if (pos == string::npos)
225 transform = t->second;
226 transform.replace(pos, 9, entityID);
228 m_log.info("forcibly transformed entityID from (%s) to (%s)", entityID.c_str(), transform.c_str());
229 entityID = transform;
232 m_log.debug("attempting lookup with entityID (%s)", transform.c_str());
234 mc.entityID_ascii = transform.c_str();
235 entity = m->getEntityDescriptor(mc);
237 m_log.info("transformed entityID from (%s) to (%s)", entityID.c_str(), transform.c_str());
239 entityID = transform;
245 for (vector< pair< bool, pair<string,string> > >::const_iterator r = m_regex.begin(); r != m_regex.end(); ++r) {
247 RegularExpression exp(r->second.first.c_str());
248 XMLCh* temp = exp.replace(entityID.c_str(), r->second.second.c_str());
250 auto_ptr_char narrow(temp);
251 XMLString::release(&temp);
253 // For some reason it returns the match string if it doesn't match the expression.
254 if (entityID == narrow.get())
258 m_log.info("forcibly transformed entityID from (%s) to (%s)", entityID.c_str(), narrow.get());
259 entityID = narrow.get();
262 m_log.debug("attempting lookup with entityID (%s)", narrow.get());
264 mc.entityID_ascii = narrow.get();
265 entity = m->getEntityDescriptor(mc);
267 m_log.info("transformed entityID from (%s) to (%s)", entityID.c_str(), narrow.get());
269 entityID = narrow.get();
274 catch (XMLException& ex) {
275 auto_ptr_char msg(ex.getMessage());
276 m_log.error("caught error applying regular expression: %s", msg.get());
280 m_log.warn("unable to find a valid entityID based on the supplied input");