Browse Source

Added C++17 implementation password hashing with picosha2

php8
chodak166 4 months ago
parent
commit
4a990be146
  1. 5
      cpp17/app/defaults/users.json
  2. 3
      cpp17/lib/src/domain/entities/User.h
  3. 6
      cpp17/lib/src/infrastructure/auth/FileJwtAuthService.cpp
  4. 1
      cpp17/vcpkg.json

5
cpp17/app/defaults/users.json

@ -1,12 +1,13 @@
[ [
{ {
"username": "admin", "username": "admin",
"password": "admin", "password": "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
"id": "1000" "id": "1000"
}, },
{ {
"username": "user", "username": "user",
"password": "user", "password": "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb",
"id": "1001" "id": "1001"
} }
] ]

3
cpp17/lib/src/domain/entities/User.h

@ -9,8 +9,9 @@ struct User
using Id_t = std::string; using Id_t = std::string;
inline static const Id_t NULL_ID{""}; inline static const Id_t NULL_ID{""};
Id_t id; Id_t id;
std::string username; std::string username;
std::string password; std::string passwordHash;
}; };
} // namespace nxl::autostore::domain } // namespace nxl::autostore::domain

6
cpp17/lib/src/infrastructure/auth/FileJwtAuthService.cpp

@ -2,6 +2,7 @@
#include <jwt-cpp/jwt.h> #include <jwt-cpp/jwt.h>
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <picosha2.h>
namespace nxl::autostore::infrastructure { namespace nxl::autostore::infrastructure {
@ -68,6 +69,9 @@ FileJwtAuthService::authenticateUser(std::string_view username,
std::string_view password) std::string_view password)
{ {
try { try {
std::string passwordHash;
picosha2::hash256_hex_string(password, passwordHash);
std::ifstream file(dbPath); std::ifstream file(dbPath);
if (!file.is_open()) { if (!file.is_open()) {
return std::nullopt; return std::nullopt;
@ -78,7 +82,7 @@ FileJwtAuthService::authenticateUser(std::string_view username,
for (const auto& userJson : usersJson) { for (const auto& userJson : usersJson) {
if (userJson["username"] == username if (userJson["username"] == username
&& userJson["password"] == password) { && userJson["password"] == passwordHash) {
return userJson["id"].get<std::string>(); return userJson["id"].get<std::string>();
} }
} }

1
cpp17/vcpkg.json

@ -6,6 +6,7 @@
"nlohmann-json", "nlohmann-json",
"jwt-cpp", "jwt-cpp",
"spdlog", "spdlog",
"picosha2",
"catch2", "catch2",
"trompeloeil" "trompeloeil"
], ],

Loading…
Cancel
Save