Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsBasicAuth.cpp
Go to the documentation of this file.
1 
14 #include "WsBasicAuth.h"
15 #include <Include/WsGlobalConfig.h>
16 #include <boost/tokenizer.hpp>
17 #include <stdlib.h>
18 using namespace std;
19 
20 extern "C" {
21  void WsBasicAuthInit(void)
22  {
23  LOG(DEBUG) << "WsBasicAuth::WsBasicAuthInit() : Preparing required libraries : libgdcore ";
24  void* hndl = dlopen("libgdcore.so", RTLD_NOW | RTLD_GLOBAL);
25  if ( hndl == NULL )
26  LOG(ERROR) << "WsBasicAuth::WsBasicAuthInit() : Cannot load libgdcore.so shared library! " << dlerror();
27  }
28 }
29 
30 
32 {
33  string guest = WsGlobalProperties::instance()->get("global", "guest_group", "guest");
34  m_allGroups.insert(guest);
35 }
36 
37 int WsBasicAuth::authentify(const string& uid, const string& pass, const std::string& ip)
38 {
39  m_uid = uid;
40  m_ip = ip;
41  string guest = WsGlobalProperties::instance()->get("global", "guest_group", "guest");
42  /* Generate unique id for the guest */
43  int ran = rand() % 10000000 + 1;
44  m_uid = m_uid + "-" + boost::lexical_cast<string>(ran);
45  m_groups.insert(guest);
46  return ErrorCode::Success;
47 }
48 
50 {
51  return m_uid;
52 }
53 
55 {
56  return m_name;
57 }
58 
60 {
61  return m_surname;
62 }
63 
65 {
66  return m_email;
67 }
68 
70 {
71  return m_groups;
72 }
73 
75 {
76  return m_allGroups;
77 }
78 
79 bool WsBasicAuth::ipValid(string ip)
80 {
81  string ipMask = WsGlobalProperties::instance()->get("ldap", "ip_mask", "*");
82  if (ipMask == "*")
83  return true;
84  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
85  boost::char_separator<char> sep(".");
86  tokenizer maskTok(ipMask, sep);
87  tokenizer uipTok(ip, sep);
88  tokenizer::iterator mask_iter = maskTok.begin();
89  tokenizer::iterator ip_iter = uipTok.begin();
90  for (; ip_iter != uipTok.end(); ++mask_iter, ++ip_iter) {
91  if (mask_iter == maskTok.end())
92  return false;
93  if (*mask_iter == "*")
94  continue;
95  if (*ip_iter != *mask_iter)
96  return false;
97  }
98  return true;
99 }
WsBasicAuth()
Constructor.
Definition: WsBasicAuth.cpp:31
std::string getEmail()
Get the email of the user.
Definition: WsBasicAuth.cpp:64
std::set< std::string > getUserGroups()
get the groups of the user
Definition: WsBasicAuth.cpp:69
bool ipValid(std::string ip)
return true or false whether the ip is valid (ie match the mask in the configuration file ...
Definition: WsBasicAuth.cpp:79
#define DEBUG
Definition: WsLogger.h:27
int authentify(const std::string &uid, const std::string &pass="", const std::string &ip="")
authenticate the user this function is a basic function and will authenticate any user and add the gr...
Definition: WsBasicAuth.cpp:37
void WsBasicAuthInit(void)
Definition: WsBasicAuth.cpp:21
std::string getFirstName()
Get the first name of the user.
Definition: WsBasicAuth.cpp:54
#define LOG
Definition: WsLogger.h:22
std::set< std::string > getAllGroups()
get all the groups in the ldap
Definition: WsBasicAuth.cpp:74
Basic connector class.
std::string get(const std::string &section, const std::string &id, const std::string &def)
std::string getSurname()
Get the surname of the user.
Definition: WsBasicAuth.cpp:59
static WsGlobalProperties * instance()
std::string getUid()
Get the uid of the user.
Definition: WsBasicAuth.cpp:49
const int Success
#define ERROR
Definition: WsLogger.h:42