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 
67 
68  protected:
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 
117 
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 
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 
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
Core module.
Definition: pt-gfx-images.dox:14
Server side authorization.
Definition: Authorizer.h:92
void removeUser(const std::string &user)
Remove user from list.
void cancelAuthorization(Authorization *auth)
Cancel a running authorization.
HTTP request message.
Definition: Request.h:44
HTTP authorization operation.
Definition: Authorizer.h:50
HTTP reply message.
Definition: Reply.h:44
BasicUserListAuthorizer(const char *realm)
Construct for a realm.
Authorization * beginAuthorize(const Request &req, Reply &reply, bool &granted)
Begin authorization for a reply.
BasicUserListAuthorizer(const std::string &realm)
Construct for a realm.
virtual ~Authorization()
Destructor.
~BasicAuthorizer()
Destructor.
Multicast Signal to call multiple slots.
Definition: Signal.h:110
virtual void onReleaseAuthorization(Authorization *auth)=0
Release authorization operation.
virtual void onReleaseAuthorization(Authorization *auth)
Release authorization operation.
const std::string & realm() const
Returns the realm.
BasicAuthorizer(const char *realm)
Construct for a realm.
virtual Authorization * onAuthorizeCredentials(const Credential &cred, bool &granted)
Begin authorization using client credentials.
void beginAuthorize(const Request &req, Reply &reply)
Begin authorization for a reply.
BasicAuthorizer(const std::string &realm)
Construct for a realm.
void clear()
Clears all content.
Server side basic HTTP authorization.
Definition: Authorizer.h:165
bool endAuthorization(Authorization *auth)
End authorization.
void setUser(const Credential &cred)
Set user credential.
virtual void onBeginAuthorize(const Request &req, Reply &reply)=0
Begin authorization for a reply.
bool endAuthorize()
End authorization for a reply.
~BasicUserListAuthorizer()
Destructor.
Mutual exclusion device.
Definition: Mutex.h:49
Credentials for authorization and authentication.
Definition: Credentials.h:43
void setReady()
Set authorization to ready state.
virtual ~Authorizer()
Destructor.
Authorization()
Default constructor.
virtual Authorization * onBeginAuthorize(const Request &req, Reply &reply, bool &granted)
Begin authorization for a reply.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Authorizer(const char *realm)
Construct for a realm.
Server side basic HTTP authorization.
Definition: Authorizer.h:139
Signal< Authorization & > & finished()
Notifies that authorization is finished.
Atomic integers to be used with atomicity functions.
Definition: Atomicity.h:51
virtual bool onEndAuthorize()=0
End authorization for a reply.
void removeUser(const char *user)
Remove user from list.
Authorizer(const std::string &realm)
Construct for a realm.
virtual Authorization * onBeginAuthorize(const Request &req, Reply &reply, bool &granted)=0
Begin authorization for a reply.
virtual Authorization * onAuthorizeCredentials(const Credential &cred, bool &granted)=0
Begin authorization using client credentials.