Authorizer.h
1 /*
2  * Copyright (C) 2012 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Http_Authorizer_h
30 #define Pt_Http_Authorizer_h
31 
32 #include <Pt/Http/Api.h>
33 #include <Pt/Http/Credentials.h>
34 #include <Pt/System/Mutex.h>
35 #include <Pt/Atomicity.h>
36 #include <Pt/Signal.h>
37 #include <string>
38 #include <map>
39 
40 namespace Pt {
41 
42 namespace Http {
43 
44 class Request;
45 class Reply;
46 
49 class PT_HTTP_API Authorization : private Pt::NonCopyable
50 {
51  public:
54  virtual ~Authorization();
55 
58  void beginAuthorize(const Request& req, Reply& reply);
59 
62  bool endAuthorize();
63 
66  Signal<Authorization&>& finished();
67 
68  protected:
71  Authorization();
72 
75  void setReady();
76 
79  virtual void onBeginAuthorize(const Request& req, Reply& reply) = 0;
80 
83  virtual bool onEndAuthorize() = 0;
84 
85  private:
86  Signal<Authorization&> _finished;
87 };
88 
91 class PT_HTTP_API Authorizer : private Pt::NonCopyable
92 {
93  public:
96  Authorizer(const std::string& realm);
97 
100  Authorizer(const char* realm);
101 
104  virtual ~Authorizer();
105 
108  const std::string& realm() const;
109 
112  Authorization* beginAuthorize(const Request& req, Reply& reply, bool& granted);
113 
116  bool endAuthorization(Authorization* auth);
117 
120  void cancelAuthorization(Authorization* auth);
121 
122  protected:
125  virtual Authorization* onBeginAuthorize(const Request& req, Reply& reply, bool& granted) = 0;
126 
129  virtual void onReleaseAuthorization(Authorization* auth) = 0;
130 
131  private:
132  atomic_t _useCount;
133  std::string _realm;
134 };
135 
138 class PT_HTTP_API BasicAuthorizer : public Authorizer
139 {
140  public:
143  BasicAuthorizer(const std::string& realm);
144 
147  BasicAuthorizer(const char* realm);
148 
151  ~BasicAuthorizer();
152 
153  protected:
154  virtual Authorization* onBeginAuthorize(const Request& req, Reply& reply, bool& granted);
155 
158  virtual Authorization* onAuthorizeCredentials(const Credential& cred, bool& granted) = 0;
159 };
160 
161 
164 class PT_HTTP_API BasicUserListAuthorizer : public BasicAuthorizer
165 {
166  public:
169  BasicUserListAuthorizer(const std::string& realm);
170 
173  BasicUserListAuthorizer(const char* realm);
174 
178 
181  void setUser(const Credential& cred);
182 
185  void removeUser(const std::string& user);
186 
189  void removeUser(const char* user);
190 
193  void clear();
194 
195  protected:
196  virtual Authorization* onAuthorizeCredentials(const Credential& cred, bool& granted);
197 
198  virtual void onReleaseAuthorization(Authorization* auth);
199 
200  private:
201  System::Mutex _mutex;
202  std::map<std::string, std::string> _passwd;
203 };
204 
205 } // namespace Http
206 
207 } // namespace Pt
208 
209 #endif // Pt_Http_Authorizer_h
Server side authorization.
Definition: Authorizer.h:91
HTTP request message.
Definition: Request.h:43
Atomic integers to be used with atomicity functions.
Definition: Atomicity.h:50
Protects derived classes from being copied.
Definition: NonCopyable.h:54
HTTP authorization operation.
Definition: Authorizer.h:49
Credentials for authorization and authentication.
Definition: Credentials.h:42
Server side basic HTTP authorization.
Definition: Authorizer.h:164
Server side basic HTTP authorization.
Definition: Authorizer.h:138
HTTP reply message.
Definition: Reply.h:43
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Mutual exclusion device.
Definition: Mutex.h:48