Robot Raconteur Core C++ Library
Security.h
Go to the documentation of this file.
1 
25 
26 #pragma once
27 
28 namespace RobotRaconteur
29 {
30 class ROBOTRACONTEUR_CORE_API UserAuthenticator;
31 class ROBOTRACONTEUR_CORE_API ServerContext;
32 
53 class ROBOTRACONTEUR_CORE_API ServiceSecurityPolicy
54 {
55  public:
56  RR_SHARED_PTR<UserAuthenticator> Authenticator;
57  std::map<std::string, std::string> Policies;
58 
66 
73  ServiceSecurityPolicy(const RR_SHARED_PTR<UserAuthenticator>& Authenticator,
74  const std::map<std::string, std::string>& Policies);
75 };
76 
86 class ROBOTRACONTEUR_CORE_API AuthenticatedUser
87 {
88  private:
89  std::string m_Username;
90  std::vector<std::string> m_Privileges;
91  std::vector<std::string> m_Properties;
92  boost::posix_time::ptime m_LoginTime;
93  boost::posix_time::ptime m_LastAccessTime;
94 
95  boost::mutex m_LastAccessTime_lock;
96 
97  RR_WEAK_PTR<ServerContext> context;
98 
99  public:
101  virtual std::string GetUsername();
102 
104  virtual std::vector<std::string> GetPrivileges();
105 
107  virtual std::vector<std::string> GetProperties();
108 
110  virtual boost::posix_time::ptime GetLoginTime();
111 
113  virtual boost::posix_time::ptime GetLastAccessTime();
114 
132  AuthenticatedUser(boost::string_ref username, const std::vector<std::string>& privileges,
133  const std::vector<std::string>& properties, const RR_SHARED_PTR<ServerContext>& context);
134 
136  virtual void UpdateLastAccess();
137 
138  virtual ~AuthenticatedUser() {}
139 };
140 
150 class ROBOTRACONTEUR_CORE_API UserAuthenticator
151 {
152  public:
171  virtual RR_SHARED_PTR<AuthenticatedUser> AuthenticateUser(
172  boost::string_ref username, const std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials,
173  const RR_SHARED_PTR<ServerContext>& context, const RR_SHARED_PTR<ITransportConnection>& transport) = 0;
174 
175  virtual ~UserAuthenticator() {}
176 };
177 
200 class ROBOTRACONTEUR_CORE_API PasswordFileUserAuthenticator : public UserAuthenticator
201 {
202 
203  private:
204  class ROBOTRACONTEUR_CORE_API User
205  {
206  public:
207  std::string username;
208  std::string passwordhash;
209  std::vector<std::string> privileges;
210  std::vector<NodeID> allowed_client_nodeid;
211  };
212 
213  std::map<std::string, RR_SHARED_PTR<User> > validusers;
214  bool require_verified_client;
215 
216  public:
222  PasswordFileUserAuthenticator(std::istream& file, bool require_verified_client = false);
223 
229  PasswordFileUserAuthenticator(boost::string_ref data, bool require_verified_client = false);
230 
231  RR_OVIRTUAL ~PasswordFileUserAuthenticator() RR_OVERRIDE {}
232 
233  private:
234  void load(boost::string_ref data);
235 
236  public:
237  RR_OVIRTUAL RR_SHARED_PTR<AuthenticatedUser> AuthenticateUser(
238  boost::string_ref username, const std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials,
239  const RR_SHARED_PTR<ServerContext>& context, const RR_SHARED_PTR<ITransportConnection>& transport) RR_OVERRIDE;
240 
241  static std::string MD5Hash(boost::string_ref text);
242 };
243 
244 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
246 using ServiceSecurityPolicyPtr = RR_SHARED_PTR<ServiceSecurityPolicy>;
248 using AuthenticatedUserPtr = RR_SHARED_PTR<AuthenticatedUser>;
250 using UserAuthenticatorPtr = RR_SHARED_PTR<UserAuthenticator>;
252 using PasswordFileUserAuthenticatorPtr = RR_SHARED_PTR<PasswordFileUserAuthenticator>;
253 #endif
254 
255 } // namespace RobotRaconteur
boost::shared_ptr< ServiceSecurityPolicy > ServiceSecurityPolicyPtr
Convenience alias for ServiceSecurityPolicy shared_ptr.
Definition: Security.h:246
boost::shared_ptr< PasswordFileUserAuthenticator > PasswordFileUserAuthenticatorPtr
Convenience alias for PasswordFileUserAuthenticator shared_ptr.
Definition: Security.h:252
boost::shared_ptr< AuthenticatedUser > AuthenticatedUserPtr
Convenience alias for AuthenticatedUser shared_ptr.
Definition: Security.h:248
boost::shared_ptr< UserAuthenticator > UserAuthenticatorPtr
Convenience alias for UserAuthenticator shared_ptr.
Definition: Security.h:250
Class representing an authenticated user.
Definition: Security.h:87
virtual boost::posix_time::ptime GetLoginTime()
The user login time.
virtual void UpdateLastAccess()
Update the last access time to now.
virtual std::vector< std::string > GetPrivileges()
The user privileges.
virtual std::string GetUsername()
The authenticated username.
virtual boost::posix_time::ptime GetLastAccessTime()
The user last access time.
virtual std::vector< std::string > GetProperties()
The user properties.
AuthenticatedUser(boost::string_ref username, const std::vector< std::string > &privileges, const std::vector< std::string > &properties, const boost::shared_ptr< ServerContext > &context)
Construct a new AuthenticatedUser.
Simple authenticator using a list of username, password hash, and privileges stored in a file or stri...
Definition: Security.h:201
PasswordFileUserAuthenticator(std::istream &file, bool require_verified_client=false)
Construct a new PasswordFileUserAuthenticator using text supplied as a stream.
PasswordFileUserAuthenticator(boost::string_ref data, bool require_verified_client=false)
Construct a new PasswordFileUserAuthenticator using text supplied as a string.
Security policy for Robot Raconteur service.
Definition: Security.h:54
ServiceSecurityPolicy()
Construct an empty ServiceSecurityPolicy.
ServiceSecurityPolicy(const boost::shared_ptr< UserAuthenticator > &Authenticator, const std::map< std::string, std::string > &Policies)
Construct a ServiceSecurityPolicy.
Base class for user authenticators.
Definition: Security.h:151
virtual boost::shared_ptr< AuthenticatedUser > AuthenticateUser(boost::string_ref username, const std::map< std::string, boost::intrusive_ptr< RRValue > > &credentials, const boost::shared_ptr< ServerContext > &context, const boost::shared_ptr< ITransportConnection > &transport)=0
Authenticate a user using username and credentials.