Skip to content

Commit 55575b7

Browse files
authored
Add password salting and minor corrections (#6)
1 parent 3e91b00 commit 55575b7

File tree

19 files changed

+60
-102
lines changed

19 files changed

+60
-102
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Reuse ccache directory
3535
uses: actions/cache@v4
3636
with:
37-
path: ~/.ccache
37+
path: ~/.cache/ccache
3838
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
3939
restore-keys: |
4040
${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-'

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ compile_commands.json
88
.ccache/
99
cmake-build-*
1010
Testing/
11-
configs/static_config.yaml
1211
.DS_Store

CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ add_library(${PROJECT_NAME}_objs OBJECT
9797
src/validators/user_validators.cpp
9898
src/validators/length_validator.hpp
9999
src/validators/length_validator.cpp
100+
src/utils/random.hpp
101+
src/utils/random.cpp
100102
src/utils/errors.hpp
101103
src/utils/errors.cpp
102104
src/utils/make_error.hpp
@@ -142,7 +144,6 @@ add_google_tests(${PROJECT_NAME}_unittest)
142144
# Functional Tests
143145
add_subdirectory(tests)
144146

145-
146147
# Install
147148
include(GNUInstallDirs)
148149

@@ -152,12 +153,8 @@ if(DEFINED ENV{PREFIX})
152153
set(CMAKE_INSTALL_PREFIX ${PREFIX_PATH})
153154
endif()
154155

155-
set(CONFIG_VAR_PATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}/config_vars.yaml)
156-
set(CONFIG_FALLBACK_PATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}/dynamic_config_fallback.json)
157156
set(CONFIG_JWT ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}/jwt_config.json)
158157

159-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml.in ${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml)
160-
161158
file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.json)
162159

163160
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})

configs/dynamic_config_fallback.json

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# yaml
2-
3-
config_vars: @CONFIG_VAR_PATH@
4-
51
components_manager:
62
coro_pool:
73
initial_size: 500 # Preallocate 500 coroutines at startup.

postgresql/schemas/db-1.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS real_medium.users(
99
bio text,
1010
image varchar(255),
1111
password_hash varchar(255) NOT NULL,
12+
salt varchar(255) NOT NULL,
1213
CONSTRAINT uniq_username UNIQUE (username),
1314
CONSTRAINT uniq_email UNIQUE (email)
1415
);
@@ -77,7 +78,8 @@ CREATE TYPE real_medium.user AS (
7778
email text,
7879
bio TEXT,
7980
image VARCHAR(255),
80-
password_hash TEXT
81+
password_hash TEXT,
82+
salt TEXT
8183
);
8284

8385
CREATE TYPE real_medium.full_article_info AS (

src/db/sql.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace real_medium::sql {
66

77
inline constexpr std::string_view kInsertUser = R"~(
8-
INSERT INTO real_medium.users(username, email, password_hash)
9-
VALUES($1, $2, $3)
8+
INSERT INTO real_medium.users(username, email, password_hash, salt)
9+
VALUES($1, $2, $3, $4)
1010
RETURNING *
1111
)~";
1212

@@ -21,7 +21,8 @@ UPDATE real_medium.users SET
2121
email = COALESCE($3, email),
2222
bio = COALESCE($4, bio),
2323
image = COALESCE($5, image),
24-
password_hash = COALESCE($6, password_hash)
24+
password_hash = COALESCE($6, password_hash),
25+
salt = COALESCE($7, salt)
2526
WHERE user_id = $1
2627
RETURNING *
2728
)~";
@@ -111,6 +112,10 @@ SELECT profile.username, profile.bio, profile.image,
111112
FROM profile
112113
)~";
113114

115+
inline constexpr std::string_view kGetSaltByEmail = R"~(
116+
SELECT salt FROM real_medium.users WHERE email = $1
117+
)~";
118+
114119
inline constexpr std::string_view KFollowingUser = R"~(
115120
WITH profile AS (
116121
SELECT * FROM real_medium.users WHERE user_id = $1
@@ -235,7 +240,7 @@ SELECT a.article_id AS articleId,
235240
FROM real_medium.followers fl
236241
WHERE fl.followed_user_id = a.user_id
237242
) AS author_followed_by_user_ids,
238-
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash)::real_medium.user AS author_info
243+
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash, u.salt)::real_medium.user AS author_info
239244
FROM real_medium.articles a
240245
JOIN real_medium.users u ON a.user_id = u.user_id
241246
)~"};
@@ -247,7 +252,7 @@ SELECT c.comment_id,
247252
c.body,
248253
c.user_id,
249254
a.slug,
250-
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash)::real_medium.user AS author_info,
255+
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash, u.salt)::real_medium.user AS author_info,
251256
ARRAY(
252257
SELECT follower_user_id
253258
FROM real_medium.followers fl

src/handlers/comments/comment_delete.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
1818
const userver::formats::json::Value& /*request_json*/,
1919
userver::server::request::RequestContext& context) const {
2020
auto user_id = context.GetData<std::optional<std::string>>("id");
21-
const auto& comment_id = std::atoi(request.GetPathArg("id").c_str());
21+
const auto& comment_id = userver::utils::FromString<int, std::string>(request.GetPathArg("id"));
2222
const auto& slug = request.GetPathArg("slug");
2323

2424
const auto result_find_comment = pg_cluster_->Execute(

src/handlers/comments/comment_delete.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <fmt/format.h>
55
#include <string>
66
#include <string_view>
7+
#include <userver/utils/from_string.hpp>
78

89
#include "userver/server/handlers/http_handler_base.hpp"
910
#include "userver/server/handlers/http_handler_json_base.hpp"

src/handlers/profiles/profiles.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "userver/storages/postgres/cluster.hpp"
1111
#include "userver/storages/postgres/component.hpp"
1212

13-
using namespace std;
1413
using namespace userver::formats;
1514
using namespace userver::server::http;
1615
using namespace userver::server::request;

0 commit comments

Comments
 (0)