Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/server/acl/user.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

#include "server/acl/user.h"

#include <xxhash.h>
#include <openssl/sha.h>

namespace dfly {

namespace {
std::string StringSHA256(std::string_view password) {
std::string hash;
hash.resize(SHA256_DIGEST_LENGTH);
SHA256(reinterpret_cast<const unsigned char*>(password.data()), password.size(),
reinterpret_cast<unsigned char*>(hash.data()));
return hash;
}

} // namespace

User::User() {
// acl_categories_ = AclCat::ACL_CATEGORY_ADMIN;
}

void User::Update(UpdateRequest&& req) {
if (req.password) {
SetPassword(*req.password);
SetPasswordHash(*req.password);
}

if (req.plus_acl_categories) {
Expand All @@ -30,19 +41,19 @@ void User::Update(UpdateRequest&& req) {
}
}

void User::SetPassword(std::string_view password) {
password_ = HashPassword(password);
void User::SetPasswordHash(std::string_view password) {
password_hash_ = StringSHA256(password);
}

bool User::HasPassword(std::string_view password) const {
if (!password_) {
if (!password_hash_) {
if (password == "nopass") {
return true;
}
return false;
}
// hash password and compare
return *password_ == HashPassword(password);
return *password_hash_ == StringSHA256(password);
}

void User::SetAclCategories(uint64_t cat) {
Expand Down Expand Up @@ -70,8 +81,4 @@ bool User::IsActive() const {
return is_active_;
}

uint32_t User::HashPassword(std::string_view password) const {
return XXH3_64bits(password.data(), password.size());
}

} // namespace dfly
7 changes: 2 additions & 5 deletions src/server/acl/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,12 @@ class User final {
// For is_active flag
void SetIsActive(bool is_active);

// Helper function for hashing passwords
uint32_t HashPassword(std::string_view password) const;

// For passwords
void SetPassword(std::string_view password);
void SetPasswordHash(std::string_view password);

// when optional is empty, the special `nopass` password is implied
// password hashed with xx64
std::optional<uint64_t> password_;
std::optional<std::string> password_hash_;
uint32_t acl_categories_{AclCat::ACL_CATEGORY_NONE};

// we have at least 221 commands including a bunch of subcommands
Expand Down