Authenticator.h
1/*
2 * Copyright (C) 2012 by 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_Authenticator_h
30#define Pt_Http_Authenticator_h
31
32#include <Pt/Http/Api.h>
33#include <Pt/Http/Credentials.h>
34#include <Pt/NonCopyable.h>
35#include <string>
36#include <vector>
37
38namespace Pt {
39
40namespace Http {
41
42class Request;
43class Reply;
44
50{
51 public:
53 typedef std::map<std::string, Credential> Credentials;
54
55 public:
61 Authentication(const std::string& name)
62 : _name(name)
63 {}
64
70 Authentication(const char* name)
71 : _name(name)
72 {}
73
77 {}
78
81 const std::string& name() const
82 { return _name; }
83
89 virtual bool authenticate(const Credentials& credentials, Request& request, const Reply& reply) = 0;
90
91 private:
92 std::string _name;
93};
94
99class PT_HTTP_API BasicAuthentication : public Authentication
100{
101 public:
105 : Authentication("basic")
106 {}
107
111 {}
112
115 void preAuthenticate(const Credential& credential, Request& request);
116
119 virtual bool authenticate(const Credentials& credentials, Request& request, const Reply& reply);
120};
121
151class PT_HTTP_API Authenticator : private NonCopyable
152{
153 public:
155 typedef std::map<std::string, Credential> Credentials;
156
157 public:
161
165
169
172 void setCredential(const std::string& realm, const Credential& cred);
173
176 bool authenticate(Request& request, const Reply& reply);
177
178 private:
179 Credentials _credentials;
180 std::vector<Authentication*> _auths;
181 BasicAuthentication _basicAuth;
182};
183
184} // namespace Http
185
186} // namespace Pt
187
188#endif // Pt_Http_Authenticator_h
HTTP authentication method for clients.
Definition Authenticator.h:50
std::map< std::string, Credential > Credentials
Credentials for realms.
Definition Authenticator.h:53
Authentication(const std::string &name)
Construct with type name.
Definition Authenticator.h:61
const std::string & name() const
Returns the type name of the authentication.
Definition Authenticator.h:81
virtual ~Authentication()
Destructor.
Definition Authenticator.h:76
virtual bool authenticate(const Credentials &credentials, Request &request, const Reply &reply)=0
Authenticate a request in response to a reply.
Authentication(const char *name)
Construct with type name.
Definition Authenticator.h:70
std::map< std::string, Credential > Credentials
Credentials for realms.
Definition Authenticator.h:155
void addAuthentication(Authentication &auth)
Adds an authentication method.
void setCredential(const std::string &realm, const Credential &cred)
Set credential for a realm.
Authenticator()
Default Constructor.
~Authenticator()
Destructor.
bool authenticate(Request &request, const Reply &reply)
Authenticate a request in response to a reply.
Basic HTTP authentication for clients.
Definition Authenticator.h:100
virtual ~BasicAuthentication()
Destructor.
Definition Authenticator.h:110
void preAuthenticate(const Credential &credential, Request &request)
Writes basic credentials onto request before a challenge.
BasicAuthentication()
Default Constructor.
Definition Authenticator.h:104
virtual bool authenticate(const Credentials &credentials, Request &request, const Reply &reply)
Authenticates request from credentials in response to reply.
Credentials for authorization and authentication.
Definition Credentials.h:45
HTTP reply message.
Definition Reply.h:64
HTTP request message.
Definition Request.h:65
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
HTTP clients and servers.
Core module.
Definition Allocator.h:33