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 alwaysRun[] = UNICODE_LITERAL_9(a,l,w,a,y,s,R,u,n);
66 static const XMLCh force[] = UNICODE_LITERAL_5(f,o,r,c,e);
67 static const XMLCh match[] = UNICODE_LITERAL_5(m,a,t,c,h);
68 static const XMLCh Regex[] = UNICODE_LITERAL_5(R,e,g,e,x);
69 static const XMLCh Subst[] = UNICODE_LITERAL_5(S,u,b,s,t);
72 class SHIBSP_DLLLOCAL TransformSessionInitiator : public SessionInitiator, public AbstractHandler, public RemotedHandler
75 TransformSessionInitiator(const DOMElement* e, const char* appId)
76 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Transform"), &g_TSINFilter), m_appId(appId) {
77 // If Location isn't set, defer address registration until the setParent call.
78 pair<bool,const char*> loc = getString("Location");
80 string address = m_appId + loc.second + "::run::TransformSI";
81 setAddress(address.c_str());
85 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
86 m_alwaysRun = getBool("alwaysRun").second;
87 e = XMLHelper::getFirstChildElement(e);
89 if (e->hasChildNodes()) {
90 const XMLCh* flag = e->getAttributeNS(NULL, force);
93 if (XMLString::equals(e->getLocalName(), Subst)) {
94 auto_ptr_char temp(e->getFirstChild()->getNodeValue());
95 m_subst.push_back(pair<bool,string>((*flag==chDigit_1 || *flag==chLatin_t), temp.get()));
97 else if (XMLString::equals(e->getLocalName(), Regex) && e->hasAttributeNS(NULL, match)) {
98 auto_ptr_char m(e->getAttributeNS(NULL, match));
99 auto_ptr_char repl(e->getFirstChild()->getNodeValue());
100 m_regex.push_back(make_pair((*flag==chDigit_1 || *flag==chLatin_t), pair<string,string>(m.get(), repl.get())));
103 m_log.warn("Unknown element found in Transform SessionInitiator configuration, check for errors.");
106 e = XMLHelper::getNextSiblingElement(e);
112 virtual ~TransformSessionInitiator() {}
114 void setParent(const PropertySet* parent);
115 void receive(DDF& in, ostream& out);
116 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
119 void doRequest(const Application& application, string& entityID) const;
123 vector< pair<bool, string> > m_subst;
124 vector< pair< bool, pair<string,string> > > m_regex;
128 #if defined (_MSC_VER)
129 #pragma warning( pop )
132 SessionInitiator* SHIBSP_DLLLOCAL TransformSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
134 return new TransformSessionInitiator(p.first, p.second);
139 void TransformSessionInitiator::setParent(const PropertySet* parent)
141 DOMPropertySet::setParent(parent);
142 pair<bool,const char*> loc = getString("Location");
144 string address = m_appId + loc.second + "::run::TransformSI";
145 setAddress(address.c_str());
148 m_log.warn("no Location property in Transform SessionInitiator (or parent), can't register as remoted handler");
152 pair<bool,long> TransformSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
154 // We have to have a candidate name to function.
155 if (entityID.empty())
156 return make_pair(false,0L);
159 const Application& app=request.getApplication();
161 m_log.debug("attempting to transform input (%s) into a valid entityID", entityID.c_str());
163 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess))
164 doRequest(app, entityID);
167 DDF out,in = DDF(m_address.c_str()).structure();
168 DDFJanitor jin(in), jout(out);
169 in.addmember("application_id").string(app.getId());
170 in.addmember("entity_id").string(entityID.c_str());
172 // Remote the processing.
173 out = request.getServiceProvider().getListenerService()->send(in);
175 entityID = out.string();
178 return make_pair(false,0L);
181 void TransformSessionInitiator::receive(DDF& in, ostream& out)
184 const char* aid=in["application_id"].string();
185 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
187 // Something's horribly wrong.
188 m_log.error("couldn't find application (%s) to generate AuthnRequest", aid ? aid : "(missing)");
189 throw ConfigurationException("Unable to locate application for new session, deleted?");
192 const char* entityID = in["entity_id"].string();
194 throw ConfigurationException("No entityID parameter supplied to remoted SessionInitiator.");
196 string copy(entityID);
197 doRequest(*app, copy);
198 DDF ret = DDF(NULL).string(copy.c_str());
199 DDFJanitor jout(ret);
203 void TransformSessionInitiator::doRequest(const Application& application, string& entityID) const
206 MetadataProvider* m=application.getMetadataProvider();
209 MetadataProvider::Criteria mc(entityID.c_str(), &IDPSSODescriptor::ELEMENT_QNAME);
210 pair<const EntityDescriptor*,const RoleDescriptor*> entity;
212 // First check the original value, it might be valid already.
213 entity = m->getEntityDescriptor(mc);
218 m_log.debug("attempting transform of (%s)", entityID.c_str());
220 // Guess not, try each subst.
222 for (vector< pair<bool,string> >::const_iterator t = m_subst.begin(); t != m_subst.end(); ++t) {
223 string::size_type pos = t->second.find("$entityID");
224 if (pos == string::npos)
226 transform = t->second;
227 transform.replace(pos, 9, entityID);
229 m_log.info("forcibly transformed entityID from (%s) to (%s)", entityID.c_str(), transform.c_str());
230 entityID = transform;
233 m_log.debug("attempting lookup with entityID (%s)", transform.c_str());
235 mc.entityID_ascii = transform.c_str();
236 entity = m->getEntityDescriptor(mc);
238 m_log.info("transformed entityID from (%s) to (%s)", entityID.c_str(), transform.c_str());
240 entityID = transform;
246 for (vector< pair< bool, pair<string,string> > >::const_iterator r = m_regex.begin(); r != m_regex.end(); ++r) {
248 RegularExpression exp(r->second.first.c_str());
249 XMLCh* temp = exp.replace(entityID.c_str(), r->second.second.c_str());
251 auto_ptr_char narrow(temp);
252 XMLString::release(&temp);
254 // For some reason it returns the match string if it doesn't match the expression.
255 if (entityID == narrow.get())
259 m_log.info("forcibly transformed entityID from (%s) to (%s)", entityID.c_str(), narrow.get());
260 entityID = narrow.get();
263 m_log.debug("attempting lookup with entityID (%s)", narrow.get());
265 mc.entityID_ascii = narrow.get();
266 entity = m->getEntityDescriptor(mc);
268 m_log.info("transformed entityID from (%s) to (%s)", entityID.c_str(), narrow.get());
270 entityID = narrow.get();
275 catch (XMLException& ex) {
276 auto_ptr_char msg(ex.getMessage());
277 m_log.error("caught error applying regular expression: %s", msg.get());
281 m_log.warn("unable to find a valid entityID based on the supplied input");