Mantis App v0.1.13
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
9#ifndef MANTIS_UTILS_H
10#define MANTIS_UTILS_H
11
12#include <string>
13#include <filesystem>
14#include <random>
15
16#include "../core/logging.h"
17
18namespace mantis
19{
20 namespace fs = std::filesystem;
21
22 // ----------------------------------------------------------------- //
23 // PATH UTILS
24 // ----------------------------------------------------------------- //
25
32 fs::path joinPaths(const std::string& path1, const std::string& path2);
33
44 fs::path resolvePath(const std::string& input_path);
45
55 bool createDirs(const fs::path& path);
56
68 std::string dirFromPath(const std::string& path);
69
70 // ----------------------------------------------------------------- //
71 // STRING UTILS
72 // ----------------------------------------------------------------- //
81 void toLowerCase(std::string& str);
82
91 void toUpperCase(std::string& str);
92
99 std::string trim(const std::string& s);
100
113 std::optional<json> tryParseJsonStr(const std::string& json_str);
114
128 std::string generateTimeBasedId();
129
143 std::string generateReadableTimeId();
144
158 std::string generateShortId(size_t length = 12);
159
173 std::vector<std::string> splitString(const std::string& input, const std::string& delimiter);
174
181 std::string getEnvOrDefault(const std::string& key, const std::string& defaultValue);
182
183
184 // ----------------------------------------------------------------- //
185 // AUTH UTILS
186 // ----------------------------------------------------------------- //
193 std::string bcryptBase64Encode(const unsigned char* data, size_t len);
194
202 std::string generateSalt(int cost = 12);
203
209 json hashPassword(const std::string& password);
210
221 json verifyPassword(const std::string& password, const std::string& stored_hash);
222
223}
224
225#endif // MANTIS_UTILS_H
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:30
json hashPassword(const std::string &password)
Digests user password + a generated salt to yield a hashed password.
Definition auth_utils.cpp:49
std::string generateTimeBasedId()
Generate a time base UUID.
Definition string_utils.cpp:42
std::string generateReadableTimeId()
Generates a readable time-based UUID.
Definition string_utils.cpp:59
fs::path joinPaths(const std::string &path1, const std::string &path2)
< Use shorthand fs to refer to the std::filesystem
Definition path_utils.cpp:8
std::string getEnvOrDefault(const std::string &key, const std::string &defaultValue)
Retrieves a value from an environment variable or a default value if the env variable was not set.
Definition string_utils.cpp:116
bool createDirs(const fs::path &path)
Create directory, given a path.
Definition path_utils.cpp:27
std::string generateSalt(int cost=12)
Generates a salt to be used in hashing user passwords.
Definition auth_utils.cpp:35
std::string trim(const std::string &s)
Trims leading and trailing whitespaces from a string.
Definition string_utils.cpp:35
std::optional< json > tryParseJsonStr(const std::string &json_str)
Attempt to parse a JSON string.
Definition string_utils.cpp:9
std::string bcryptBase64Encode(const unsigned char *data, size_t len)
Encode a Salt string to bcrypt base64 format.
Definition auth_utils.cpp:14
void toUpperCase(std::string &str)
Converts a string to its uppercase variant.
Definition string_utils.cpp:29
std::string dirFromPath(const std::string &path)
Returns a created/existing directory from a path.
Definition path_utils.cpp:46
fs::path resolvePath(const std::string &input_path)
Definition path_utils.cpp:14
json verifyPassword(const std::string &password, const std::string &stored_hash)
Verifies user password if it matches the given hashed password.
Definition auth_utils.cpp:85
void toLowerCase(std::string &str)
Converts a string to its lowercase variant.
Definition string_utils.cpp:23
std::string generateShortId(size_t length=12)
Generates a short UUID.
Definition string_utils.cpp:81
std::vector< std::string > splitString(const std::string &input, const std::string &delimiter)
Split given string based on given delimiter.
Definition string_utils.cpp:100