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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * Handler for dumping information about an active session.
28 #include "Application.h"
29 #include "exceptions.h"
30 #include "ServiceProvider.h"
31 #include "SessionCache.h"
32 #include "SPRequest.h"
33 #include "attribute/Attribute.h"
34 #include "handler/AbstractHandler.h"
38 using namespace shibsp;
39 using namespace xmltooling;
44 #if defined (_MSC_VER)
45 #pragma warning( push )
46 #pragma warning( disable : 4250 )
49 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
52 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
57 acceptNode(const DOMNode* node) const {
62 static SHIBSP_DLLLOCAL Blocker g_Blocker;
64 class SHIBSP_API SessionHandler : public AbstractHandler
67 SessionHandler(const DOMElement* e, const char* appId);
68 virtual ~SessionHandler() {}
70 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
77 #if defined (_MSC_VER)
78 #pragma warning( pop )
81 Handler* SHIBSP_DLLLOCAL SessionHandlerFactory(const pair<const DOMElement*,const char*>& p)
83 return new SessionHandler(p.first, p.second);
88 SessionHandler::SessionHandler(const DOMElement* e, const char* appId)
89 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionHandler"), &g_Blocker), m_values(false)
91 pair<bool,const char*> acl = getString("acl");
93 string aclbuf=acl.second;
95 for (unsigned int i=0; i < aclbuf.length(); i++) {
96 if (aclbuf.at(i)==' ') {
97 m_acl.insert(aclbuf.substr(j, i-j));
101 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
104 pair<bool,bool> flag = getBool("showAttributeValues");
106 m_values = flag.second;
109 pair<bool,long> SessionHandler::run(SPRequest& request, bool isHandler) const
111 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
112 m_log.error("session handler request blocked from invalid address (%s)", request.getRemoteAddr().c_str());
113 istringstream msg("Session Handler Blocked");
114 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
118 s << "<html><head><title>Session Summary</title></head><body><pre>" << endl;
120 Session* session = nullptr;
122 session = request.getSession();
124 s << "A valid session was not found.</pre></body></html>" << endl;
125 request.setContentType("text/html");
126 request.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
127 request.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
128 return make_pair(true, request.sendResponse(s));
131 catch (exception& ex) {
132 s << "Exception while retrieving active session:" << endl
133 << '\t' << ex.what() << "</pre></body></html>" << endl;
134 request.setContentType("text/html");
135 request.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
136 request.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
137 return make_pair(true, request.sendResponse(s));
140 s << "<u>Miscellaneous</u>" << endl;
142 s << "<strong>Client Address:</strong> " << (session->getClientAddress() ? session->getClientAddress() : "(none)") << endl;
143 s << "<strong>Identity Provider:</strong> " << (session->getEntityID() ? session->getEntityID() : "(none)") << endl;
144 s << "<strong>SSO Protocol:</strong> " << (session->getProtocol() ? session->getProtocol() : "(none)") << endl;
145 s << "<strong>Authentication Time:</strong> " << (session->getAuthnInstant() ? session->getAuthnInstant() : "(none)") << endl;
146 s << "<strong>Authentication Context Class:</strong> " << (session->getAuthnContextClassRef() ? session->getAuthnContextClassRef() : "(none)") << endl;
147 s << "<strong>Authentication Context Decl:</strong> " << (session->getAuthnContextDeclRef() ? session->getAuthnContextDeclRef() : "(none)") << endl;
148 s << "<strong>Session Expiration (barring inactivity):</strong> ";
149 if (session->getExpiration())
150 s << ((session->getExpiration() - time(nullptr)) / 60) << " minute(s)" << endl;
152 s << "Infinite" << endl;
154 s << endl << "<u>Attributes</u>" << endl;
157 vector<string>::size_type count=0;
158 const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
159 for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a != attributes.end(); ++a) {
160 if (a->first != key) {
161 if (a != attributes.begin()) {
165 s << count << " value(s)" << endl;
169 s << "<strong>" << a->first << "</strong>: ";
173 const vector<string>& vals = a->second->getSerializedValues();
174 for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
175 if (v != vals.begin() || a->first == key)
177 string::size_type pos = v->find_first_of(';',string::size_type(0));
178 if (pos!=string::npos) {
180 for (; pos != string::npos; pos = value.find_first_of(';',pos)) {
181 value.insert(pos, "\\");
192 count += a->second->getSerializedValues().size();
196 if (!m_values && !attributes.empty())
197 s << count << " value(s)" << endl;
199 s << "</pre></body></html>";
200 request.setContentType("text/html; charset=UTF-8");
201 request.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
202 request.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
203 return make_pair(true, request.sendResponse(s));