WIP. User authentication on user interface
This commit is contained in:
@@ -1,14 +1,55 @@
|
||||
#include "DatabaseHandler.hpp"
|
||||
#include <Wt/Auth/Dbo/AuthInfo>
|
||||
#include <Wt/Auth/Dbo/UserDatabase>
|
||||
#include <Wt/Auth/AuthService>
|
||||
#include <Wt/Auth/HashFunction>
|
||||
#include <Wt/Auth/PasswordService>
|
||||
#include <Wt/Auth/PasswordStrengthValidator>
|
||||
#include <Wt/Auth/PasswordVerifier>
|
||||
|
||||
// Db types
|
||||
#include "AudioTypes.hpp"
|
||||
#include "FileTypes.hpp"
|
||||
#include "MediaDirectory.hpp"
|
||||
#include "User.hpp"
|
||||
|
||||
#include "DatabaseHandler.hpp"
|
||||
|
||||
namespace Database {
|
||||
|
||||
|
||||
namespace {
|
||||
Wt::Auth::AuthService authService;
|
||||
Wt::Auth::PasswordService passwordService(authService);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Handler::configureAuth(void)
|
||||
{
|
||||
authService.setEmailVerificationEnabled(true);
|
||||
|
||||
Wt::Auth::PasswordVerifier *verifier = new Wt::Auth::PasswordVerifier();
|
||||
verifier->addHashFunction(new Wt::Auth::BCryptHashFunction(8));
|
||||
passwordService.setVerifier(verifier);
|
||||
passwordService.setAttemptThrottlingEnabled(true);
|
||||
passwordService.setStrengthValidator(new Wt::Auth::PasswordStrengthValidator());
|
||||
}
|
||||
|
||||
const Wt::Auth::AuthService&
|
||||
Handler::getAuthService()
|
||||
{
|
||||
return authService;
|
||||
}
|
||||
|
||||
const Wt::Auth::PasswordService&
|
||||
Handler::getPasswordService()
|
||||
{
|
||||
return passwordService;
|
||||
}
|
||||
|
||||
|
||||
Handler::Handler(boost::filesystem::path db)
|
||||
:
|
||||
_path(db),
|
||||
_dbBackend( db.string() )
|
||||
{
|
||||
_session.setConnection(_dbBackend);
|
||||
@@ -22,6 +63,11 @@ _dbBackend( db.string() )
|
||||
_session.mapClass<Database::MediaDirectory>("media_directory");
|
||||
_session.mapClass<Database::MediaDirectorySettings>("media_directory_settings");
|
||||
|
||||
_session.mapClass<Database::User>("user");
|
||||
_session.mapClass<Database::AuthInfo>("auth_info");
|
||||
_session.mapClass<Database::AuthInfo::AuthIdentityType>("auth_identity");
|
||||
_session.mapClass<Database::AuthInfo::AuthTokenType>("auth_token");
|
||||
|
||||
try {
|
||||
_session.createTables();
|
||||
}
|
||||
@@ -30,6 +76,19 @@ _dbBackend( db.string() )
|
||||
}
|
||||
|
||||
_dbBackend.executeSql("pragma journal_mode=WAL");
|
||||
|
||||
_users = new UserDatabase(_session);
|
||||
}
|
||||
|
||||
Handler::~Handler()
|
||||
{
|
||||
delete _users;
|
||||
}
|
||||
|
||||
Wt::Auth::AbstractUserDatabase&
|
||||
Handler::getUserDatabase()
|
||||
{
|
||||
return *_users;
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -5,26 +5,44 @@
|
||||
|
||||
#include <Wt/Dbo/Dbo>
|
||||
#include <Wt/Dbo/backend/Sqlite3>
|
||||
#include <Wt/Auth/Dbo/UserDatabase>
|
||||
#include <Wt/Auth/Login>
|
||||
#include <Wt/Auth/PasswordService>
|
||||
|
||||
#include "User.hpp"
|
||||
|
||||
namespace Database {
|
||||
|
||||
// Long living class handling the database
|
||||
typedef Wt::Auth::Dbo::UserDatabase<AuthInfo> UserDatabase;
|
||||
|
||||
// Session living class handling the database
|
||||
class Handler
|
||||
{
|
||||
public:
|
||||
|
||||
Handler(boost::filesystem::path db);
|
||||
~Handler();
|
||||
|
||||
Wt::Dbo::Session& getSession() { return _session; }
|
||||
|
||||
boost::filesystem::path getPath() const { return _path; }
|
||||
Wt::Auth::AbstractUserDatabase& getUserDatabase();
|
||||
Wt::Auth::Login& getLogin() { return _login; }
|
||||
|
||||
// Long living shared associated services
|
||||
static void configureAuth();
|
||||
|
||||
static const Wt::Auth::AuthService& getAuthService();
|
||||
static const Wt::Auth::PasswordService& getPasswordService();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
boost::filesystem::path _path;
|
||||
Wt::Dbo::backend::Sqlite3 _dbBackend;
|
||||
Wt::Dbo::Session _session;
|
||||
|
||||
UserDatabase* _users;
|
||||
Wt::Auth::Login _login;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef DATABASE_USER_HPP
|
||||
#define DATABASE_USER_HPP
|
||||
|
||||
|
||||
#include <Wt/Auth/Dbo/AuthInfo>
|
||||
|
||||
namespace Database {
|
||||
|
||||
class User;
|
||||
typedef Wt::Auth::Dbo::AuthInfo<User> AuthInfo;
|
||||
|
||||
class User {
|
||||
|
||||
public:
|
||||
|
||||
template<class Action>
|
||||
void persist(Action& a)
|
||||
{
|
||||
Wt::Dbo::field(a, _isAdmin, "admin");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool _isAdmin;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Databas'
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user