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 * Shib1SessionInitiator.cpp
20 * Shibboleth 1.x AuthnRequest support.
24 #include "Application.h"
25 #include "SPRequest.h"
26 #include "handler/AbstractHandler.h"
27 #include "handler/SessionInitiator.h"
28 #include "util/SPConstants.h"
30 #include <saml/saml2/metadata/Metadata.h>
31 #include <saml/saml2/metadata/EndpointManager.h>
32 #include <xmltooling/XMLToolingConfig.h>
33 #include <xmltooling/util/URLEncoder.h>
35 using namespace shibsp;
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 #if defined (_MSC_VER)
45 #pragma warning( push )
46 #pragma warning( disable : 4250 )
49 class SHIBSP_DLLLOCAL Shib1SessionInitiator : public SessionInitiator, public AbstractHandler
52 Shib1SessionInitiator(const DOMElement* e, const char* appId)
53 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator")) {}
54 virtual ~Shib1SessionInitiator() {}
56 pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
59 #if defined (_MSC_VER)
60 #pragma warning( pop )
63 SessionInitiator* SHIBSP_DLLLOCAL Shib1SessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
65 return new Shib1SessionInitiator(p.first, p.second);
70 pair<bool,long> Shib1SessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
72 // We have to know the IdP to function.
73 if (!entityID || !*entityID)
74 return make_pair(false,0);
78 const Handler* ACS=NULL;
79 const Application& app=request.getApplication();
82 option=request.getParameter("acsIndex");
84 ACS=app.getAssertionConsumerServiceByIndex(atoi(option));
86 option = request.getParameter("target");
89 recoverRelayState(request, target, false);
92 // We're running as a "virtual handler" from within the filter.
93 // The target resource is the current one and everything else is defaulted.
94 target=request.getRequestURL();
97 m_log.debug("attempting to initiate session using SAML 1.x with provider (%s)", entityID);
99 // Use metadata to invoke the SSO service directly.
100 MetadataProvider* m=app.getMetadataProvider();
102 const EntityDescriptor* entity=m->getEntityDescriptor(entityID);
104 m_log.error("unable to locate metadata for provider (%s)", entityID);
105 return make_pair(false,0);
107 const IDPSSODescriptor* role=entity->getIDPSSODescriptor(shibspconstants::SHIB1_PROTOCOL_ENUM);
109 m_log.error("unable to locate Shibboleth-aware identity provider role for provider (%s)", entityID);
110 return make_pair(false,0);
112 const EndpointType* ep=EndpointManager<SingleSignOnService>(role->getSingleSignOnServices()).getByBinding(
113 shibspconstants::SHIB1_AUTHNREQUEST_PROFILE_URI
116 m_log.error("unable to locate compatible SSO service for provider (%s)", entityID);
117 return make_pair(false,0);
119 auto_ptr_char dest(ep->getLocation());
122 ACS = app.getDefaultAssertionConsumerService();
124 // Compute the ACS URL. We add the ACS location to the base handlerURL.
125 string ACSloc=request.getHandlerURL(target.c_str());
126 pair<bool,const char*> loc=ACS ? ACS->getString("Location") : pair<bool,const char*>(false,NULL);
127 if (loc.first) ACSloc+=loc.second;
130 // We may already have RelayState set if we looped back here,
131 // but just in case target is a resource, we reset it back.
132 option = request.getParameter("target");
136 preserveRelayState(request, target);
138 // Shib 1.x requires a target value.
143 sprintf(timebuf,"%u",time(NULL));
144 const URLEncoder* urlenc = XMLToolingConfig::getConfig().getURLEncoder();
145 string req=string(dest.get()) + (strchr(dest.get(),'?') ? '&' : '?') + "shire=" + urlenc->encode(ACSloc.c_str()) +
146 "&time=" + timebuf + "&target=" + urlenc->encode(target.c_str()) +
147 "&providerId=" + urlenc->encode(app.getString("entityID").second);
149 return make_pair(true, request.sendRedirect(req.c_str()));