diff --git a/.gitmodules b/.gitmodules index 03137ae..4455ce3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "3rdparty/ormpp"] path = 3rdparty/ormpp url = https://github.com/qicosmos/ormpp +[submodule "3rdparty/libbcrypt"] + path = 3rdparty/libbcrypt + url = https://github.com/rg3/libbcrypt diff --git a/3rdparty/libbcrypt b/3rdparty/libbcrypt new file mode 160000 index 0000000..8aa32ad --- /dev/null +++ b/3rdparty/libbcrypt @@ -0,0 +1 @@ +Subproject commit 8aa32ad94ebe06b76853b0767c910c9fbf7ccef4 diff --git a/3rdparty/oatpp b/3rdparty/oatpp index 2156421..5b4b313 160000 --- a/3rdparty/oatpp +++ b/3rdparty/oatpp @@ -1 +1 @@ -Subproject commit 215642129d79d9d0335b4618e75dd2ba634aceb2 +Subproject commit 5b4b313a0bc57e48abfd9c85d7d7c42a1bbdef70 diff --git a/CMakeLists.txt b/CMakeLists.txt index 3323eea..4d52c10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES src/database/CoverArtRepository.cpp src/database/GenreRepository.cpp src/database/SongRepository.cpp + src/database/UserRepository.cpp src/database/YearRepository.cpp src/Main.cpp src/manager/AlbumManager.cpp @@ -22,9 +23,11 @@ set(SOURCES src/manager/GenreManager.cpp src/manager/SongManager.cpp src/manager/TokenManager.cpp + src/manager/UserManager.cpp src/manager/YearManager.cpp src/utility/ImageFile.cpp src/utility/MetadataRetriever.cpp + src/utility/PasswordEncryption.cpp src/verify/Initialization.cpp ) set(HEADERS @@ -35,6 +38,7 @@ set(HEADERS include/controller/CoverArtController.hpp include/controller/GenreController.hpp include/controller/LoginController.hpp + include/controller/RegisterController.hpp include/controller/SongController.hpp include/controller/YearController.hpp include/database/AlbumRepository.h @@ -43,6 +47,7 @@ set(HEADERS include/database/CoverArtRepository.h include/database/GenreRepository.h include/database/SongRepository.h + include/database/UserRepository.h include/database/YearRepository.h include/dto/AlbumDto.hpp include/dto/ArtistDto.hpp @@ -57,19 +62,23 @@ set(HEADERS include/manager/DirectoryManager.h include/manager/GenreManager.h include/manager/SongManager.h - include/manager/TokenManager.h + include/manager/TokenManager.h + include/manager/UserManager.h include/manager/YearManager.h include/model/Models.h include/utility/ImageFile.h include/utility/MetadataRetriever.h + include/utility/PasswordEncryption.h include/type/AlbumFilter.h include/type/ArtistFilter.h include/type/CoverFilter.h include/type/GenreFilter.h include/type/PathType.h + include/type/SaltFilter.h include/type/Scopes.h include/type/SongChanged.h include/type/SongFilter.h + include/type/UserFilter.h include/type/YearFilter.h include/verify/Initialization.h ) @@ -78,6 +87,10 @@ set (TAGLIB ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib/3rdparty ) +set (BCRYPTLIB + ${CMAKE_SOURCE_DIR}/3rdparty/libbcrypt +) + set(TAGLIB_HEADERS "${CMAKE_SOURCE_DIR}/build/3rdparty/taglib" "${CMAKE_SOURCE_DIR}/3rdparty/taglib" @@ -114,11 +127,14 @@ set (ORM_DIR include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) 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/taglib) 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}/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}) 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}) diff --git a/Scripts/MySQL/create_database.sql b/Scripts/MySQL/create_database.sql index 8680c54..fde8f28 100644 --- a/Scripts/MySQL/create_database.sql +++ b/Scripts/MySQL/create_database.sql @@ -75,3 +75,12 @@ CREATE TABLE User ( 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) +); diff --git a/include/controller/RegisterController.hpp b/include/controller/RegisterController.hpp new file mode 100644 index 0000000..6724501 --- /dev/null +++ b/include/controller/RegisterController.hpp @@ -0,0 +1,40 @@ +#ifndef REGISTERCONTROLLER_H_ +#define REGISTERCONTROLLER_H_ + +#include +#include + +#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)) : + 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 diff --git a/include/database/UserRepository.h b/include/database/UserRepository.h new file mode 100644 index 0000000..88710a9 --- /dev/null +++ b/include/database/UserRepository.h @@ -0,0 +1,60 @@ +#ifndef USERREPOSITORY_H_ +#define USERREPOSITORY_H_ + +#include +#include + +#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 firstname; + std::shared_ptr lastname; + std::shared_ptr phone; + std::shared_ptr email; + std::shared_ptr username; + std::shared_ptr password; + */ + char firstname[1024]; + char lastname[1024]; + char phone[1024]; + char email[1024]; + char username[1024]; + char password[1024]; + }; + + std::shared_ptr insertUserValues(const model::User&, std::shared_ptr); + std::shared_ptr valueBind(model::User&, std::shared_ptr); + std::shared_ptr valueBind(model::User&, UserVals&); + std::shared_ptr valueBind(model::User&); + std::shared_ptr fetchUserLengths(const model::User&); + std::shared_ptr fetchUserVals(); + + model::User parseRecord(MYSQL_STMT*); + }; +} + + +#endif diff --git a/include/dto/LoginResultDto.hpp b/include/dto/LoginResultDto.hpp index adf1004..43f22d6 100644 --- a/include/dto/LoginResultDto.hpp +++ b/include/dto/LoginResultDto.hpp @@ -24,6 +24,11 @@ namespace dto { 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, password); }; diff --git a/include/manager/UserManager.h b/include/manager/UserManager.h new file mode 100644 index 0000000..fa65f44 --- /dev/null +++ b/include/manager/UserManager.h @@ -0,0 +1,25 @@ +#ifndef USERMANAGER_H_ +#define USERMANAGER_H_ + +#include + +#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 diff --git a/include/model/Models.h b/include/model/Models.h index 6c4194c..feb4ebf 100644 --- a/include/model/Models.h +++ b/include/model/Models.h @@ -116,6 +116,23 @@ namespace model 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 { std::string domain; diff --git a/include/type/SaltFilter.h b/include/type/SaltFilter.h new file mode 100644 index 0000000..b3efa14 --- /dev/null +++ b/include/type/SaltFilter.h @@ -0,0 +1,12 @@ +#ifndef SALTFILTER_H_ +#define SALTFILTER_H_ + +namespace type { + enum class SaltFilter + { + id = 0, + salt + }; +} + +#endif diff --git a/include/type/UserFilter.h b/include/type/UserFilter.h new file mode 100644 index 0000000..0d5c2ac --- /dev/null +++ b/include/type/UserFilter.h @@ -0,0 +1,12 @@ +#ifndef USERFILTER_H_ +#define USERFILTER_H_ + +namespace type { + enum class UserFilter + { + id = 0, + username + }; +} + +#endif diff --git a/include/utility/PasswordEncryption.h b/include/utility/PasswordEncryption.h new file mode 100644 index 0000000..e5662db --- /dev/null +++ b/include/utility/PasswordEncryption.h @@ -0,0 +1,17 @@ +#ifndef PASSWORDENCRYPTION_H_ +#define PASSWORDENCRYPTION_H_ + +#include + +#include "model/Models.h" + +namespace utility { + class PasswordEncryption { + public: + model::PassSec hashPassword(const model::User&); + private: + int saltSize(); + }; +} + +#endif diff --git a/include/verify/Initialization.h b/include/verify/Initialization.h index ff1a2ff..c5e0145 100644 --- a/include/verify/Initialization.h +++ b/include/verify/Initialization.h @@ -1,6 +1,8 @@ #ifndef INITIALIZATION_H_ #define INITIALIZATION_H_ +#include + #include "model/Models.h" namespace verify @@ -8,6 +10,8 @@ namespace verify class Initialization { public: + static bool skipVerification(const std::string&); + static void checkIcarus(const model::BinaryPath&); private: static bool confirmConfigAuth(const model::BinaryPath&); diff --git a/src/Main.cpp b/src/Main.cpp index e0a7cd6..345a2bd 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -15,6 +15,7 @@ #include "controller/CoverArtController.hpp" #include "controller/GenreController.hpp" #include "controller/LoginController.hpp" +#include "controller/RegisterController.hpp" #include "controller/SongController.hpp" #include "controller/YearController.hpp" #include "model/Models.h" @@ -33,6 +34,7 @@ void run(const model::BinaryPath& bConf) auto coverArtController = std::make_shared(bConf); auto gnrController = std::make_shared(bConf); auto logController = std::make_shared(bConf); + auto regController = std::make_shared(bConf); auto sngController = std::make_shared(bConf); auto yearController = std::make_shared(bConf); @@ -41,6 +43,7 @@ void run(const model::BinaryPath& bConf) coverArtController->addEndpointsToRouter(router); gnrController->addEndpointsToRouter(router); logController->addEndpointsToRouter(router); + regController->addEndpointsToRouter(router); sngController->addEndpointsToRouter(router); yearController->addEndpointsToRouter(router); @@ -59,8 +62,17 @@ int main(int argc, char **argv) { oatpp::base::Environment::init(); - model::BinaryPath bConf(argv[0]); - verify::Initialization::checkIcarus(bConf); + model::BinaryPath bConf(std::move(argv[0])); + + if (argc > 1) { + if (!verify::Initialization::skipVerification(argv[1])) { + verify::Initialization::checkIcarus(bConf); + } else { + std::cout << "skiping verifyication" << std::endl; + } + } else { + verify::Initialization::checkIcarus(bConf); + } run(bConf); diff --git a/src/database/UserRepository.cpp b/src/database/UserRepository.cpp new file mode 100644 index 0000000..da03b36 --- /dev/null +++ b/src/database/UserRepository.cpp @@ -0,0 +1,277 @@ +#include "database/UserRepository.h" + +#include +#include +#include + +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 UserRepository::insertUserValues(const model::User& user, + std::shared_ptr lengths) +{ + std::shared_ptr 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 UserRepository::valueBind(model::User& user, std::shared_ptr userVal) +{ + std::shared_ptr 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 UserRepository::valueBind(model::User& user, UserVals& userVal) +{ + std::shared_ptr 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 UserRepository::valueBind(model::User& user) +{ + std::shared_ptr 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::fetchUserLengths(const model::User& user) +{ + auto userLen = std::make_shared(); + 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::fetchUserVals() +{ + constexpr auto strLen = 1024; + auto userVal = std::make_shared(); + /** + userVal->firstname = std::make_shared(new char[strLen]); + userVal->lastname = std::make_shared(new char[strLen]); + userVal->email = std::make_shared(new char[strLen]); + userVal->phone = std::make_shared(new char[strLen]); + userVal->username = std::make_shared(new char[strLen]); + userVal->password = std::make_shared(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; +} +} diff --git a/src/manager/UserManager.cpp b/src/manager/UserManager.cpp new file mode 100644 index 0000000..8c7940e --- /dev/null +++ b/src/manager/UserManager.cpp @@ -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); +} + +} diff --git a/src/utility/PasswordEncryption.cpp b/src/utility/PasswordEncryption.cpp new file mode 100644 index 0000000..74af613 --- /dev/null +++ b/src/utility/PasswordEncryption.cpp @@ -0,0 +1,37 @@ +#include "utility/PasswordEncryption.h" + +#include +#include + +#include + +namespace utility { + +model::PassSec PasswordEncryption::hashPassword(const model::User& user) +{ + model::PassSec passSec; + + std::unique_ptr salt(new char[BCRYPT_HASHSIZE]); + std::unique_ptr 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; +} +} diff --git a/src/verify/Initialization.cpp b/src/verify/Initialization.cpp index d8e98da..b0c26f7 100644 --- a/src/verify/Initialization.cpp +++ b/src/verify/Initialization.cpp @@ -10,8 +10,16 @@ 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 -void verify::Initialization::checkIcarus(const model::BinaryPath& bConf) +void Initialization::checkIcarus(const model::BinaryPath& bConf) { auto auth = confirmConfigAuth(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 -bool verify::Initialization::confirmConfigAuth(const model::BinaryPath& bConf) +bool Initialization::confirmConfigAuth(const model::BinaryPath& bConf) { manager::TokenManager tokMgr; @@ -34,7 +42,7 @@ bool verify::Initialization::confirmConfigAuth(const model::BinaryPath& bConf) } // 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); @@ -42,7 +50,7 @@ bool verify::Initialization::confirmConfigDatabase(const model::BinaryPath& bCon } // 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); @@ -96,8 +104,10 @@ bool verify::Initialization::confirmConfigPaths(const model::BinaryPath& bConf) // confirmation failed -void verify::Initialization::failedConfirmation() +void Initialization::failedConfirmation() { std::cout << "configuration confirmation failed. check your settings" << std::endl; std::exit(-1); } + +}