Clean up existing handlers a bit.
[shibboleth/sp.git] / shibsp / handler / impl / CookieSessionInitiator.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  * CookieSessionInitiator.cpp
19  * 
20  * Cookie-based IdP discovery.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "SPRequest.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/SessionInitiator.h"
29
30 #ifndef SHIBSP_LITE
31 # include <saml/util/CommonDomainCookie.h>
32 #else
33 # include "lite/CommonDomainCookie.h"
34 #endif
35
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/util/URLEncoder.h>
38
39 using namespace shibsp;
40 using namespace opensaml;
41 using namespace xmltooling;
42 using namespace std;
43
44 namespace shibsp {
45
46 #if defined (_MSC_VER)
47     #pragma warning( push )
48     #pragma warning( disable : 4250 )
49 #endif
50
51     class SHIBSP_DLLLOCAL CookieSessionInitiator : public SessionInitiator, public AbstractHandler
52     {
53     public:
54         CookieSessionInitiator(const DOMElement* e, const char* appId)
55             : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Cookie")), m_followMultiple(getBool("followMultiple").second) {
56         }
57         virtual ~CookieSessionInitiator() {}
58         
59         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
60
61     private:
62         bool m_followMultiple;
63     };
64
65 #if defined (_MSC_VER)
66     #pragma warning( pop )
67 #endif
68
69     SessionInitiator* SHIBSP_DLLLOCAL CookieSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
70     {
71         return new CookieSessionInitiator(p.first, p.second);
72     }
73
74 };
75
76 pair<bool,long> CookieSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
77 {
78     // The IdP CANNOT be specified for us to run.
79     if (!entityID.empty())
80         return make_pair(false,0L);
81
82     // If there's no entityID yet, we can check for cookie processing.
83     CommonDomainCookie cdc(request.getCookie(CommonDomainCookie::CDCName));
84     if ((m_followMultiple && cdc.get().size() > 0) || (!m_followMultiple && cdc.get().size() == 1)) {
85         entityID = cdc.get().back();
86         m_log.info("set entityID (%s) from IdP history cookie", entityID.c_str());
87     }
88     
89     return make_pair(false,0L);
90 }