pion-net  4.0.9
HTTPAuth.cpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #include <boost/algorithm/string.hpp>
11 #include <pion/net/HTTPAuth.hpp>
12 #include <pion/net/HTTPServer.hpp>
13 
14 
15 namespace pion { // begin namespace pion
16 namespace net { // begin namespace net (Pion Network Library)
17 
18 
19 // HTTPAuth member functions
20 
21 void HTTPAuth::addRestrict(const std::string& resource)
22 {
23  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
24  const std::string clean_resource(HTTPServer::stripTrailingSlash(resource));
25  m_restrict_list.insert(clean_resource);
26  PION_LOG_INFO(m_logger, "Set authentication restrictions for HTTP resource: " << clean_resource);
27 }
28 
29 void HTTPAuth::addPermit(const std::string& resource)
30 {
31  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
32  const std::string clean_resource(HTTPServer::stripTrailingSlash(resource));
33  m_white_list.insert(clean_resource);
34  PION_LOG_INFO(m_logger, "Set authentication permission for HTTP resource: " << clean_resource);
35 }
36 
37 bool HTTPAuth::needAuthentication(const HTTPRequestPtr& http_request) const
38 {
39  // if no users are defined, authentication is never required
40  if (m_user_manager->empty())
41  return false;
42 
43  // strip off trailing slash if the request has one
44  std::string resource(HTTPServer::stripTrailingSlash(http_request->getResource()));
45 
46  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
47 
48  // just return false if restricted list is empty
49  if (m_restrict_list.empty())
50  return false;
51 
52  // try to find resource in restricted list
53  if (findResource(m_restrict_list, resource)) {
54  // return true if white list is empty
55  if (m_white_list.empty())
56  return true;
57  // return false if found in white list, or true if not found
58  return ( ! findResource(m_white_list, resource) );
59  }
60 
61  // resource not found in restricted list
62  return false;
63 }
64 
65 bool HTTPAuth::findResource(const AuthResourceSet& resource_set,
66  const std::string& resource) const
67 {
68  AuthResourceSet::const_iterator i = resource_set.upper_bound(resource);
69  while (i != resource_set.begin()) {
70  --i;
71  // check for a match if the first part of the strings match
72  if (i->empty() || resource.compare(0, i->size(), *i) == 0) {
73  // only if the resource matches exactly
74  // or if resource is followed first with a '/' character
75  if (resource.size() == i->size() || resource[i->size()]=='/') {
76  return true;
77  }
78  }
79  }
80  return false;
81 }
82 
83 
84 } // end namespace net
85 } // end namespace pion
PionUserManagerPtr m_user_manager
container used to manager user objects
Definition: HTTPAuth.hpp:157
AuthResourceSet m_white_list
collection of resources that do NOT require authentication
Definition: HTTPAuth.hpp:163
PionLogger m_logger
primary logging interface used by this class
Definition: HTTPAuth.hpp:154
static std::string stripTrailingSlash(const std::string &str)
Definition: HTTPServer.hpp:159
bool findResource(const AuthResourceSet &resource_set, const std::string &resource) const
Definition: HTTPAuth.cpp:65
void addPermit(const std::string &resource)
Definition: HTTPAuth.cpp:29
boost::mutex m_resource_mutex
mutex used to protect access to the resources
Definition: HTTPAuth.hpp:166
AuthResourceSet m_restrict_list
collection of resources that require authentication
Definition: HTTPAuth.hpp:160
void addRestrict(const std::string &resource)
Definition: HTTPAuth.cpp:21
bool needAuthentication(HTTPRequestPtr const &http_request) const
Definition: HTTPAuth.cpp:37
std::set< std::string > AuthResourceSet
data type for a set of resources to be authenticated
Definition: HTTPAuth.hpp:128