-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthModule.hpp
More file actions
473 lines (426 loc) · 13.1 KB
/
Copy pathAuthModule.hpp
File metadata and controls
473 lines (426 loc) · 13.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
*
* @file AuthModule.hpp
* @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
*
*/
#ifndef RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED
#define RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED
#include <rix/auth/Auth.hpp>
#include <rix/auth/AuthConfig.hpp>
#include <rix/auth/AuthError.hpp>
#include <rix/auth/AuthResult.hpp>
#include <rix/auth/ManagedAuth.hpp>
#include <rix/auth/PasswordHasher.hpp>
#include <rix/auth/SessionStore.hpp>
#include <rix/auth/UserStore.hpp>
#include <rix/auth/Version.hpp>
#include <rix/auth/stores/DbSessionStore.hpp>
#include <rix/auth/stores/DbUserStore.hpp>
#include <rix/auth/stores/MemorySessionStore.hpp>
#include <rix/auth/stores/MemoryUserStore.hpp>
#include <memory>
#include <string>
#include <string_view>
#include <vix/db/Database.hpp>
namespace rixlib::auth
{
/**
* @brief Auth error helpers exposed through rix.auth.error.
*
* This module keeps public examples away from low-level namespace helpers.
*/
class AuthErrorModule
{
public:
/**
* @brief Return a success auth error value.
*
* @return AuthError with AuthErrorCode::None.
*/
[[nodiscard]] AuthError none() const;
/**
* @brief Create an auth error value.
*
* @param code Stable auth error code.
* @param message Human-readable error message.
* @return AuthError value.
*/
[[nodiscard]] AuthError make(
AuthErrorCode code,
std::string message) const;
/**
* @brief Convert an auth error code to a stable string.
*
* @param code Auth error code.
* @return Stable string representation.
*/
[[nodiscard]] std::string_view to_string(
AuthErrorCode code) const noexcept;
/**
* @brief Convert an auth error value to a stable string.
*
* @param error Auth error value.
* @return Stable string representation.
*/
[[nodiscard]] std::string_view to_string(
const AuthError &error) const noexcept;
/**
* @brief Return true when the error is success.
*
* @param error Auth error value.
* @return true if the error code is None.
*/
[[nodiscard]] bool ok(const AuthError &error) const noexcept;
/**
* @brief Return true when the error is failure.
*
* @param error Auth error value.
* @return true if the error code is not None.
*/
[[nodiscard]] bool failed(const AuthError &error) const noexcept;
/**
* @brief Return true when the error has the given code.
*
* @param error Auth error value.
* @param code Expected auth error code.
* @return true if the code matches.
*/
[[nodiscard]] bool is(
const AuthError &error,
AuthErrorCode code) const noexcept;
};
/**
* @brief Auth configuration facade exposed through rix.auth.config.
*/
class AuthConfigModule
{
public:
/**
* @brief Return the default development auth configuration.
*
* @return Development AuthConfig.
*/
[[nodiscard]] AuthConfig development() const;
/**
* @brief Return the default production auth configuration.
*
* @return Production AuthConfig.
*/
[[nodiscard]] AuthConfig production() const;
/**
* @brief Return a development config with a custom minimum password length.
*
* @param min_length Minimum accepted password length.
* @return Development AuthConfig.
*/
[[nodiscard]] AuthConfig development_with_min_password_length(
std::size_t min_length) const;
/**
* @brief Return a production config with a custom minimum password length.
*
* @param min_length Minimum accepted password length.
* @return Production AuthConfig.
*/
[[nodiscard]] AuthConfig production_with_min_password_length(
std::size_t min_length) const;
/**
* @brief Return true when the config looks usable.
*
* This is intentionally conservative. It catches obvious configuration
* mistakes before the user creates an auth service.
*
* @param config Auth configuration.
* @return true if the configuration is accepted.
*/
[[nodiscard]] bool valid(const AuthConfig &config) const noexcept;
/**
* @brief Validate an auth configuration.
*
* @param config Auth configuration.
* @return AuthStatus indicating success or the first validation error.
*/
[[nodiscard]] AuthStatus validate(const AuthConfig &config) const;
};
/**
* @brief Password helper facade exposed through rix.auth.password.
*/
class AuthPasswordModule
{
public:
/**
* @brief Hash a plain-text password using the default hasher.
*
* @param password Plain-text password.
* @return Encoded password hash on success.
*/
[[nodiscard]] AuthResult<std::string>
hash(std::string_view password) const;
/**
* @brief Hash a plain-text password using the given auth config.
*
* @param password Plain-text password.
* @param config Auth configuration.
* @return Encoded password hash on success.
*/
[[nodiscard]] AuthResult<std::string>
hash(
std::string_view password,
const AuthConfig &config) const;
/**
* @brief Verify a password against an encoded password hash.
*
* @param password Plain-text password.
* @param password_hash Encoded password hash.
* @return true when the password matches.
*/
[[nodiscard]] bool verify(
std::string_view password,
std::string_view password_hash) const;
/**
* @brief Verify a password against an encoded password hash using config.
*
* @param password Plain-text password.
* @param password_hash Encoded password hash.
* @param config Auth configuration.
* @return true when the password matches.
*/
[[nodiscard]] bool verify(
std::string_view password,
std::string_view password_hash,
const AuthConfig &config) const;
/**
* @brief Return true when the password satisfies default policy.
*
* @param password Plain-text password.
* @return true if accepted.
*/
[[nodiscard]] bool accepts(std::string_view password) const;
/**
* @brief Return true when the password satisfies the given config.
*
* @param password Plain-text password.
* @param config Auth configuration.
* @return true if accepted.
*/
[[nodiscard]] bool accepts(
std::string_view password,
const AuthConfig &config) const;
/**
* @brief Return a new password hasher with default settings.
*
* @return PasswordHasher instance.
*/
[[nodiscard]] PasswordHasher hasher() const;
/**
* @brief Return a new password hasher with the given config.
*
* @param config Auth configuration.
* @return PasswordHasher instance.
*/
[[nodiscard]] PasswordHasher hasher(const AuthConfig &config) const;
};
/**
* @brief Store factory helpers exposed through rix.auth.stores.
*
* This allows advanced users to create stores without manually depending on
* concrete store class names in public examples.
*/
class AuthStoreModule
{
public:
/**
* @brief Create a memory user store.
*
* @return Memory user store.
*/
[[nodiscard]] std::unique_ptr<UserStore> memory_users() const;
/**
* @brief Create a memory session store.
*
* @return Memory session store.
*/
[[nodiscard]] std::unique_ptr<SessionStore> memory_sessions() const;
/**
* @brief Create a database user store.
*
* @param database Vix database facade.
* @return Database user store.
*/
[[nodiscard]] std::unique_ptr<UserStore>
database_users(vix::db::Database &database) const;
/**
* @brief Create a database session store.
*
* @param database Vix database facade.
* @return Database session store.
*/
[[nodiscard]] std::unique_ptr<SessionStore>
database_sessions(vix::db::Database &database) const;
};
/**
* @brief High-level authentication facade exposed through rix.auth.
*
* AuthModule is the public entry point used by the global Rix facade.
*
* Public examples should prefer:
*
* @code
* auto auth = rix.auth.memory();
* auto auth = rix.auth.database(db);
* @endcode
*
* Lower-level caller-owned store APIs are still available for advanced
* integration, but managed APIs are the safer default.
*/
class AuthModule
{
public:
/**
* @brief Error helpers.
*/
AuthErrorModule error{};
/**
* @brief Configuration helpers.
*/
AuthConfigModule config{};
/**
* @brief Password hashing helpers.
*/
AuthPasswordModule password{};
/**
* @brief Store factory helpers.
*/
AuthStoreModule stores{};
/**
* @brief Create an Auth service with caller-owned stores.
*
* This is an advanced API. The caller must guarantee that both stores
* outlive the returned Auth service.
*
* Prefer memory(), database(), or managed() for public application code.
*
* @param users User storage backend.
* @param sessions Session storage backend.
* @return Auth service using development defaults.
*/
[[nodiscard]] Auth create(
UserStore &users,
SessionStore &sessions) const;
/**
* @brief Create an Auth service with caller-owned stores and config.
*
* This is an advanced API. The caller must guarantee that both stores
* outlive the returned Auth service.
*
* Prefer memory(), database(), or managed() for public application code.
*
* @param users User storage backend.
* @param sessions Session storage backend.
* @param config Authentication configuration.
* @return Auth service.
*/
[[nodiscard]] Auth create(
UserStore &users,
SessionStore &sessions,
AuthConfig config) const;
/**
* @brief Create a managed auth service from owned stores.
*
* This is the safe custom-store API. The returned ManagedAuth owns the
* stores and prevents dangling references.
*
* @param users Owned user storage backend.
* @param sessions Owned session storage backend.
* @return Managed auth service or configuration/store error.
*/
[[nodiscard]] AuthResult<ManagedAuth> managed(
std::unique_ptr<UserStore> users,
std::unique_ptr<SessionStore> sessions) const;
/**
* @brief Create a managed auth service from owned stores and config.
*
* This is the safe custom-store API. The returned ManagedAuth owns the
* stores and prevents dangling references.
*
* @param users Owned user storage backend.
* @param sessions Owned session storage backend.
* @param config Authentication configuration.
* @return Managed auth service or configuration/store error.
*/
[[nodiscard]] AuthResult<ManagedAuth> managed(
std::unique_ptr<UserStore> users,
std::unique_ptr<SessionStore> sessions,
AuthConfig config) const;
/**
* @brief Create a memory-backed managed Auth service.
*
* @return Managed auth service using development defaults.
*/
[[nodiscard]] ManagedAuth memory() const;
/**
* @brief Create a memory-backed managed Auth service with config.
*
* @param config Authentication configuration.
* @return Managed auth service.
*/
[[nodiscard]] ManagedAuth memory(AuthConfig config) const;
/**
* @brief Create a database-backed managed Auth service.
*
* @param database Vix database facade.
* @return Managed auth service using production defaults.
*/
[[nodiscard]] ManagedAuth database(vix::db::Database &database) const;
/**
* @brief Create a database-backed managed Auth service with config.
*
* @param database Vix database facade.
* @param config Authentication configuration.
* @return Managed auth service.
*/
[[nodiscard]] ManagedAuth database(
vix::db::Database &database,
AuthConfig config) const;
/**
* @brief Return the package version string.
*
* @return Version string.
*/
[[nodiscard]] std::string version() const;
/**
* @brief Return the package major version.
*
* @return Major version.
*/
[[nodiscard]] int version_major() const noexcept;
/**
* @brief Return the package minor version.
*
* @return Minor version.
*/
[[nodiscard]] int version_minor() const noexcept;
/**
* @brief Return the package patch version.
*
* @return Patch version.
*/
[[nodiscard]] int version_patch() const noexcept;
/**
* @brief Return the encoded package version number.
*
* @return Encoded version.
*/
[[nodiscard]] int version_number() const noexcept;
};
} // namespace rixlib::auth
#endif // RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED