root/trunk/whisperlib/net/base/user_authenticator.h

Revision 7, 4.1 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #ifndef __NET_BASE_USER_AUTHENTICATOR_H__
33 #define __NET_BASE_USER_AUTHENTICATOR_H__
34
35 // This is more of an interface for authenticating users..
36
37 #include <string>
38 #include <map>
39 #include <whisperlib/common/base/types.h>
40 #include <whisperlib/common/base/callback.h>
41
42 #include WHISPER_HASH_MAP_HEADER
43
44 namespace net {
45
46 class UserAuthenticator {
47  public:
48   explicit UserAuthenticator(const string& realm)
49       : realm_(realm) {
50   }
51   virtual ~UserAuthenticator() {
52   }
53
54   enum Answer {
55     Authenticated = 0,
56     BadUser,
57     BadPassword,
58     MissingCredentials,
59   };
60   typedef Callback1<Answer> AnswerCallback;
61   // The main authentication function - synchronous version
62   virtual Answer Authenticate(const string& user,
63                               const string& passwd) const = 0;
64
65   // The main authentication function - asynchronous version
66   virtual void Authenticate(const string& user,
67                             const string& passwd,
68                             AnswerCallback* answer_callback) const = 0;
69   const string& realm() const {
70     return realm_;
71   }
72  private:
73   const string realm_;
74
75   DISALLOW_EVIL_CONSTRUCTORS(UserAuthenticator);
76 };
77
78 // A simple authenticator - not the smartest thing you can do, and definitely
79 // not the most secure, as the passwords are kept in clear, in memory, but
80 // for something not very demanding, on a secure machine, this should be
81 // probably fine..
82 class SimpleUserAuthenticator : public UserAuthenticator {
83  public:
84   explicit SimpleUserAuthenticator(const string& realm)
85       : UserAuthenticator(realm) {
86   }
87   virtual ~SimpleUserAuthenticator() {
88   }
89
90   virtual Answer Authenticate(const string& user,
91                               const string& passwd) const {
92     hash_map<string, string>::const_iterator it = user_passwords_.find(user);
93     if ( it == user_passwords_.end() ) {
94       return BadUser;
95     }
96     if ( it->second != passwd ) {
97       return BadPassword;
98     }
99     return Authenticated;
100   }
101   virtual void Authenticate(const string& user,
102                             const string& passwd,
103                             Callback1<Answer>* result_callback) const {
104     result_callback->Run(Authenticate(user, passwd));
105   }
106
107   void set_user_password(const string& user,
108                          const string& passwd) {
109     user_passwords_[user] = passwd;
110   }
111   void remove_user(const string& user) {
112     user_passwords_.erase(user);
113   }
114   const hash_map<string, string>& user_passwords() const {
115     return user_passwords_;
116   }
117  private:
118   hash_map<string, string> user_passwords_;
119
120   DISALLOW_EVIL_CONSTRUCTORS(SimpleUserAuthenticator);
121 };
122 }
123
124 #endif  // __NET_BASE_USER_AUTHENTICATOR_H__
Note: See TracBrowser for help on using the browser.