00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #include <boost/algorithm/string.hpp>
00011 #include <pion/http/auth.hpp>
00012 #include <pion/http/server.hpp>
00013 
00014 
00015 namespace pion {    
00016 namespace http {    
00017 
00018 
00019 
00020 
00021 void auth::add_restrict(const std::string& resource)
00022 {
00023     boost::mutex::scoped_lock resource_lock(m_resource_mutex);
00024     const std::string clean_resource(http::server::strip_trailing_slash(resource));
00025     m_restrict_list.insert(clean_resource);
00026     PION_LOG_INFO(m_logger, "Set authentication restrictions for HTTP resource: " << clean_resource);
00027 }
00028 
00029 void auth::add_permit(const std::string& resource)
00030 {
00031     boost::mutex::scoped_lock resource_lock(m_resource_mutex);
00032     const std::string clean_resource(http::server::strip_trailing_slash(resource));
00033     m_white_list.insert(clean_resource);
00034     PION_LOG_INFO(m_logger, "Set authentication permission for HTTP resource: " << clean_resource);
00035 }
00036 
00037 bool auth::need_authentication(const http::request_ptr& http_request_ptr) const
00038 {
00039     
00040     if (m_user_manager->empty())
00041         return false;
00042     
00043     
00044     std::string resource(http::server::strip_trailing_slash(http_request_ptr->get_resource()));
00045     
00046     boost::mutex::scoped_lock resource_lock(m_resource_mutex);
00047     
00048     
00049     if (m_restrict_list.empty())
00050         return false;
00051 
00052     
00053     if (find_resource(m_restrict_list, resource)) {
00054         
00055         if (m_white_list.empty())
00056             return true;
00057         
00058         return ( ! find_resource(m_white_list, resource) );
00059     }
00060     
00061     
00062     return false;
00063 }
00064 
00065 bool auth::find_resource(const resource_set_type& resource_set,
00066                             const std::string& resource) const
00067 {
00068     resource_set_type::const_iterator i = resource_set.upper_bound(resource);
00069     while (i != resource_set.begin()) {
00070         --i;
00071         
00072         if (i->empty() || resource.compare(0, i->size(), *i) == 0) {
00073             
00074             
00075             if (resource.size() == i->size() || resource[i->size()]=='/') {
00076                 return true;
00077             }
00078         }
00079     }
00080     return false;
00081 }
00082 
00083   
00084 }   
00085 }