-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthModule.cpp
More file actions
376 lines (319 loc) · 8.72 KB
/
Copy pathAuthModule.cpp
File metadata and controls
376 lines (319 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
*
* @file AuthModule.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/rixcpp/auth
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Rix
*
*/
#include <rix/auth/AuthModule.hpp>
#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
namespace rixlib::auth
{
namespace
{
[[nodiscard]] AuthError configuration_error(std::string message)
{
return make_auth_error(
AuthErrorCode::ConfigurationError,
std::move(message));
}
[[nodiscard]] AuthError invalid_store_error(std::string message)
{
return make_auth_error(
AuthErrorCode::InvalidState,
std::move(message));
}
} // namespace
AuthError AuthErrorModule::none() const
{
return make_auth_ok();
}
AuthError AuthErrorModule::make(
AuthErrorCode code,
std::string message) const
{
return make_auth_error(code, std::move(message));
}
std::string_view AuthErrorModule::to_string(
AuthErrorCode code) const noexcept
{
return rixlib::auth::to_string(code);
}
std::string_view AuthErrorModule::to_string(
const AuthError &error) const noexcept
{
return rixlib::auth::to_string(error.code());
}
bool AuthErrorModule::ok(const AuthError &error) const noexcept
{
return error.ok();
}
bool AuthErrorModule::failed(const AuthError &error) const noexcept
{
return error.has_error();
}
bool AuthErrorModule::is(
const AuthError &error,
AuthErrorCode code) const noexcept
{
return error.is(code);
}
AuthConfig AuthConfigModule::development() const
{
return AuthConfig::development();
}
AuthConfig AuthConfigModule::production() const
{
return AuthConfig::production();
}
AuthConfig AuthConfigModule::development_with_min_password_length(
std::size_t min_length) const
{
auto config = AuthConfig::development();
config.set_min_password_length(min_length);
return config;
}
AuthConfig AuthConfigModule::production_with_min_password_length(
std::size_t min_length) const
{
auto config = AuthConfig::production();
config.set_min_password_length(min_length);
return config;
}
bool AuthConfigModule::valid(const AuthConfig &config) const noexcept
{
return validate(config).ok();
}
AuthStatus AuthConfigModule::validate(const AuthConfig &config) const
{
if (config.min_password_length() == 0)
{
return AuthStatus::failure(
configuration_error("Minimum password length must be greater than zero."));
}
if (config.max_password_length() == 0)
{
return AuthStatus::failure(
configuration_error("Maximum password length must be greater than zero."));
}
if (config.min_password_length() > config.max_password_length())
{
return AuthStatus::failure(
configuration_error(
"Minimum password length cannot be greater than maximum password length."));
}
if (config.session_ttl_seconds() <= 0)
{
return AuthStatus::failure(
configuration_error("Session lifetime must be greater than zero."));
}
if (config.token_ttl_seconds() <= 0)
{
return AuthStatus::failure(
configuration_error("Token lifetime must be greater than zero."));
}
if (config.password_hash_iterations() == 0)
{
return AuthStatus::failure(
configuration_error("Password hash iterations must be greater than zero."));
}
if (config.password_salt_size() == 0)
{
return AuthStatus::failure(
configuration_error("Password salt size must be greater than zero."));
}
if (config.password_hash_size() == 0)
{
return AuthStatus::failure(
configuration_error("Password hash size must be greater than zero."));
}
if (config.issuer().empty())
{
return AuthStatus::failure(
configuration_error("Token issuer cannot be empty."));
}
return AuthStatus::success();
}
AuthResult<std::string> AuthPasswordModule::hash(
std::string_view password) const
{
PasswordHasher hasher;
return hasher.hash(password);
}
AuthResult<std::string> AuthPasswordModule::hash(
std::string_view password,
const AuthConfig &config) const
{
PasswordHasher hasher(config);
return hasher.hash(password);
}
bool AuthPasswordModule::verify(
std::string_view password,
std::string_view password_hash) const
{
PasswordHasher hasher;
return hasher.verify(password, password_hash);
}
bool AuthPasswordModule::verify(
std::string_view password,
std::string_view password_hash,
const AuthConfig &config) const
{
PasswordHasher hasher(config);
return hasher.verify(password, password_hash);
}
bool AuthPasswordModule::accepts(std::string_view password) const
{
PasswordHasher hasher;
return hasher.accepts(password);
}
bool AuthPasswordModule::accepts(
std::string_view password,
const AuthConfig &config) const
{
PasswordHasher hasher(config);
return hasher.accepts(password);
}
PasswordHasher AuthPasswordModule::hasher() const
{
return PasswordHasher{};
}
PasswordHasher AuthPasswordModule::hasher(const AuthConfig &config) const
{
return PasswordHasher{config};
}
std::unique_ptr<UserStore> AuthStoreModule::memory_users() const
{
return std::make_unique<MemoryUserStore>();
}
std::unique_ptr<SessionStore> AuthStoreModule::memory_sessions() const
{
return std::make_unique<MemorySessionStore>();
}
std::unique_ptr<UserStore> AuthStoreModule::database_users(
vix::db::Database &database) const
{
return std::make_unique<DbUserStore>(database);
}
std::unique_ptr<SessionStore> AuthStoreModule::database_sessions(
vix::db::Database &database) const
{
return std::make_unique<DbSessionStore>(database);
}
Auth AuthModule::create(
UserStore &users,
SessionStore &sessions) const
{
return Auth{
users,
sessions,
AuthConfig::development()};
}
Auth AuthModule::create(
UserStore &users,
SessionStore &sessions,
AuthConfig config) const
{
return Auth{
users,
sessions,
std::move(config)};
}
AuthResult<ManagedAuth> AuthModule::managed(
std::unique_ptr<UserStore> users,
std::unique_ptr<SessionStore> sessions) const
{
return managed(
std::move(users),
std::move(sessions),
AuthConfig::development());
}
AuthResult<ManagedAuth> AuthModule::managed(
std::unique_ptr<UserStore> users,
std::unique_ptr<SessionStore> sessions,
AuthConfig config) const
{
if (!users)
{
return AuthResult<ManagedAuth>::failure(
invalid_store_error("User store cannot be null."));
}
if (!sessions)
{
return AuthResult<ManagedAuth>::failure(
invalid_store_error("Session store cannot be null."));
}
const auto validation = this->config.validate(config);
if (validation.failed())
{
return AuthResult<ManagedAuth>::failure(validation.error());
}
return AuthResult<ManagedAuth>::success(
ManagedAuth{
std::move(users),
std::move(sessions),
std::move(config)});
}
ManagedAuth AuthModule::memory() const
{
return memory(AuthConfig::development());
}
ManagedAuth AuthModule::memory(AuthConfig config) const
{
auto users = std::make_unique<MemoryUserStore>();
auto sessions = std::make_unique<MemorySessionStore>();
return ManagedAuth{
std::move(users),
std::move(sessions),
std::move(config)};
}
ManagedAuth AuthModule::database(vix::db::Database &database) const
{
return this->database(
database,
AuthConfig::production());
}
ManagedAuth AuthModule::database(
vix::db::Database &database,
AuthConfig config) const
{
auto users = std::make_unique<DbUserStore>(database);
auto sessions = std::make_unique<DbSessionStore>(database);
return ManagedAuth{
std::move(users),
std::move(sessions),
std::move(config)};
}
std::string AuthModule::version() const
{
return rixlib::auth::version();
}
int AuthModule::version_major() const noexcept
{
return rixlib::auth::version_major();
}
int AuthModule::version_minor() const noexcept
{
return rixlib::auth::version_minor();
}
int AuthModule::version_patch() const noexcept
{
return rixlib::auth::version_patch();
}
int AuthModule::version_number() const noexcept
{
return rixlib::auth::version_number();
}
} // namespace rixlib::auth