Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #if !defined(WIN32) && !defined(__QNXNTO__)
00008 #define _XOPEN_SOURCE
00009 #include <unistd.h>
00010 #endif
00011
00012 #include "User.h"
00013
00014 #if !defined(WIN32) && !defined(__CYGWIN__) && !defined(ANDROID)
00015 #define HAVE_CRYPT
00016 #endif
00017
00018 #ifdef HAVE_CRYPT
00019 namespace {
00020 std::string generateSalt() {
00021
00022 unsigned long seed[2];
00023 char salt[] = "$1$........";
00024 const char *const seedchars =
00025 "./0123456789ABCDEFGHIJKLMNOPQRST"
00026 "UVWXYZabcdefghijklmnopqrstuvwxyz";
00027
00028
00029 seed[0] = time(NULL);
00030 seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
00031
00032
00033 for (int i = 0; i < 8; i++)
00034 salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
00035
00036 return salt;
00037 }
00038
00039 std::string md5(const std::string& s, const std::string& salt) {
00040 return crypt(s.c_str(), salt.c_str());
00041 }
00042 }
00043 #endif
00044
00045 using namespace Wt;
00046 using namespace Wt::Dbo;
00047
00048 User::User(const std::string &name, const std::string &password)
00049 : name(name),
00050 gamesPlayed(0),
00051 score(0)
00052 {
00053 setPassword(password);
00054 }
00055
00056
00057 void User::setPassword(const std::string& password)
00058 {
00059 #ifdef HAVE_CRYPT
00060 password_ = md5(password, generateSalt());
00061 #else
00062
00063 password_ = password;
00064 #endif
00065 }
00066
00067 bool User::authenticate(const std::string& password) const
00068 {
00069 #ifdef HAVE_CRYPT
00070 return md5(password, password_) == password_;
00071 #else
00072 return password_ == password;
00073 #endif
00074 }