pion-net  4.0.9
HTTPAuth.hpp
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 #ifndef __PION_HTTPAUTH_HEADER__
11 #define __PION_HTTPAUTH_HEADER__
12 
13 #include <set>
14 #include <boost/noncopyable.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <pion/PionConfig.hpp>
17 #include <pion/PionLogger.hpp>
18 #include <pion/PionException.hpp>
19 #include <pion/net/PionUser.hpp>
20 #include <pion/net/TCPConnection.hpp>
21 #include <pion/net/HTTPRequest.hpp>
22 
23 
24 namespace pion { // begin namespace pion
25 namespace net { // begin namespace net (Pion Network Library)
26 
30 class PION_NET_API HTTPAuth :
31  private boost::noncopyable
32 {
33 public:
34 
37  public:
38  UnknownOptionException(const std::string& name)
39  : PionException("Option not recognized by authentication service: ", name) {}
40  };
41 
42 
44  HTTPAuth(PionUserManagerPtr userManager)
45  : m_logger(PION_GET_LOGGER("pion.net.HTTPAuth")),
46  m_user_manager(userManager)
47  {}
48 
50  virtual ~HTTPAuth() {}
51 
64  virtual bool handleRequest(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn) = 0;
65 
72  virtual void setOption(const std::string& name, const std::string& value) {
73  throw UnknownOptionException(name);
74  }
75 
81  void addRestrict(const std::string& resource);
82 
88  void addPermit(const std::string& resource);
89 
95  virtual bool addUser(std::string const &username, std::string const &password) {
96  return m_user_manager->addUser(username, password);
97  }
98 
104  virtual bool updateUser(std::string const &username, std::string const &password) {
105  return m_user_manager->updateUser(username, password);
106  }
107 
113  virtual bool removeUser(std::string const &username) {
114  return m_user_manager->removeUser(username);
115  };
116 
120  virtual PionUserPtr getUser(std::string const &username) {
121  return m_user_manager->getUser(username);
122  }
123 
124 
125 protected:
126 
128  typedef std::set<std::string> AuthResourceSet;
129 
130 
136  bool needAuthentication(HTTPRequestPtr const& http_request) const;
137 
146  bool findResource(const AuthResourceSet& resource_set,
147  const std::string& resource) const;
148 
150  inline void setLogger(PionLogger log_ptr) { m_logger = log_ptr; }
151 
152 
155 
157  PionUserManagerPtr m_user_manager;
158 
161 
164 
166  mutable boost::mutex m_resource_mutex;
167 };
168 
170 typedef boost::shared_ptr<HTTPAuth> HTTPAuthPtr;
171 
172 
173 } // end namespace net
174 } // end namespace pion
175 
176 #endif