Set signing/digest algorithms using new settings.
[shibboleth/sp.git] / shibsp / handler / impl / Shib1SessionInitiator.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * Shib1SessionInitiator.cpp
19  * 
20  * Shibboleth 1.x AuthnRequest support.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "SPRequest.h"
26 #include "handler/AbstractHandler.h"
27 #include "handler/SessionInitiator.h"
28 #include "util/SPConstants.h"
29
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>
34
35 using namespace shibsp;
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace log4cpp;
40 using namespace std;
41
42 namespace shibsp {
43
44 #if defined (_MSC_VER)
45     #pragma warning( push )
46     #pragma warning( disable : 4250 )
47 #endif
48
49     class SHIBSP_DLLLOCAL Shib1SessionInitiator : public SessionInitiator, public AbstractHandler
50     {
51     public:
52         Shib1SessionInitiator(const DOMElement* e, const char* appId)
53             : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator")) {}
54         virtual ~Shib1SessionInitiator() {}
55         
56         pair<bool,long> run(SPRequest& request, const char* entityID=NULL, bool isHandler=true) const;
57     };
58
59 #if defined (_MSC_VER)
60     #pragma warning( pop )
61 #endif
62
63     SessionInitiator* SHIBSP_DLLLOCAL Shib1SessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
64     {
65         return new Shib1SessionInitiator(p.first, p.second);
66     }
67
68 };
69
70 pair<bool,long> Shib1SessionInitiator::run(SPRequest& request, const char* entityID, bool isHandler) const
71 {
72     // We have to know the IdP to function.
73     if (!entityID || !*entityID)
74         return make_pair(false,0);
75
76     string target;
77     const char* option;
78     const Handler* ACS=NULL;
79     const Application& app=request.getApplication();
80
81     if (isHandler) {
82         option=request.getParameter("acsIndex");
83         if (option)
84             ACS=app.getAssertionConsumerServiceByIndex(atoi(option));
85
86         option = request.getParameter("target");
87         if (option)
88             target = option;
89         recoverRelayState(request, target, false);
90     }
91     else {
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();
95     }
96         
97     m_log.debug("attempting to initiate session using SAML 1.x with provider (%s)", entityID);
98
99     // Use metadata to invoke the SSO service directly.
100     MetadataProvider* m=app.getMetadataProvider();
101     Locker locker(m);
102     const EntityDescriptor* entity=m->getEntityDescriptor(entityID);
103     if (!entity) {
104         m_log.error("unable to locate metadata for provider (%s)", entityID);
105         return make_pair(false,0);
106     }
107     const IDPSSODescriptor* role=entity->getIDPSSODescriptor(shibspconstants::SHIB1_PROTOCOL_ENUM);
108     if (!role) {
109         m_log.error("unable to locate Shibboleth-aware identity provider role for provider (%s)", entityID);
110         return make_pair(false,0);
111     }
112     const EndpointType* ep=EndpointManager<SingleSignOnService>(role->getSingleSignOnServices()).getByBinding(
113         shibspconstants::SHIB1_AUTHNREQUEST_PROFILE_URI
114         );
115     if (!ep) {
116         m_log.error("unable to locate compatible SSO service for provider (%s)", entityID);
117         return make_pair(false,0);
118     }
119     auto_ptr_char dest(ep->getLocation());
120
121     if (!ACS)
122         ACS = app.getDefaultAssertionConsumerService();
123
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;
128
129     if (isHandler) {
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");
133         if (option)
134             target = option;
135     }
136     preserveRelayState(request, target);
137
138     // Shib 1.x requires a target value.
139     if (target.empty())
140         target = "default";
141
142     char timebuf[16];
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);
148
149     return make_pair(true, request.sendRedirect(req.c_str()));
150 }