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 
38 namespace Pt {
39 
40 namespace Http {
41 
42 class Request;
43 class Reply;
44 
48 {
49  public:
51  typedef std::map<std::string, Credential> Credentials;
52 
53  public:
59  Authentication(const std::string& name)
60  : _name(name)
61  {}
62 
68  Authentication(const char* name)
69  : _name(name)
70  {}
71 
74  virtual ~Authentication()
75  {}
76 
79  const std::string& name() const
80  { return _name; }
81 
87  virtual bool authenticate(const Credentials& credentials, Request& request, const Reply& reply) = 0;
88 
89  private:
90  std::string _name;
91 };
92 
95 class PT_HTTP_API BasicAuthentication : public Authentication
96 {
97  public:
101  : Authentication("basic")
102  {}
103 
107  {}
108 
109  void preAuthenticate(const Credential& credential, Request& request);
110 
111  // inheric docs
112  virtual bool authenticate(const Credentials& credentials, Request& request, const Reply& reply);
113 };
114 
117 class PT_HTTP_API Authenticator : private NonCopyable
118 {
119  public:
121  typedef std::map<std::string, Credential> Credentials;
122 
123  public:
126  Authenticator();
127 
130  ~Authenticator();
131 
134  void addAuthentication(Authentication& auth);
135 
138  void setCredential(const std::string& realm, const Credential& cred);
139 
142  bool authenticate(Request& request, const Reply& reply);
143 
144  private:
145  Credentials _credentials;
146  std::vector<Authentication*> _auths;
147  BasicAuthentication _basicAuth;
148 };
149 
150 } // namespace Http
151 
152 } // namespace Pt
153 
154 #endif // Pt_Http_Authenticator_h
virtual ~Authentication()
Destructor.
Definition: Authenticator.h:74
HTTP request message.
Definition: Request.h:43
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Client side authentication.
Definition: Authenticator.h:117
Credentials for authorization and authentication.
Definition: Credentials.h:42
Authentication(const std::string &name)
Construct with type name.
Definition: Authenticator.h:59
HTTP authentication for clients.
Definition: Authenticator.h:47
HTTP reply message.
Definition: Reply.h:43
BasicAuthentication()
Default Constructor.
Definition: Authenticator.h:100
virtual bool authenticate(const Credentials &credentials, Request &request, const Reply &reply)=0
Authenticate a request in response to a reply.
std::map< std::string, Credential > Credentials
Credentials for realms.
Definition: Authenticator.h:51
std::map< std::string, Credential > Credentials
Credentials for realms.
Definition: Authenticator.h:121
Authentication(const char *name)
Construct with type name.
Definition: Authenticator.h:68
const std::string & name() const
Returns the type name of the authentication.
Definition: Authenticator.h:79
Basic HTTP authentication for clients.
Definition: Authenticator.h:95
virtual ~BasicAuthentication()
Destructor.
Definition: Authenticator.h:106