pion  5.0.6
http_auth.cpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
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/http/auth.hpp>
12 #include <pion/http/server.hpp>
13 
14 
15 namespace pion { // begin namespace pion
16 namespace http { // begin namespace http
17 
18 
19 // auth member functions
20 
21 void auth::add_restrict(const std::string& resource)
22 {
23  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
24  const std::string clean_resource(http::server::strip_trailing_slash(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 auth::add_permit(const std::string& resource)
30 {
31  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
32  const std::string clean_resource(http::server::strip_trailing_slash(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 auth::need_authentication(const http::request_ptr& http_request_ptr) 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(http::server::strip_trailing_slash(http_request_ptr->get_resource()));
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 (find_resource(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 ( ! find_resource(m_white_list, resource) );
59  }
60 
61  // resource not found in restricted list
62  return false;
63 }
64 
65 bool auth::find_resource(const resource_set_type& resource_set,
66  const std::string& resource) const
67 {
68  resource_set_type::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 http
85 } // end namespace pion
user_manager_ptr m_user_manager
container used to manager user objects
Definition: auth.hpp:156
resource_set_type m_white_list
collection of resources that do NOT require authentication
Definition: auth.hpp:162
bool find_resource(const resource_set_type &resource_set, const std::string &resource) const
Definition: http_auth.cpp:65
boost::mutex m_resource_mutex
mutex used to protect access to the resources
Definition: auth.hpp:165
static std::string strip_trailing_slash(const std::string &str)
Definition: server.hpp:160
logger m_logger
primary logging interface used by this class
Definition: auth.hpp:153
bool need_authentication(http::request_ptr const &http_request_ptr) const
Definition: http_auth.cpp:37
void add_permit(const std::string &resource)
Definition: http_auth.cpp:29
void add_restrict(const std::string &resource)
Definition: http_auth.cpp:21
std::set< std::string > resource_set_type
data type for a set of resources to be authenticated
Definition: auth.hpp:124
resource_set_type m_restrict_list
collection of resources that require authentication
Definition: auth.hpp:159