SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / handler / impl / CookieSessionInitiator.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * CookieSessionInitiator.cpp
23  * 
24  * Cookie-based IdP discovery.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "exceptions.h"
30 #include "handler/AbstractHandler.h"
31 #include "handler/SessionInitiator.h"
32
33 #ifndef SHIBSP_LITE
34 # include <saml/util/CommonDomainCookie.h>
35 #else
36 # include "lite/CommonDomainCookie.h"
37 #endif
38
39 #include <xmltooling/XMLToolingConfig.h>
40 #include <xmltooling/util/URLEncoder.h>
41
42 using namespace shibsp;
43 using namespace opensaml;
44 using namespace xmltooling;
45 using namespace std;
46
47 namespace shibsp {
48
49 #if defined (_MSC_VER)
50     #pragma warning( push )
51     #pragma warning( disable : 4250 )
52 #endif
53
54     class SHIBSP_DLLLOCAL CookieSessionInitiator : public SessionInitiator, public AbstractHandler
55     {
56     public:
57         CookieSessionInitiator(const DOMElement* e, const char* appId)
58             : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator.Cookie")),
59               m_followMultiple(getBool("followMultiple").second) {
60             m_supportedOptions.insert("isPassive");
61         }
62         virtual ~CookieSessionInitiator() {}
63         
64         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
65
66     private:
67         bool m_followMultiple;
68     };
69
70 #if defined (_MSC_VER)
71     #pragma warning( pop )
72 #endif
73
74     SessionInitiator* SHIBSP_DLLLOCAL CookieSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
75     {
76         return new CookieSessionInitiator(p.first, p.second);
77     }
78
79 };
80
81 pair<bool,long> CookieSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
82 {
83     // The IdP CANNOT be specified for us to run.
84     if (!entityID.empty() || !checkCompatibility(request, isHandler))
85         return make_pair(false,0L);
86
87     // If there's no entityID yet, we can check for cookie processing.
88     CommonDomainCookie cdc(request.getCookie(CommonDomainCookie::CDCName));
89     if ((m_followMultiple && cdc.get().size() > 0) || (!m_followMultiple && cdc.get().size() == 1)) {
90         entityID = cdc.get().back();
91         m_log.info("set entityID (%s) from IdP history cookie", entityID.c_str());
92     }
93     
94     return make_pair(false,0L);
95 }