auth.hpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_HTTP_AUTH_HEADER__
#define __PION_HTTP_AUTH_HEADER__
#include <set>
#include <map>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <pion/config.hpp>
#include <pion/error.hpp>
#include <pion/logger.hpp>
#include <pion/hash_map.hpp>
#include <pion/tcp/connection.hpp>
#include <pion/user.hpp>
#include <pion/http/request.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> // order important, otherwise compiling error under win32
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class PION_API auth :
private boost::noncopyable
{
public:
auth(user_manager_ptr userManager)
: m_logger(PION_GET_LOGGER("pion.http.auth")),
m_user_manager(userManager)
{}
virtual ~auth() {}
virtual bool handle_request(const http::request_ptr& http_request_ptr, const tcp::connection_ptr& tcp_conn) = 0;
virtual void set_option(const std::string& name, const std::string& value) {
BOOST_THROW_EXCEPTION( error::bad_arg() << error::errinfo_arg_name(name) );
}
void add_restrict(const std::string& resource);
void add_permit(const std::string& resource);
virtual bool add_user(std::string const &username, std::string const &password) {
return m_user_manager->add_user(username, password);
}
virtual bool update_user(std::string const &username, std::string const &password) {
return m_user_manager->update_user(username, password);
}
virtual bool remove_user(std::string const &username) {
return m_user_manager->remove_user(username);
};
virtual user_ptr get_user(std::string const &username) {
return m_user_manager->get_user(username);
}
protected:
typedef std::set<std::string> resource_set_type;
typedef std::map<std::string,std::pair<boost::posix_time::ptime,user_ptr> > user_cache_type;
bool need_authentication(http::request_ptr const& http_request_ptr) const;
bool find_resource(const resource_set_type& resource_set,
const std::string& resource) const;
inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
mutable logger m_logger;
user_manager_ptr m_user_manager;
resource_set_type m_restrict_list;
resource_set_type m_white_list;
mutable boost::mutex m_resource_mutex;
};
typedef boost::shared_ptr<auth> auth_ptr;
} // end namespace http
} // end namespace pion
#endif
pion::http::auth::update_user
virtual bool update_user(std::string const &username, std::string const &password)
Definition: auth.hpp:100
pion::http::auth::remove_user
virtual bool remove_user(std::string const &username)
Definition: auth.hpp:109
pion::http::auth::~auth
virtual ~auth()
virtual destructor
Definition: auth.hpp:46
pion::http::auth::add_user
virtual bool add_user(std::string const &username, std::string const &password)
Definition: auth.hpp:91
pion::http::auth::m_user_manager
user_manager_ptr m_user_manager
container used to manager user objects
Definition: auth.hpp:156
pion::http::auth::m_white_list
resource_set_type m_white_list
collection of resources that do NOT require authentication
Definition: auth.hpp:162
pion::http::auth::m_resource_mutex
boost::mutex m_resource_mutex
mutex used to protect access to the resources
Definition: auth.hpp:165
pion::http::auth
Definition: auth.hpp:34
pion::http::auth::user_cache_type
std::map< std::string, std::pair< boost::posix_time::ptime, user_ptr > > user_cache_type
data type used to map authentication credentials to user objects
Definition: auth.hpp:127
pion::http::auth::set_option
virtual void set_option(const std::string &name, const std::string &value)
Definition: auth.hpp:68
pion::http::auth::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: auth.hpp:149
pion::http::auth::m_logger
logger m_logger
primary logging interface used by this class
Definition: auth.hpp:153
pion::logger
Definition: logger.hpp:238
pion
Definition: admin_rights.hpp:19
pion::http::auth::get_user
virtual user_ptr get_user(std::string const &username)
Definition: auth.hpp:116
pion::error::bad_arg
exception thrown for an invalid configuration argument or option
Definition: error.hpp:132
pion::http::auth::auth
auth(user_manager_ptr userManager)
default constructor
Definition: auth.hpp:40
pion::http::auth::resource_set_type
std::set< std::string > resource_set_type
data type for a set of resources to be authenticated
Definition: auth.hpp:124
pion::http::auth::m_restrict_list
resource_set_type m_restrict_list
collection of resources that require authentication
Definition: auth.hpp:159