Moved password related methods to the User from UserManager.

This commit is contained in:
Relintai 2022-07-08 10:00:54 +02:00
parent e0ceb9fbab
commit 62c895f85c
4 changed files with 52 additions and 51 deletions

View File

@ -20,15 +20,6 @@ bool UserManager::is_username_taken(const String &user_name) {
bool UserManager::is_email_taken(const String &email_input) {
return call("_is_email_taken", email_input);
}
bool UserManager::check_password(const Ref<User> &user, const String &p_password) {
return call("_check_password", user, p_password);
}
void UserManager::create_password(const Ref<User> &user, const String &p_password) {
call("_create_password", user, p_password);
}
String UserManager::hash_password(const Ref<User> &user, const String &p_password) {
return call("_hash_password", user, p_password);
}
Ref<User> UserManager::_get_user(const int id) {
return Ref<User>();
@ -50,27 +41,6 @@ bool UserManager::_is_username_taken(const String &user_name) {
bool UserManager::_is_email_taken(const String &email) {
return false;
}
bool UserManager::_check_password(const Ref<User> &user, const String &p_password) {
return hash_password(user, p_password) == user->get_password_hash();
}
void UserManager::_create_password(Ref<User> user, const String &p_password) {
if (!user.is_valid()) {
printf("Error UserManager::create_password !user.is_valid()!\n");
return;
}
// todo improve a bit
user->set_pre_salt(hash_password(user, user->get_name_user_input() + user->get_email_user_input()));
user->set_post_salt(hash_password(user, user->get_email_user_input() + user->get_name_user_input()));
user->set_password_hash(hash_password(user, p_password));
}
String UserManager::_hash_password(const Ref<User> &user, const String &p_password) {
ERR_FAIL_COND_V(!user.is_valid(), "");
String p = user->get_pre_salt() + p_password + user->get_post_salt();
return p.sha256_text();
}
Vector<Ref<User>> UserManager::get_all() {
return Vector<Ref<User>>();
@ -92,10 +62,6 @@ void UserManager::_bind_methods() {
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_is_username_taken", PropertyInfo(Variant::STRING, "user_name")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_is_email_taken", PropertyInfo(Variant::STRING, "email")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_check_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
BIND_VMETHOD(MethodInfo("_create_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, "ret"), "_hash_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
ClassDB::bind_method(D_METHOD("get_user", "id"), &UserManager::get_user);
ClassDB::bind_method(D_METHOD("get_user_name", "user_name"), &UserManager::get_user_name);
ClassDB::bind_method(D_METHOD("save_user", "user"), &UserManager::save_user);
@ -105,10 +71,6 @@ void UserManager::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_username_taken", "user_name"), &UserManager::is_username_taken);
ClassDB::bind_method(D_METHOD("is_email_taken", "email"), &UserManager::is_email_taken);
ClassDB::bind_method(D_METHOD("check_password", "user", "password"), &UserManager::check_password);
ClassDB::bind_method(D_METHOD("create_password", "user", "password"), &UserManager::create_password);
ClassDB::bind_method(D_METHOD("hash_password", "user", "password"), &UserManager::hash_password);
ClassDB::bind_method(D_METHOD("_get_user", "id"), &UserManager::_get_user);
ClassDB::bind_method(D_METHOD("_get_user_name", "user_name"), &UserManager::_get_user_name);
ClassDB::bind_method(D_METHOD("_save_user", "user"), &UserManager::_save_user);
@ -117,8 +79,4 @@ void UserManager::_bind_methods() {
ClassDB::bind_method(D_METHOD("_is_username_taken", "user_name"), &UserManager::_is_username_taken);
ClassDB::bind_method(D_METHOD("_is_email_taken", "email"), &UserManager::_is_email_taken);
ClassDB::bind_method(D_METHOD("_check_password", "user", "password"), &UserManager::_check_password);
ClassDB::bind_method(D_METHOD("_create_password", "user", "password"), &UserManager::_create_password);
ClassDB::bind_method(D_METHOD("_hash_password", "user", "password"), &UserManager::_hash_password);
}

View File

@ -22,11 +22,6 @@ public:
bool is_username_taken(const String &user_name);
bool is_email_taken(const String &email);
//TODO these should probably be moved to User
bool check_password(const Ref<User> &user, const String &p_password);
void create_password(const Ref<User> &user, const String &p_password);
String hash_password(const Ref<User> &user, const String &p_password);
virtual Ref<User> _get_user(const int id);
virtual Ref<User> _get_user_name(const String &user);
virtual void _save_user(Ref<User> user);
@ -36,10 +31,6 @@ public:
virtual bool _is_username_taken(const String &user_name);
virtual bool _is_email_taken(const String &email);
virtual bool _check_password(const Ref<User> &user, const String &p_password);
virtual void _create_password(Ref<User> user, const String &p_password);
virtual String _hash_password(const Ref<User> &user, const String &p_password);
Vector<Ref<User>> get_all();
UserManager();

View File

@ -64,6 +64,38 @@ void User::set_locked(const bool &val) {
_locked = val;
}
bool User::check_password(const Ref<User> &user, const String &p_password) {
return call("_check_password", user, p_password);
}
void User::create_password(const Ref<User> &user, const String &p_password) {
call("_create_password", user, p_password);
}
String User::hash_password(const Ref<User> &user, const String &p_password) {
return call("_hash_password", user, p_password);
}
bool User::_check_password(const Ref<User> &user, const String &p_password) {
return hash_password(user, p_password) == user->get_password_hash();
}
void User::_create_password(Ref<User> user, const String &p_password) {
if (!user.is_valid()) {
printf("Error UserManager::create_password !user.is_valid()!\n");
return;
}
// todo improve a bit
user->set_pre_salt(hash_password(user, user->get_name_user_input() + user->get_email_user_input()));
user->set_post_salt(hash_password(user, user->get_email_user_input() + user->get_name_user_input()));
user->set_password_hash(hash_password(user, p_password));
}
String User::_hash_password(const Ref<User> &user, const String &p_password) {
ERR_FAIL_COND_V(!user.is_valid(), "");
String p = user->get_pre_salt() + p_password + user->get_post_salt();
return p.sha256_text();
}
User::User() {
_rank = 0;
_banned = false;
@ -110,6 +142,18 @@ void User::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_locked", "val"), &User::set_locked);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "locked"), "set_locked", "get_locked");
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_check_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
BIND_VMETHOD(MethodInfo("_create_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, "ret"), "_hash_password", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"), PropertyInfo(Variant::STRING, "password")));
ClassDB::bind_method(D_METHOD("check_password", "user", "password"), &User::check_password);
ClassDB::bind_method(D_METHOD("create_password", "user", "password"), &User::create_password);
ClassDB::bind_method(D_METHOD("hash_password", "user", "password"), &User::hash_password);
ClassDB::bind_method(D_METHOD("_check_password", "user", "password"), &User::_check_password);
ClassDB::bind_method(D_METHOD("_create_password", "user", "password"), &User::_create_password);
ClassDB::bind_method(D_METHOD("_hash_password", "user", "password"), &User::_hash_password);
BIND_ENUM_CONSTANT(PERMISSION_CREATE);
BIND_ENUM_CONSTANT(PERMISSION_READ);
BIND_ENUM_CONSTANT(PERMISSION_UPDATE);

View File

@ -46,6 +46,14 @@ public:
bool get_locked() const;
void set_locked(const bool &val);
bool check_password(const Ref<User> &user, const String &p_password);
void create_password(const Ref<User> &user, const String &p_password);
String hash_password(const Ref<User> &user, const String &p_password);
virtual bool _check_password(const Ref<User> &user, const String &p_password);
virtual void _create_password(Ref<User> user, const String &p_password);
virtual String _hash_password(const Ref<User> &user, const String &p_password);
User();
~User();