Merge branch '2.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-sp into 2.x
[shibboleth/cpp-sp.git] / shibsp / Application.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  * Application.cpp
23  *
24  * Interface to a Shibboleth Application instance.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "SPRequest.h"
30 #include "ServiceProvider.h"
31 #include "attribute/Attribute.h"
32 #include "remoting/ListenerService.h"
33
34 #include <algorithm>
35 #include <xmltooling/util/Threads.h>
36
37 using namespace shibsp;
38 using namespace xmltooling;
39 using namespace std;
40
41 Application::Application(const ServiceProvider* sp) : m_sp(sp), m_lock(RWLock::create())
42 {
43 }
44
45 Application::~Application()
46 {
47     delete m_lock;
48 }
49
50 const ServiceProvider& Application::getServiceProvider() const
51 {
52     return *m_sp;
53 }
54
55 const char* Application::getId() const
56 {
57     pair<bool,const char*> ret = getString("id");
58     return ret.first ? ret.second : "default";
59 }
60
61 pair<string,const char*> Application::getCookieNameProps(const char* prefix, time_t* lifetime) const
62 {
63     static const char* defProps="; path=/; HttpOnly";
64
65     if (lifetime)
66         *lifetime = 0;
67     const PropertySet* props=getPropertySet("Sessions");
68     if (props) {
69         if (lifetime) {
70             pair<bool,unsigned int> lt = props->getUnsignedInt("cookieLifetime");
71             if (lt.first)
72                 *lifetime = lt.second;
73         }
74         pair<bool,const char*> p=props->getString("cookieProps");
75         if (!p.first)
76             p.second=defProps;
77         pair<bool,const char*> p2=props->getString("cookieName");
78         if (p2.first)
79             return make_pair(string(prefix) + p2.second,p.second);
80         return make_pair(string(prefix) + getHash(),p.second);
81     }
82
83     // Shouldn't happen, but just in case..
84     return pair<string,const char*>(prefix,defProps);
85 }
86
87 void Application::clearHeader(SPRequest& request, const char* rawname, const char* cginame) const
88 {
89     request.clearHeader(rawname, cginame);
90 }
91
92 void Application::setHeader(SPRequest& request, const char* name, const char* value) const
93 {
94     request.setHeader(name, value);
95 }
96
97 string Application::getSecureHeader(const SPRequest& request, const char* name) const
98 {
99     return request.getSecureHeader(name);
100 }
101
102 void Application::clearAttributeHeaders(SPRequest& request) const
103 {
104     if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
105         for (vector< pair<string,string> >::const_iterator i = m_unsetHeaders.begin(); i!=m_unsetHeaders.end(); ++i)
106             request.clearHeader(i->first.c_str(), i->second.c_str());
107         return;
108     }
109
110     m_lock->rdlock();
111     if (m_unsetHeaders.empty()) {
112         // No headers yet, so we have to request them from the remote half.
113         m_lock->unlock();
114         m_lock->wrlock();
115         if (m_unsetHeaders.empty()) {
116             SharedLock wrlock(m_lock, false);
117             string addr=string(getId()) + "::getHeaders::Application";
118             DDF out,in = DDF(addr.c_str());
119             DDFJanitor jin(in),jout(out);
120             out = getServiceProvider().getListenerService()->send(in);
121             if (out.islist()) {
122                 DDF header = out.first();
123                 while (header.isstring()) {
124                     m_unsetHeaders.push_back(pair<string,string>(header.name(),header.string()));
125                     header = out.next();
126                 }
127             }
128         }
129         else {
130             m_lock->unlock();
131         }
132         m_lock->rdlock();
133     }
134
135     // Now holding read lock.
136     SharedLock unsetLock(m_lock, false);
137     for (vector< pair<string,string> >::const_iterator i = m_unsetHeaders.begin(); i!=m_unsetHeaders.end(); ++i)
138         request.clearHeader(i->first.c_str(), i->second.c_str());
139 }
140
141 const Handler* Application::getAssertionConsumerServiceByProtocol(const XMLCh* protocol, const char* binding) const
142 {
143     auto_ptr_XMLCh b(binding);
144     const vector<const Handler*>& handlers = getAssertionConsumerServicesByBinding(b.get());
145     return handlers.empty() ? nullptr : handlers.front();
146 }