/usr/lib/Wt/examples/hangman/Session.C is in witty-examples 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | /*
* Copyright (C) 2011 Emweb bvba, Heverlee, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Session.h"
#include "Wt/Auth/AuthService"
#include "Wt/Auth/HashFunction"
#include "Wt/Auth/PasswordService"
#include "Wt/Auth/PasswordStrengthValidator"
#include "Wt/Auth/PasswordVerifier"
#include "Wt/Auth/GoogleService"
#include "Wt/Auth/Dbo/AuthInfo"
#include "Wt/Auth/Dbo/UserDatabase"
#include <Wt/WApplication>
#include <Wt/WLogger>
#ifndef WIN32
#include <unistd.h>
#endif
#if !defined(WIN32) && !defined(__CYGWIN__) && !defined(ANDROID)
#define HAVE_CRYPT
#endif
using namespace Wt;
namespace dbo = Wt::Dbo;
namespace {
#ifdef HAVE_CRYPT
class UnixCryptHashFunction : public Auth::HashFunction
{
public:
virtual std::string compute(const std::string& msg,
const std::string& salt) const
{
std::string md5Salt = "$1$" + salt;
return crypt(msg.c_str(), md5Salt.c_str());
}
virtual bool verify(const std::string& msg,
const std::string& salt,
const std::string& hash) const
{
return crypt(msg.c_str(), hash.c_str()) == hash;
}
virtual std::string name () const {
return "crypt";
}
};
#endif // HAVE_CRYPT
class MyOAuth : public std::vector<const Auth::OAuthService *>
{
public:
~MyOAuth()
{
for (unsigned i = 0; i < size(); ++i)
delete (*this)[i];
}
};
Auth::AuthService myAuthService;
Auth::PasswordService myPasswordService(myAuthService);
MyOAuth myOAuthServices;
}
void Session::configureAuth()
{
myAuthService.setAuthTokensEnabled(true, "hangmancookie");
myAuthService.setEmailVerificationEnabled(true);
Auth::PasswordVerifier *verifier = new Auth::PasswordVerifier();
verifier->addHashFunction(new Auth::BCryptHashFunction(7));
#ifdef HAVE_CRYPT
// We want to still support users registered in the pre - Wt::Auth
// version of the hangman example
verifier->addHashFunction(new UnixCryptHashFunction());
#endif
myPasswordService.setVerifier(verifier);
myPasswordService.setStrengthValidator(new Auth::PasswordStrengthValidator());
myPasswordService.setAttemptThrottlingEnabled(true);
if (Auth::GoogleService::configured())
myOAuthServices.push_back(new Auth::GoogleService(myAuthService));
}
Session::Session()
: sqlite3_(WApplication::instance()->appRoot() + "hangman.db")
{
session_.setConnection(sqlite3_);
sqlite3_.setProperty("show-queries", "true");
session_.mapClass<User>("user");
session_.mapClass<AuthInfo>("auth_info");
session_.mapClass<AuthInfo::AuthIdentityType>("auth_identity");
session_.mapClass<AuthInfo::AuthTokenType>("auth_token");
users_ = new UserDatabase(session_);
dbo::Transaction transaction(session_);
try {
session_.createTables();
/*
* Add a default guest/guest account
*/
Auth::User guestUser = users_->registerNew();
guestUser.addIdentity(Auth::Identity::LoginName, "guest");
myPasswordService.updatePassword(guestUser, "guest");
Wt::log("info") << "Database created";
} catch (...) {
Wt::log("info") << "Using existing database";
}
transaction.commit();
}
Session::~Session()
{
delete users_;
}
dbo::ptr<User> Session::user() const
{
if (login_.loggedIn()) {
dbo::ptr<AuthInfo> authInfo = users_->find(login_.user());
dbo::ptr<User> user = authInfo->user();
if (!user) {
user = session_.add(new User());
authInfo.modify()->setUser(user);
}
return user;
} else
return dbo::ptr<User>();
}
std::string Session::userName() const
{
if (login_.loggedIn())
return login_.user().identity(Auth::Identity::LoginName).toUTF8();
else
return std::string();
}
void Session::addToScore(int s)
{
dbo::Transaction transaction(session_);
dbo::ptr<User> u = user();
if (u) {
u.modify()->score += s;
++u.modify()->gamesPlayed;
u.modify()->lastGame = WDateTime::currentDateTime();
}
transaction.commit();
}
std::vector<User> Session::topUsers(int limit)
{
dbo::Transaction transaction(session_);
Users top = session_.find<User>().orderBy("score desc").limit(20);
std::vector<User> result;
for (Users::const_iterator i = top.begin(); i != top.end(); ++i) {
dbo::ptr<User> user = *i;
result.push_back(*user);
dbo::ptr<AuthInfo> auth = *user->authInfos.begin();
std::string name = auth->identity(Auth::Identity::LoginName).toUTF8();
result.back().name = name;
}
transaction.commit();
return result;
}
int Session::findRanking()
{
dbo::Transaction transaction(session_);
dbo::ptr<User> u = user();
int ranking = -1;
if (u)
ranking = session_.query<int>("select distinct count(score) from user")
.where("score > ?").bind(u->score);
transaction.commit();
return ranking + 1;
}
Auth::AbstractUserDatabase& Session::users()
{
return *users_;
}
const Auth::AuthService& Session::auth()
{
return myAuthService;
}
const Auth::AbstractPasswordService& Session::passwordAuth()
{
return myPasswordService;
}
const std::vector<const Auth::OAuthService *>& Session::oAuth()
{
return myOAuthServices;
}
|