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
40namespace Pt {
41
42namespace Http {
43
44class Request;
45class Reply;
46
51class PT_HTTP_API Authorization : private Pt::NonCopyable
52{
53 public:
56 virtual ~Authorization();
57
60 void beginAuthorize(const Request& req, Reply& reply);
61
65
69
70 protected:
74
77 void setReady();
78
81 virtual void onBeginAuthorize(const Request& req, Reply& reply) = 0;
82
85 virtual bool onEndAuthorize() = 0;
86
87 private:
88 Signal<Authorization&> _finished;
89};
90
144class PT_HTTP_API Authorizer : private Pt::NonCopyable
145{
146 public:
149 Authorizer(const std::string& realm);
150
153 Authorizer(const char* realm);
154
157 virtual ~Authorizer();
158
161 const std::string& realm() const;
162
165 Authorization* beginAuthorize(const Request& req, Reply& reply, bool& granted);
166
170
174
175 protected:
178 virtual Authorization* onBeginAuthorize(const Request& req, Reply& reply, bool& granted) = 0;
179
182 virtual void onReleaseAuthorization(Authorization* auth) = 0;
183
184 private:
185 atomic_t _useCount;
186 std::string _realm;
187};
188
193class PT_HTTP_API BasicAuthorizer : public Authorizer
194{
195 public:
198 BasicAuthorizer(const std::string& realm);
199
202 BasicAuthorizer(const char* realm);
203
207
208 protected:
209 virtual Authorization* onBeginAuthorize(const Request& req, Reply& reply, bool& granted);
210
213 virtual Authorization* onAuthorizeCredentials(const Credential& cred, bool& granted) = 0;
214};
215
216
221class PT_HTTP_API BasicUserListAuthorizer : public BasicAuthorizer
222{
223 public:
226 BasicUserListAuthorizer(const std::string& realm);
227
231
235
238 void setUser(const Credential& cred);
239
242 void removeUser(const std::string& user);
243
246 void removeUser(const char* user);
247
250 void clear();
251
252 protected:
253 virtual Authorization* onAuthorizeCredentials(const Credential& cred, bool& granted);
254
256
257 private:
258 System::Mutex _mutex;
259 std::map<std::string, std::string> _passwd;
260};
261
262} // namespace Http
263
264} // namespace Pt
265
266#endif // Pt_Http_Authorizer_h
Asynchronous HTTP authorization operation.
Definition Authorizer.h:52
Authorization()
Default constructor.
virtual bool onEndAuthorize()=0
End authorization for a reply.
virtual void onBeginAuthorize(const Request &req, Reply &reply)=0
Begin authorization for a reply.
void setReady()
Set authorization to ready state.
bool endAuthorize()
End authorization for a reply.
virtual ~Authorization()
Destructor.
void beginAuthorize(const Request &req, Reply &reply)
Begin authorization for a reply.
Signal< Authorization & > & finished()
Notifies that authorization is finished.
const std::string & realm() const
Returns the realm.
Authorizer(const char *realm)
Construct for a realm.
Authorizer(const std::string &realm)
Construct for a realm.
bool endAuthorization(Authorization *auth)
End authorization.
void cancelAuthorization(Authorization *auth)
Cancel a running authorization.
virtual ~Authorizer()
Destructor.
virtual Authorization * onBeginAuthorize(const Request &req, Reply &reply, bool &granted)=0
Begin authorization for a reply.
virtual void onReleaseAuthorization(Authorization *auth)=0
Release authorization operation.
Authorization * beginAuthorize(const Request &req, Reply &reply, bool &granted)
Begin authorization for a reply.
virtual Authorization * onBeginAuthorize(const Request &req, Reply &reply, bool &granted)
Begin authorization for a reply.
virtual Authorization * onAuthorizeCredentials(const Credential &cred, bool &granted)=0
Begin authorization using client credentials.
~BasicAuthorizer()
Destructor.
BasicAuthorizer(const std::string &realm)
Construct for a realm.
BasicAuthorizer(const char *realm)
Construct for a realm.
void removeUser(const char *user)
Remove user from list.
BasicUserListAuthorizer(const std::string &realm)
Construct for a realm.
virtual void onReleaseAuthorization(Authorization *auth)
Release authorization operation.
virtual Authorization * onAuthorizeCredentials(const Credential &cred, bool &granted)
Begin authorization using client credentials.
void setUser(const Credential &cred)
Set user credential.
void clear()
Clears all content.
void removeUser(const std::string &user)
Remove user from list.
BasicUserListAuthorizer(const char *realm)
Construct for a realm.
Credentials for authorization and authentication.
Definition Credentials.h:45
HTTP reply message.
Definition Reply.h:64
HTTP request message.
Definition Request.h:65
Base class that disables copy construction and assignment.
Definition NonCopyable.h:59
Multicast Signal to call multiple slots.
Definition Signal.h:190
Mutual exclusion device.
Definition Mutex.h:73
HTTP clients and servers.
Core module.
Definition Allocator.h:33
Atomic integers to be used with atomicity functions.
Definition Atomicity.h:52