Working on User authentication

This commit is contained in:
kdeng00
2019-09-22 23:57:14 -04:00
parent 89ed9f9252
commit 4bd794c288
19 changed files with 627 additions and 11 deletions
+3
View File
@@ -16,3 +16,6 @@
[submodule "3rdparty/ormpp"] [submodule "3rdparty/ormpp"]
path = 3rdparty/ormpp path = 3rdparty/ormpp
url = https://github.com/qicosmos/ormpp url = https://github.com/qicosmos/ormpp
[submodule "3rdparty/libbcrypt"]
path = 3rdparty/libbcrypt
url = https://github.com/rg3/libbcrypt
Vendored Submodule
+1
Submodule 3rdparty/libbcrypt added at 8aa32ad94e
+1 -1
+18 -2
View File
@@ -13,6 +13,7 @@ set(SOURCES
src/database/CoverArtRepository.cpp src/database/CoverArtRepository.cpp
src/database/GenreRepository.cpp src/database/GenreRepository.cpp
src/database/SongRepository.cpp src/database/SongRepository.cpp
src/database/UserRepository.cpp
src/database/YearRepository.cpp src/database/YearRepository.cpp
src/Main.cpp src/Main.cpp
src/manager/AlbumManager.cpp src/manager/AlbumManager.cpp
@@ -22,9 +23,11 @@ set(SOURCES
src/manager/GenreManager.cpp src/manager/GenreManager.cpp
src/manager/SongManager.cpp src/manager/SongManager.cpp
src/manager/TokenManager.cpp src/manager/TokenManager.cpp
src/manager/UserManager.cpp
src/manager/YearManager.cpp src/manager/YearManager.cpp
src/utility/ImageFile.cpp src/utility/ImageFile.cpp
src/utility/MetadataRetriever.cpp src/utility/MetadataRetriever.cpp
src/utility/PasswordEncryption.cpp
src/verify/Initialization.cpp src/verify/Initialization.cpp
) )
set(HEADERS set(HEADERS
@@ -35,6 +38,7 @@ set(HEADERS
include/controller/CoverArtController.hpp include/controller/CoverArtController.hpp
include/controller/GenreController.hpp include/controller/GenreController.hpp
include/controller/LoginController.hpp include/controller/LoginController.hpp
include/controller/RegisterController.hpp
include/controller/SongController.hpp include/controller/SongController.hpp
include/controller/YearController.hpp include/controller/YearController.hpp
include/database/AlbumRepository.h include/database/AlbumRepository.h
@@ -43,6 +47,7 @@ set(HEADERS
include/database/CoverArtRepository.h include/database/CoverArtRepository.h
include/database/GenreRepository.h include/database/GenreRepository.h
include/database/SongRepository.h include/database/SongRepository.h
include/database/UserRepository.h
include/database/YearRepository.h include/database/YearRepository.h
include/dto/AlbumDto.hpp include/dto/AlbumDto.hpp
include/dto/ArtistDto.hpp include/dto/ArtistDto.hpp
@@ -58,18 +63,22 @@ set(HEADERS
include/manager/GenreManager.h include/manager/GenreManager.h
include/manager/SongManager.h include/manager/SongManager.h
include/manager/TokenManager.h include/manager/TokenManager.h
include/manager/UserManager.h
include/manager/YearManager.h include/manager/YearManager.h
include/model/Models.h include/model/Models.h
include/utility/ImageFile.h include/utility/ImageFile.h
include/utility/MetadataRetriever.h include/utility/MetadataRetriever.h
include/utility/PasswordEncryption.h
include/type/AlbumFilter.h include/type/AlbumFilter.h
include/type/ArtistFilter.h include/type/ArtistFilter.h
include/type/CoverFilter.h include/type/CoverFilter.h
include/type/GenreFilter.h include/type/GenreFilter.h
include/type/PathType.h include/type/PathType.h
include/type/SaltFilter.h
include/type/Scopes.h include/type/Scopes.h
include/type/SongChanged.h include/type/SongChanged.h
include/type/SongFilter.h include/type/SongFilter.h
include/type/UserFilter.h
include/type/YearFilter.h include/type/YearFilter.h
include/verify/Initialization.h include/verify/Initialization.h
) )
@@ -78,6 +87,10 @@ set (TAGLIB
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib/3rdparty ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib/3rdparty
) )
set (BCRYPTLIB
${CMAKE_SOURCE_DIR}/3rdparty/libbcrypt
)
set(TAGLIB_HEADERS set(TAGLIB_HEADERS
"${CMAKE_SOURCE_DIR}/build/3rdparty/taglib" "${CMAKE_SOURCE_DIR}/build/3rdparty/taglib"
"${CMAKE_SOURCE_DIR}/3rdparty/taglib" "${CMAKE_SOURCE_DIR}/3rdparty/taglib"
@@ -114,11 +127,14 @@ set (ORM_DIR
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() conan_basic_setup()
include_directories(include ${CPR_INCLUDE_DIRS} ${TAGLIB} ${TAGLIB_HEADERS}) find_library(BCRYPT bcrypt ${BCRYPTLIB})
include_directories(include ${CPR_INCLUDE_DIRS} ${TAGLIB} ${TAGLIB_HEADERS} ${BCRYPTLIB}/)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpr) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpr)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/oatpp) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/oatpp)
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libbcrypt)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/appsettings.json ${CMAKE_BINARY_DIR}/bin/appsettings.json COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/appsettings.json ${CMAKE_BINARY_DIR}/bin/appsettings.json COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_DIR}/bin/authcredentials.json COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_DIR}/bin/authcredentials.json COPYONLY)
@@ -128,4 +144,4 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Images/Stock/CoverArt.png ${CMAKE_BIN
add_executable(icarus ${SOURCES} ${HEADERS}) add_executable(icarus ${SOURCES} ${HEADERS})
target_include_directories(icarus PUBLIC ${JWT_CPP_INCLUDE}) target_include_directories(icarus PUBLIC ${JWT_CPP_INCLUDE})
target_link_libraries(icarus "-lstdc++fs" tag oatpp mysqlclient ${CONAN_LIBS} ${CPR_LIBRARIES}) target_link_libraries(icarus "-lstdc++fs" tag oatpp mysqlclient ${CONAN_LIBS} ${CPR_LIBRARIES} ${BCRYPT})
+9
View File
@@ -75,3 +75,12 @@ CREATE TABLE User (
PRIMARY KEY (UserId) PRIMARY KEY (UserId)
); );
CREATE TABLE Salt (
SaltId INT NOT NULL AUTO_INCREMENT,
Salt TEXT NOT NULL,
UserId INT NOT NULL,
PRIMARY KEY (SaltId),
CONSTRAINT FK_UserId FOREIGN KEY (UserId) REFERENCES User (UserId)
);
+40
View File
@@ -0,0 +1,40 @@
#ifndef REGISTERCONTROLLER_H_
#define REGISTERCONTROLLER_H_
#include <iostream>
#include <memory>
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"
#include "oatpp/web/server/api/ApiController.hpp"
#include "dto/LoginResultDto.hpp"
#include "manager/UserManager.h"
#include "model/Models.h"
namespace controller
{
class RegisterController : public oatpp::web::server::api::ApiController
{
public:
RegisterController(const model::BinaryPath& bConf,
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper)) :
oatpp::web::server::api::ApiController(objectMapper), m_bConf(bConf) { }
#include OATPP_CODEGEN_BEGIN(ApiController)
ENDPOINT("POST", "api/v1/register", registerUser, BODY_DTO(dto::UserDto::ObjectWrapper, usr)) {
manager::UserManager usrMgr(m_bConf);
auto user = usrMgr.userDtoConv(usr);
usrMgr.registerUser(user);
return createResponse(Status::CODE_200, "Reg");
}
#include OATPP_CODEGEN_END(ApiController)
private:
model::BinaryPath m_bConf;
};
}
#endif
+60
View File
@@ -0,0 +1,60 @@
#ifndef USERREPOSITORY_H_
#define USERREPOSITORY_H_
#include <iostream>
#include <memory>
#include "database/BaseRepository.h"
#include "model/Models.h"
#include "type/UserFilter.h"
namespace database {
class UserRepository : BaseRepository {
public:
UserRepository(const model::BinaryPath&);
model::User retrieveUserRecord(model::User&, type::UserFilter);
void saveUserRecord(const model::User&);
private:
struct UserLengths;
struct UserLengths
{
unsigned long firstnameLength;
unsigned long lastnameLength;
unsigned long phoneLength;
unsigned long emailLength;
unsigned long usernameLength;
unsigned long passwordLength;
};
struct UserVals
{
/**
std::shared_ptr<char*> firstname;
std::shared_ptr<char*> lastname;
std::shared_ptr<char*> phone;
std::shared_ptr<char*> email;
std::shared_ptr<char*> username;
std::shared_ptr<char*> password;
*/
char firstname[1024];
char lastname[1024];
char phone[1024];
char email[1024];
char username[1024];
char password[1024];
};
std::shared_ptr<MYSQL_BIND> insertUserValues(const model::User&, std::shared_ptr<UserLengths>);
std::shared_ptr<MYSQL_BIND> valueBind(model::User&, std::shared_ptr<UserVals>);
std::shared_ptr<MYSQL_BIND> valueBind(model::User&, UserVals&);
std::shared_ptr<MYSQL_BIND> valueBind(model::User&);
std::shared_ptr<UserLengths> fetchUserLengths(const model::User&);
std::shared_ptr<UserVals> fetchUserVals();
model::User parseRecord(MYSQL_STMT*);
};
}
#endif
+5
View File
@@ -24,6 +24,11 @@ namespace dto
{ {
DTO_INIT(UserDto, Object) DTO_INIT(UserDto, Object)
DTO_FIELD(Int32, userId);
DTO_FIELD(String, firstname);
DTO_FIELD(String, lastname);
DTO_FIELD(String, phone);
DTO_FIELD(String, email);
DTO_FIELD(String, username); DTO_FIELD(String, username);
DTO_FIELD(String, password); DTO_FIELD(String, password);
}; };
+25
View File
@@ -0,0 +1,25 @@
#ifndef USERMANAGER_H_
#define USERMANAGER_H_
#include <iostream>
#include "dto/LoginResultDto.hpp"
#include "model/Models.h"
namespace manager
{
class UserManager
{
public:
UserManager(const model::BinaryPath&);
model::User userDtoConv(dto::UserDto::ObjectWrapper&);
void printUser(const model::User&);
void registerUser(model::User&);
private:
model::BinaryPath m_bConf;
};
}
#endif
+17
View File
@@ -116,6 +116,23 @@ namespace model
int expiration; int expiration;
}; };
struct User
{
int id;
std::string firstname;
std::string lastname;
std::string email;
std::string phone;
std::string username;
std::string password;
};
struct PassSec
{
std::string hashPassword;
std::string salt;
};
struct AuthCredentials struct AuthCredentials
{ {
std::string domain; std::string domain;
+12
View File
@@ -0,0 +1,12 @@
#ifndef SALTFILTER_H_
#define SALTFILTER_H_
namespace type {
enum class SaltFilter
{
id = 0,
salt
};
}
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef USERFILTER_H_
#define USERFILTER_H_
namespace type {
enum class UserFilter
{
id = 0,
username
};
}
#endif
+17
View File
@@ -0,0 +1,17 @@
#ifndef PASSWORDENCRYPTION_H_
#define PASSWORDENCRYPTION_H_
#include <string>
#include "model/Models.h"
namespace utility {
class PasswordEncryption {
public:
model::PassSec hashPassword(const model::User&);
private:
int saltSize();
};
}
#endif
+4
View File
@@ -1,6 +1,8 @@
#ifndef INITIALIZATION_H_ #ifndef INITIALIZATION_H_
#define INITIALIZATION_H_ #define INITIALIZATION_H_
#include <string>
#include "model/Models.h" #include "model/Models.h"
namespace verify namespace verify
@@ -8,6 +10,8 @@ namespace verify
class Initialization class Initialization
{ {
public: public:
static bool skipVerification(const std::string&);
static void checkIcarus(const model::BinaryPath&); static void checkIcarus(const model::BinaryPath&);
private: private:
static bool confirmConfigAuth(const model::BinaryPath&); static bool confirmConfigAuth(const model::BinaryPath&);
+13 -1
View File
@@ -15,6 +15,7 @@
#include "controller/CoverArtController.hpp" #include "controller/CoverArtController.hpp"
#include "controller/GenreController.hpp" #include "controller/GenreController.hpp"
#include "controller/LoginController.hpp" #include "controller/LoginController.hpp"
#include "controller/RegisterController.hpp"
#include "controller/SongController.hpp" #include "controller/SongController.hpp"
#include "controller/YearController.hpp" #include "controller/YearController.hpp"
#include "model/Models.h" #include "model/Models.h"
@@ -33,6 +34,7 @@ void run(const model::BinaryPath& bConf)
auto coverArtController = std::make_shared<controller::CoverArtController>(bConf); auto coverArtController = std::make_shared<controller::CoverArtController>(bConf);
auto gnrController = std::make_shared<controller::GenreController>(bConf); auto gnrController = std::make_shared<controller::GenreController>(bConf);
auto logController = std::make_shared<controller::LoginController>(bConf); auto logController = std::make_shared<controller::LoginController>(bConf);
auto regController = std::make_shared<controller::RegisterController>(bConf);
auto sngController = std::make_shared<controller::SongController>(bConf); auto sngController = std::make_shared<controller::SongController>(bConf);
auto yearController = std::make_shared<controller::YearController>(bConf); auto yearController = std::make_shared<controller::YearController>(bConf);
@@ -41,6 +43,7 @@ void run(const model::BinaryPath& bConf)
coverArtController->addEndpointsToRouter(router); coverArtController->addEndpointsToRouter(router);
gnrController->addEndpointsToRouter(router); gnrController->addEndpointsToRouter(router);
logController->addEndpointsToRouter(router); logController->addEndpointsToRouter(router);
regController->addEndpointsToRouter(router);
sngController->addEndpointsToRouter(router); sngController->addEndpointsToRouter(router);
yearController->addEndpointsToRouter(router); yearController->addEndpointsToRouter(router);
@@ -59,8 +62,17 @@ int main(int argc, char **argv)
{ {
oatpp::base::Environment::init(); oatpp::base::Environment::init();
model::BinaryPath bConf(argv[0]); model::BinaryPath bConf(std::move(argv[0]));
if (argc > 1) {
if (!verify::Initialization::skipVerification(argv[1])) {
verify::Initialization::checkIcarus(bConf); verify::Initialization::checkIcarus(bConf);
} else {
std::cout << "skiping verifyication" << std::endl;
}
} else {
verify::Initialization::checkIcarus(bConf);
}
run(bConf); run(bConf);
+277
View File
@@ -0,0 +1,277 @@
#include "database/UserRepository.h"
#include <string>
#include <sstream>
#include <cstring>
namespace database {
UserRepository::UserRepository(const model::BinaryPath& bConf) : BaseRepository(bConf) { }
model::User UserRepository::retrieveUserRecord(model::User& user,
type::UserFilter filter = type::UserFilter::username)
{
std::stringstream qry;
auto conn = setupMysqlConnection();
auto stmt = mysql_stmt_init(conn);
qry << "SELECT * FROM User WHERE ";
MYSQL_BIND params[1];
std::memset(params, 0, sizeof(params));
auto userLength = user.username.size();
switch (filter) {
case type::UserFilter::id:
break;
case type::UserFilter::username:
qry << "Username = ?";
params[0].buffer_type = MYSQL_TYPE_LONG;
params[0].buffer = (char*)user.username.c_str();
params[0].length = &userLength;
params[0].is_null = 0;
break;
default:
break;
}
const auto query = qry.str();
auto status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
status = mysql_stmt_bind_param(stmt, params);
status = mysql_stmt_execute(stmt);
user = parseRecord(stmt);
mysql_stmt_close(stmt);
mysql_close(conn);
return user;
}
void UserRepository::saveUserRecord(const model::User& user)
{
std::cout << "inserting user record" << std::endl;
auto conn = setupMysqlConnection();
auto stmt = mysql_stmt_init(conn);
std::stringstream qry;
qry << "INSERT INTO User(Firstname, Lastname, Phone, Email, Username, Password) ";
qry << "VALUES(?, ?, ?, ?, ?, ?)";
const auto query = qry.str();
auto sizes = fetchUserLengths(user);
auto params = insertUserValues(user, sizes);
auto status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
status = mysql_stmt_bind_param(stmt, params.get());
status = mysql_stmt_execute(stmt);
mysql_stmt_close(stmt);
mysql_close(conn);
}
std::shared_ptr<MYSQL_BIND> UserRepository::insertUserValues(const model::User& user,
std::shared_ptr<UserRepository::UserLengths> lengths)
{
std::shared_ptr<MYSQL_BIND> values((MYSQL_BIND*) std::calloc(6, sizeof(MYSQL_BIND)));
values.get()[0].buffer_type = MYSQL_TYPE_STRING;
values.get()[0].buffer = (char*)user.firstname.c_str();
values.get()[0].length = &(lengths->firstnameLength);
values.get()[0].is_null = 0;
values.get()[1].buffer_type = MYSQL_TYPE_STRING;
values.get()[1].buffer = (char*)user.lastname.c_str();
values.get()[1].length = &(lengths->lastnameLength);
values.get()[1].is_null = 0;
values.get()[2].buffer_type = MYSQL_TYPE_STRING;
values.get()[2].buffer = (char*)user.phone.c_str();
values.get()[2].length = &(lengths->phoneLength);
values.get()[2].is_null = 0;
values.get()[3].buffer_type = MYSQL_TYPE_STRING;
values.get()[3].buffer = (char*)user.email.c_str();
values.get()[3].length = &(lengths->emailLength);
values.get()[3].is_null = 0;
values.get()[4].buffer_type = MYSQL_TYPE_STRING;
values.get()[4].buffer = (char*)user.username.c_str();
values.get()[4].length = &(lengths->usernameLength);
values.get()[4].is_null = 0;
values.get()[5].buffer_type = MYSQL_TYPE_STRING;
values.get()[5].buffer = (char*)user.password.c_str();
values.get()[5].length = &(lengths->passwordLength);
values.get()[5].is_null = 0;
return values;
}
std::shared_ptr<MYSQL_BIND> UserRepository::valueBind(model::User& user, std::shared_ptr<UserVals> userVal)
{
std::shared_ptr<MYSQL_BIND> values((MYSQL_BIND*) std::calloc(7, sizeof(MYSQL_BIND)));
constexpr auto strLen = 1024;
values.get()[0].buffer_type = MYSQL_TYPE_LONG;
values.get()[0].buffer = (char*)&user.id;
values.get()[1].buffer_type = MYSQL_TYPE_STRING;
values.get()[1].buffer = (char*)userVal->firstname;
values.get()[1].buffer_length = strLen;
values.get()[2].buffer_type = MYSQL_TYPE_STRING;
values.get()[2].buffer = (char*)userVal->lastname;
values.get()[2].buffer_length = strLen;
values.get()[3].buffer_type = MYSQL_TYPE_STRING;
values.get()[3].buffer = (char*)userVal->phone;
values.get()[3].buffer_length = strLen;
values.get()[4].buffer_type = MYSQL_TYPE_STRING;
values.get()[4].buffer = (char*)userVal->email;
values.get()[4].buffer_length = strLen;
values.get()[5].buffer_type = MYSQL_TYPE_STRING;
values.get()[5].buffer = (char*)userVal->username;
values.get()[5].buffer_length = strLen;
values.get()[6].buffer_type = MYSQL_TYPE_STRING;
values.get()[6].buffer = (char*)userVal->password;
values.get()[6].buffer_length = strLen;
return values;
}
std::shared_ptr<MYSQL_BIND> UserRepository::valueBind(model::User& user, UserVals& userVal)
{
std::shared_ptr<MYSQL_BIND> values((MYSQL_BIND*) std::calloc(7, sizeof(MYSQL_BIND)));
constexpr auto strLen = 1024;
values.get()[0].buffer_type = MYSQL_TYPE_LONG;
values.get()[0].buffer = (char*)&user.id;
values.get()[1].buffer_type = MYSQL_TYPE_STRING;
values.get()[1].buffer = (char*)userVal.firstname;
values.get()[1].buffer_length = strLen;
values.get()[2].buffer_type = MYSQL_TYPE_STRING;
values.get()[2].buffer = (char*)userVal.lastname;
values.get()[2].buffer_length = strLen;
values.get()[3].buffer_type = MYSQL_TYPE_STRING;
values.get()[3].buffer = (char*)userVal.phone;
values.get()[3].buffer_length = strLen;
values.get()[4].buffer_type = MYSQL_TYPE_STRING;
values.get()[4].buffer = (char*)userVal.email;
values.get()[4].buffer_length = strLen;
values.get()[5].buffer_type = MYSQL_TYPE_STRING;
values.get()[5].buffer = (char*)userVal.username;
values.get()[5].buffer_length = strLen;
values.get()[6].buffer_type = MYSQL_TYPE_STRING;
values.get()[6].buffer = (char*)userVal.password;
values.get()[6].buffer_length = strLen;
return values;
}
std::shared_ptr<MYSQL_BIND> UserRepository::valueBind(model::User& user)
{
std::shared_ptr<MYSQL_BIND> values((MYSQL_BIND*) std::calloc(7, sizeof(MYSQL_BIND)));
constexpr auto strLen = 1024;
values.get()[0].buffer_type = MYSQL_TYPE_LONG;
values.get()[0].buffer = (char*)&user.id;
values.get()[1].buffer_type = MYSQL_TYPE_STRING;
values.get()[1].buffer = (char*)user.firstname.data();
values.get()[1].buffer_length = strLen;
values.get()[2].buffer_type = MYSQL_TYPE_STRING;
values.get()[2].buffer = (char*)user.lastname.data();
values.get()[2].buffer_length = strLen;
values.get()[3].buffer_type = MYSQL_TYPE_STRING;
values.get()[3].buffer = (char*)user.phone.data();
values.get()[3].buffer_length = strLen;
values.get()[4].buffer_type = MYSQL_TYPE_STRING;
values.get()[4].buffer = (char*)user.email.data();
values.get()[4].buffer_length = strLen;
values.get()[5].buffer_type = MYSQL_TYPE_STRING;
values.get()[5].buffer = (char*)user.username.data();
values.get()[5].buffer_length = strLen;
values.get()[6].buffer_type = MYSQL_TYPE_STRING;
values.get()[6].buffer = (char*)user.password.data();
values.get()[6].buffer_length = strLen;
return values;
}
std::shared_ptr<UserRepository::UserLengths> UserRepository::fetchUserLengths(const model::User& user)
{
auto userLen = std::make_shared<UserLengths>();
userLen->firstnameLength = user.firstname.size();
userLen->lastnameLength = user.lastname.size();
userLen->phoneLength = user.phone.size();
userLen->emailLength = user.email.size();
userLen->usernameLength = user.username.size();
userLen->passwordLength = user.password.size();
return userLen;
}
std::shared_ptr<UserRepository::UserVals> UserRepository::fetchUserVals()
{
constexpr auto strLen = 1024;
auto userVal = std::make_shared<UserVals>();
/**
userVal->firstname = std::make_shared<char*>(new char[strLen]);
userVal->lastname = std::make_shared<char*>(new char[strLen]);
userVal->email = std::make_shared<char*>(new char[strLen]);
userVal->phone = std::make_shared<char*>(new char[strLen]);
userVal->username = std::make_shared<char*>(new char[strLen]);
userVal->password = std::make_shared<char*>(new char[strLen]);
*/
return userVal;
}
model::User UserRepository::parseRecord(MYSQL_STMT *stmt)
{
// TODO : parse user record
model::User user;
auto userVal = fetchUserVals();
UserVals uv;
//auto bindedValues = valueBind(user, userVal);
auto bindedValues = valueBind(user, uv);
//auto bindedValues = valueBind(user);
auto status = mysql_stmt_bind_result(stmt, bindedValues.get());
status = mysql_stmt_fetch(stmt);
std::cout << user.id << std::endl;
//std::cout << "binded firstname: " << userVal->firstname << std::endl;
//std::cout << "binded lastname: " << uv.lastname << std::endl;
//user.firstname = userVal->firstname;
/**
user.lastname = *userVal->lastname.get();
user.email = *userVal->email.get();
user.phone = *userVal->phone.get();
user.username = *userVal->username.get();
user.password = *userVal->password.get();
*/
return user;
}
}
+59
View File
@@ -0,0 +1,59 @@
#include "manager/UserManager.h"
#include "database/UserRepository.h"
#include "type/UserFilter.h"
#include "utility/MetadataRetriever.h"
#include "utility/PasswordEncryption.h"
namespace manager {
UserManager::UserManager(const model::BinaryPath& bConf) : m_bConf(bConf) { }
model::User UserManager::userDtoConv(dto::UserDto::ObjectWrapper& userDto)
{
model::User user;
user.id = (userDto->userId.getPtr() == nullptr) ? 0 : userDto->userId->getValue();
user.firstname = (userDto->firstname == nullptr) ? "" : userDto->firstname->c_str();
user.lastname = (userDto->lastname == nullptr) ? "" : userDto->lastname->c_str();
user.phone = (userDto->phone == nullptr) ? "" : userDto->phone->c_str();
user.email = (userDto->email == nullptr) ? "" : userDto->email->c_str();
user.username = (userDto->username == nullptr) ? "" : userDto->username->c_str();
user.password = (userDto->password == nullptr) ? "" : userDto->password->c_str();
return user;
}
void UserManager::printUser(const model::User& user)
{
std::cout << "\nuser info" << std::endl;
std::cout << "id: " << user.id << std::endl;
std::cout << "firstname: " << user.firstname << std::endl;
std::cout << "lastname: " << user.lastname << std::endl;
std::cout << "phone: " << user.phone << std::endl;
std::cout << "email: " << user.email << std::endl;
std::cout << "username: " << user.username << std::endl;
std::cout << "password: " << user.password<< std::endl;
}
void UserManager::registerUser(model::User& user)
{
printUser(user);
utility::PasswordEncryption passEnc;
auto hashed = passEnc.hashPassword(user);
user.password = hashed.hashPassword;
database::UserRepository usrRepo(m_bConf);
usrRepo.saveUserRecord(user);
model::User usr;
usr.username = user.username;
std::cout << usr.username << std::endl;
usr = usrRepo.retrieveUserRecord(usr, type::UserFilter::username);
printUser(usr);
}
}
+37
View File
@@ -0,0 +1,37 @@
#include "utility/PasswordEncryption.h"
#include <iostream>
#include <memory>
#include <bcrypt.h>
namespace utility {
model::PassSec PasswordEncryption::hashPassword(const model::User& user)
{
model::PassSec passSec;
std::unique_ptr<char[]> salt(new char[BCRYPT_HASHSIZE]);
std::unique_ptr<char[]> hash(new char[BCRYPT_HASHSIZE]);
std::cout << "generating salt" << std::endl;
bcrypt_gensalt(saltSize(), salt.get());
std::cout << "hashing password" << std::endl;
bcrypt_hashpw(user.password.c_str(), salt.get(), hash.get());
passSec.salt = salt.get();
passSec.hashPassword = hash.get();
std::cout << "hash: " << passSec.hashPassword << std::endl;
std::cout << "salt: " << passSec.salt << std::endl;
return passSec;
}
int PasswordEncryption::saltSize()
{
constexpr auto size = 10000;
return size;
}
}
+15 -5
View File
@@ -10,8 +10,16 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace verify {
bool Initialization::skipVerification(const std::string& argument)
{
return argument.compare("--noverify") == 0;
}
// verifies if the configuration settings are valid // verifies if the configuration settings are valid
void verify::Initialization::checkIcarus(const model::BinaryPath& bConf) void Initialization::checkIcarus(const model::BinaryPath& bConf)
{ {
auto auth = confirmConfigAuth(bConf); auto auth = confirmConfigAuth(bConf);
auto database = confirmConfigDatabase(bConf); auto database = confirmConfigDatabase(bConf);
@@ -26,7 +34,7 @@ void verify::Initialization::checkIcarus(const model::BinaryPath& bConf)
// verifies that the authorization settings are not the default values // verifies that the authorization settings are not the default values
bool verify::Initialization::confirmConfigAuth(const model::BinaryPath& bConf) bool Initialization::confirmConfigAuth(const model::BinaryPath& bConf)
{ {
manager::TokenManager tokMgr; manager::TokenManager tokMgr;
@@ -34,7 +42,7 @@ bool verify::Initialization::confirmConfigAuth(const model::BinaryPath& bConf)
} }
// verifies if database connectivity can be established // verifies if database connectivity can be established
bool verify::Initialization::confirmConfigDatabase(const model::BinaryPath& bConf) bool Initialization::confirmConfigDatabase(const model::BinaryPath& bConf)
{ {
database::BaseRepository baseRepo(bConf); database::BaseRepository baseRepo(bConf);
@@ -42,7 +50,7 @@ bool verify::Initialization::confirmConfigDatabase(const model::BinaryPath& bCon
} }
// verifies if the paths found in the config files exists // verifies if the paths found in the config files exists
bool verify::Initialization::confirmConfigPaths(const model::BinaryPath& bConf) bool Initialization::confirmConfigPaths(const model::BinaryPath& bConf)
{ {
auto pathConfig = manager::DirectoryManager::pathConfigContent(bConf); auto pathConfig = manager::DirectoryManager::pathConfigContent(bConf);
@@ -96,8 +104,10 @@ bool verify::Initialization::confirmConfigPaths(const model::BinaryPath& bConf)
// confirmation failed // confirmation failed
void verify::Initialization::failedConfirmation() void Initialization::failedConfirmation()
{ {
std::cout << "configuration confirmation failed. check your settings" << std::endl; std::cout << "configuration confirmation failed. check your settings" << std::endl;
std::exit(-1); std::exit(-1);
} }
}