first what so ever registration
This commit is contained in:
parent
0c50cfa639
commit
99a9fd507e
17 changed files with 285 additions and 25 deletions
|
@ -44,6 +44,7 @@ void Router::route(const std::string& path, std::unique_ptr<Request> request) {
|
|||
return handleNotFound(path, std::move(request));
|
||||
|
||||
try {
|
||||
std::cout << "Handling " << path << "..." << std::endl;
|
||||
itr->second->handle(*request.get());
|
||||
|
||||
if (request->currentState() != Request::State::responded)
|
||||
|
|
|
@ -3,11 +3,20 @@
|
|||
|
||||
#include "server.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
#include "handler/info.h"
|
||||
#include "handler/env.h"
|
||||
#include "handler/register.h"
|
||||
|
||||
constexpr const char* pepper = "well, not much of a secret, huh?";
|
||||
constexpr uint8_t currentDbVesion = 1;
|
||||
constexpr const char* randomChars = "0123456789abcdef";
|
||||
constexpr uint8_t saltSize = 16;
|
||||
constexpr uint8_t hashSize = 32;
|
||||
constexpr uint8_t hashParallel = 1;
|
||||
constexpr uint8_t hashIterations = 2;
|
||||
constexpr uint32_t hashMemoryCost = 65536;
|
||||
|
||||
Server::Server():
|
||||
terminating(false),
|
||||
|
@ -29,7 +38,7 @@ Server::Server():
|
|||
|
||||
router.addRoute(std::make_unique<Handler::Info>());
|
||||
router.addRoute(std::make_unique<Handler::Env>());
|
||||
router.addRoute(std::make_unique<Handler::Register>());
|
||||
router.addRoute(std::make_unique<Handler::Register>(this));
|
||||
}
|
||||
|
||||
Server::~Server() {}
|
||||
|
@ -63,3 +72,38 @@ void Server::handleRequest(std::unique_ptr<Request> request) {
|
|||
std::string path = request->getPath(serverName.value());
|
||||
router.route(path.data(), std::move(request));
|
||||
}
|
||||
|
||||
std::string Server::generateRandomString(std::size_t length) {
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<uint8_t> distribution(0, std::strlen(randomChars));
|
||||
|
||||
std::string result(length, 0);
|
||||
for (size_t i = 0; i < length; ++i)
|
||||
result[i] = randomChars[distribution(gen)];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned int Server::registerAccount(const std::string& login, const std::string& password) {
|
||||
std::size_t encSize = argon2_encodedlen(
|
||||
hashIterations, hashMemoryCost,
|
||||
hashParallel, saltSize, hashSize, Argon2_id
|
||||
);
|
||||
|
||||
std::string hash(encSize, 0);
|
||||
std::string salt = generateRandomString(saltSize);
|
||||
std::string spiced = password + pepper;
|
||||
|
||||
int result = argon2id_hash_encoded(
|
||||
hashIterations, hashMemoryCost, hashParallel,
|
||||
spiced.data(), spiced.size(),
|
||||
salt.data(), saltSize,
|
||||
hashSize, hash.data(), encSize
|
||||
);
|
||||
|
||||
if (result != ARGON2_OK)
|
||||
throw std::runtime_error(std::string("Hashing failed: ") + argon2_error_message(result));
|
||||
|
||||
return db->registerAccount(login, hash);
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include <fcgio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <argon2.h>
|
||||
|
||||
#include "request/request.h"
|
||||
#include "response/response.h"
|
||||
|
@ -31,8 +31,11 @@ public:
|
|||
|
||||
void run(int socketDescriptor);
|
||||
|
||||
unsigned int registerAccount(const std::string& login, const std::string& password);
|
||||
|
||||
private:
|
||||
void handleRequest(std::unique_ptr<Request> request);
|
||||
static std::string generateRandomString(std::size_t length);
|
||||
|
||||
private:
|
||||
bool terminating;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue