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