Horizon Official Technical Documentation
Auth.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_AUTH_HPP
31#define HORIZON_AUTH_HPP
32
33#include "Server/pch.hpp"
34
36
37namespace Horizon
38{
39namespace Auth
40{
45 std::string _password_salt_mix;
46
48 {
49 std::string _name, _host;
50 uint16_t _port, _type, _is_new;
51 };
52
53 void add_char_server(char_server c) { _char_servers.push_back(c); }
54 std::vector<char_server> &get_char_servers() { return _char_servers; }
55
57 void set_max_network_threads(int threads) { _max_network_threads = threads; }
58
60 void set_session_max_timeout(int timeout) { _session_max_timeout = timeout; }
61
62 std::vector<char_server> _char_servers;
65};
66
67const int SALT_LEN = 16;
68const int HASH_LEN = 32;
69const int ITERATIONS = 10000;
70
71class AuthServer : public Server
72{
73public:
74 AuthServer();
76
78 {
79 static AuthServer instance;
80 return &instance;
81 }
82
83 bool read_config();
84
85 void initialize() override;
86 void finalize() override;
87
88 void generate_salt(std::vector<unsigned char>& salt) {
89 salt.resize(SALT_LEN);
90 if (!RAND_bytes(salt.data(), SALT_LEN)) {
91 throw std::runtime_error("Failed to generate salt");
92 }
93 }
94
95 void hash_password(const std::string& password, const std::vector<unsigned char>& salt, std::vector<unsigned char>& hash) {
96 hash.resize(HASH_LEN);
97 if (!PKCS5_PBKDF2_HMAC(password.c_str(), password.length(), salt.data(), salt.size(), ITERATIONS, EVP_sha256(), HASH_LEN, hash.data())) {
98 throw std::runtime_error("Failed to hash password");
99 }
100 }
101
102 /* CLI */
104 bool clicmd_reload_config(std::string /*cmd*/);
105 bool clicmd_create_new_account(std::string /*cmd*/);
106 bool clicmd_reset_password(std::string /*cmd*/);
107
109
110 /* Task Scheduler */
112
114 {
115 std::lock_guard<std::mutex> lock(_conf_lock);
116 return _auth_config;
117 }
118
119 void update(uint64_t time);
120
121protected:
123 std::mutex _conf_lock;
125 boost::asio::deadline_timer _update_timer;
126
127};
128}
129}
130
131#define sAuth Horizon::Auth::AuthServer::getInstance()
132
133#endif /* HORIZON_AUTH_HPP */
Definition: Auth.hpp:72
bool clicmd_reset_password(std::string)
Definition: Auth.cpp:204
TaskScheduler & getScheduler()
Definition: Auth.hpp:111
~AuthServer()
Horizon Destructor.
Definition: Auth.cpp:50
void hash_password(const std::string &password, const std::vector< unsigned char > &salt, std::vector< unsigned char > &hash)
Definition: Auth.hpp:95
TaskScheduler _task_scheduler
Definition: Auth.hpp:122
void initialize() override
Definition: Auth.cpp:342
std::mutex _conf_lock
Definition: Auth.hpp:123
auth_config_type & get_auth_config()
Definition: Auth.hpp:113
auth_config_type _auth_config
Definition: Auth.hpp:124
void initialize_cli_commands()
Initialize CLI Comamnds.
Definition: Auth.cpp:253
void verify_connected_sessions()
Definition: Auth.cpp:263
bool clicmd_reload_config(std::string)
CLI Command: Reload Configuration.
Definition: Auth.cpp:142
bool clicmd_create_new_account(std::string)
Definition: Auth.cpp:149
void generate_salt(std::vector< unsigned char > &salt)
Definition: Auth.hpp:88
boost::asio::deadline_timer _update_timer
Definition: Auth.hpp:125
static AuthServer * getInstance()
Definition: Auth.hpp:77
AuthServer()
Horizon Constructor.
Definition: Auth.cpp:42
void finalize() override
Definition: Auth.cpp:390
void update(uint64_t time)
Definition: Auth.cpp:311
bool read_config()
Read /config/horizon-server.yaml.
Definition: Auth.cpp:61
Definition: Server.hpp:554
The TaskScheduler class provides the ability to schedule std::function's in the near future....
Definition: TaskScheduler.hpp:58
const int HASH_LEN
Definition: Auth.hpp:68
const int ITERATIONS
Definition: Auth.hpp:69
const int SALT_LEN
Definition: Auth.hpp:67
Definition: Element.hpp:7
uint16_t _is_new
Definition: Auth.hpp:50
std::string _name
Definition: Auth.hpp:49
uint16_t _port
Definition: Auth.hpp:50
std::string _host
Definition: Auth.hpp:49
uint16_t _type
Definition: Auth.hpp:50
Main Auth Server Singleton Class.
Definition: Auth.hpp:44
void set_session_max_timeout(int timeout)
Definition: Auth.hpp:60
int _max_network_threads
Definition: Auth.hpp:63
void add_char_server(char_server c)
Definition: Auth.hpp:53
void set_max_network_threads(int threads)
Definition: Auth.hpp:57
std::vector< char_server > & get_char_servers()
Definition: Auth.hpp:54
std::string _password_salt_mix
Definition: Auth.hpp:45
int max_network_threads()
Definition: Auth.hpp:56
std::vector< char_server > _char_servers
Definition: Auth.hpp:62
int _session_max_timeout
Definition: Auth.hpp:64
int session_max_timeout()
Definition: Auth.hpp:59