diff --git a/.github/actions/configure-PostgreSQL/action.yml b/.github/actions/configure-PostgreSQL/action.yml index a771a0c0..7e9e0135 100644 --- a/.github/actions/configure-PostgreSQL/action.yml +++ b/.github/actions/configure-PostgreSQL/action.yml @@ -9,6 +9,7 @@ runs: if: runner.os == 'Linux' run: | sudo systemctl start postgresql.service + sudo -u postgres ./scripts/configure_postgres.sh - name: Start MacOS PostgreSQL service shell: bash @@ -16,6 +17,7 @@ runs: run: | brew install postgresql brew services start postgresql@14 + ./scripts/configure_postgres.sh - name: Start Windows PostgreSQL service shell: bash @@ -26,9 +28,4 @@ runs: net start $serviceName export PGPASSWORD="root" export PATH="$PATH:/c/Program Files/PostgreSQL/14/bin:/c/Program Files/PostgreSQL/14/lib" - - - name: Create Database - shell: bash - run: | - psql postgres -c "CREATE USER $DATABASE_USER WITH CREATEDB PASSWORD '${DATABASE_PASSWORD}';" - psql postgres -c "CREATE DATABASE $DATABASE_NAME WITH OWNER $DATABASE_USER;" + ./scripts/configure_postgres.sh \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 97f92d62..68e4985b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,5 +86,6 @@ endif() include(cmake/CopyHelper.cmake) copy_files(FILES tastatura.txt COPY_TO_DESTINATION TARGET_NAME ${MAIN_EXECUTABLE_NAME}) +copy_files(FILES .env COPY_TO_DESTINATION TARGET_NAME ${MAIN_EXECUTABLE_NAME}) # copy_files(FILES tastatura.txt config.json DIRECTORY images sounds COPY_TO_DESTINATION TARGET_NAME ${MAIN_EXECUTABLE_NAME}) # copy_files(DIRECTORY images sounds COPY_TO_DESTINATION TARGET_NAME ${MAIN_EXECUTABLE_NAME}) diff --git a/database/Database.cpp b/database/Database.cpp index c1f17e73..173b821d 100644 --- a/database/Database.cpp +++ b/database/Database.cpp @@ -3,7 +3,6 @@ Database::Database(const bool& withDropTables) { setConnectionString(); - connString = "dbname=oop_db user=oop password=ooppa55 hostaddr=127.0.0.1 port=5432 connect_timeout=10"; connection = std::make_unique(connString); if (!connection->is_open()) throw std::runtime_error("Database exists but a connection couldn't be established"); @@ -30,7 +29,7 @@ void Database::createTables(){ CREATE TABLE IF NOT EXISTS USERS ( id SERIAL PRIMARY KEY, name VARCHAR(20) NOT NULL, - password VARCHAR(20) NOT NULL + password VARCHAR(255) NOT NULL ) )"; @@ -50,8 +49,11 @@ void Database::dropTables(){ void Database::addUser(std::string name, std::string password) { pqxx::work transaction(*connection); + std::string hashQuery = "SELECT crypt($1, gen_salt('bf')) AS hashed_password;"; + pqxx::result hashResult = transaction.exec_params(hashQuery,password); + std::string hashedPassword = hashResult[0]["hashed_password"].c_str(); std::string insertUserQuery = "INSERT INTO USERS (name, password) VALUES ($1, $2);"; - transaction.exec_params(insertUserQuery, name, password); + transaction.exec_params(insertUserQuery, name, hashedPassword); transaction.commit(); } diff --git a/scripts/configure_postgres.sh b/scripts/configure_postgres.sh new file mode 100644 index 00000000..43ef15d9 --- /dev/null +++ b/scripts/configure_postgres.sh @@ -0,0 +1,5 @@ +#!/usr/bin/bash + +psql -h 127.0.0.1 -U postgres -c "CREATE USER $DATABASE_USER WITH CREATEDB PASSWORD '$DATABASE_PASSWORD';" +psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE $DATABASE_NAME WITH OWNER $DATABASE_USER;" +psql -h 127.0.0.1 -U postgres -d $DATABASE_NAME -c "CREATE EXTENSION IF NOT EXISTS pgcrypto;" \ No newline at end of file