Fixed demo account creation. fixes #167

This commit is contained in:
emeric
2021-09-02 21:05:13 +02:00
parent 9e37012090
commit 56a588a06b
19 changed files with 172 additions and 162 deletions
+2 -2
View File
@@ -33,9 +33,9 @@ namespace Auth
Database::User::pointer user {Database::User::getByLoginName(session, loginName)};
if (!user)
{
const Database::User::Type type {Database::User::getCount(session) == 0 ? Database::User::Type::ADMIN : Database::User::Type::REGULAR};
const Database::UserType type {Database::User::getCount(session) == 0 ? Database::UserType::ADMIN : Database::UserType::REGULAR};
LMS_LOG(AUTH, DEBUG) << "Creating user '" << loginName << "', admin = " << (type == Database::User::Type::ADMIN);
LMS_LOG(AUTH, DEBUG) << "Creating user '" << loginName << "', admin = " << (type == Database::UserType::ADMIN);
user = Database::User::create(session, loginName);
user.modify()->setType(type);
@@ -81,9 +81,18 @@ namespace Auth
}
bool
InternalPasswordService::isPasswordSecureEnough(std::string_view loginName, std::string_view password) const
InternalPasswordService::isPasswordSecureEnough(std::string_view password, const PasswordValidationContext& context) const
{
return _validator.evaluateStrength(std::string {password}, std::string {loginName}, "").isValid();
switch (context.userType)
{
case Database::UserType::ADMIN:
case Database::UserType::REGULAR:
return _validator.evaluateStrength(std::string {password}, context.loginName, "").isValid();
case Database::UserType::DEMO:
return true; // no constraint
}
throw NotImplementedException {};
}
void
@@ -97,7 +106,7 @@ namespace Auth
if (!user)
throw Exception {"User not found!"};
if (!isPasswordSecureEnough(user->getLoginName(), newPassword))
if (!isPasswordSecureEnough(newPassword, PasswordValidationContext {user->getLoginName(), user->getType()} ))
throw PasswordTooWeakException {};
user.modify()->setPasswordHash(passwordHash);
@@ -41,7 +41,7 @@ namespace Auth
std::string_view password) override;
bool canSetPasswords() const override;
bool isPasswordSecureEnough(std::string_view loginName, std::string_view password) const override;
bool isPasswordSecureEnough(std::string_view loginName, const PasswordValidationContext& context) const override;
void setPassword(Database::Session& session, Database::IdType userId, std::string_view newPassword) override;
Database::User::PasswordHash hashPassword(std::string_view password) const;
@@ -187,7 +187,7 @@ namespace Auth
}
bool
PAMPasswordService::isPasswordSecureEnough(std::string_view, std::string_view) const
PAMPasswordService::isPasswordSecureEnough(std::string_view, const PasswordValidationContext&) const
{
throw NotImplementedException {};
}
@@ -36,8 +36,7 @@ namespace Auth
std::string_view password) override;
bool canSetPasswords() const override;
bool isPasswordSecureEnough(std::string_view loginName,
std::string_view password) const override;
bool isPasswordSecureEnough(std::string_view loginName, const PasswordValidationContext& context) const override;
void setPassword(Database::Session& session,
Database::IdType userId,
std::string_view newPassword) override;
@@ -61,14 +61,9 @@ namespace Auth
std::string_view loginName,
std::string_view password) = 0;
class PasswordTooWeakException : public Auth::Exception
{
public:
PasswordTooWeakException() : Auth::Exception {"Password too weak"} {}
};
virtual bool canSetPasswords() const = 0;
virtual bool isPasswordSecureEnough(std::string_view username, std::string_view password) const = 0;
virtual bool isPasswordSecureEnough(std::string_view password, const PasswordValidationContext& context) const = 0;
virtual void setPassword(Database::Session& session, Database::IdType userId, std::string_view newPassword) = 0;
};
+14
View File
@@ -19,6 +19,8 @@
#pragma once
#include <string>
#include "database/Types.hpp"
#include "utils/Exception.hpp"
namespace Auth
@@ -33,5 +35,17 @@ namespace Auth
public:
NotImplementedException() : Auth::Exception {"Not implemented"} {}
};
struct PasswordValidationContext
{
std::string loginName;
Database::UserType userType;
};
class PasswordTooWeakException : public Exception
{
public:
PasswordTooWeakException() : Auth::Exception {"Password too weak"} {}
};
}