|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file AuthModule.hpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. |
| 7 | + * All rights reserved. |
| 8 | + * https://github.com/rixcpp/auth |
| 9 | + * |
| 10 | + * Use of this source code is governed by a MIT license |
| 11 | + * that can be found in the License file. |
| 12 | + * |
| 13 | + * Rix |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED |
| 18 | +#define RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED |
| 19 | + |
| 20 | +#include <rix/auth/Auth.hpp> |
| 21 | +#include <rix/auth/AuthConfig.hpp> |
| 22 | +#include <rix/auth/ManagedAuth.hpp> |
| 23 | +#include <rix/auth/PasswordHasher.hpp> |
| 24 | +#include <rix/auth/SessionStore.hpp> |
| 25 | +#include <rix/auth/UserStore.hpp> |
| 26 | +#include <rix/auth/Version.hpp> |
| 27 | +#include <rix/auth/stores/DbSessionStore.hpp> |
| 28 | +#include <rix/auth/stores/DbUserStore.hpp> |
| 29 | +#include <rix/auth/stores/MemorySessionStore.hpp> |
| 30 | +#include <rix/auth/stores/MemoryUserStore.hpp> |
| 31 | + |
| 32 | +#include <string> |
| 33 | + |
| 34 | +#include <vix/db/Database.hpp> |
| 35 | + |
| 36 | +namespace rixlib::auth |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @brief Auth configuration facade exposed through rix.auth.config. |
| 40 | + */ |
| 41 | + class AuthConfigModule |
| 42 | + { |
| 43 | + public: |
| 44 | + /** |
| 45 | + * @brief Return the default development auth configuration. |
| 46 | + * |
| 47 | + * @return Development AuthConfig. |
| 48 | + */ |
| 49 | + [[nodiscard]] AuthConfig development() const; |
| 50 | + |
| 51 | + /** |
| 52 | + * @brief Return the default production auth configuration. |
| 53 | + * |
| 54 | + * @return Production AuthConfig. |
| 55 | + */ |
| 56 | + [[nodiscard]] AuthConfig production() const; |
| 57 | + }; |
| 58 | + |
| 59 | + /** |
| 60 | + * @brief Password helper facade exposed through rix.auth.password. |
| 61 | + */ |
| 62 | + class AuthPasswordModule |
| 63 | + { |
| 64 | + public: |
| 65 | + /** |
| 66 | + * @brief Hash a plain-text password using the default hasher. |
| 67 | + * |
| 68 | + * @param password Plain-text password. |
| 69 | + * @return Encoded password hash on success. |
| 70 | + */ |
| 71 | + [[nodiscard]] AuthResult<std::string> |
| 72 | + hash(std::string_view password) const; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Verify a password against an encoded password hash. |
| 76 | + * |
| 77 | + * @param password Plain-text password. |
| 78 | + * @param password_hash Encoded password hash. |
| 79 | + * @return true when the password matches. |
| 80 | + */ |
| 81 | + [[nodiscard]] bool verify( |
| 82 | + std::string_view password, |
| 83 | + std::string_view password_hash) const; |
| 84 | + |
| 85 | + /** |
| 86 | + * @brief Return a new password hasher with default settings. |
| 87 | + * |
| 88 | + * @return PasswordHasher instance. |
| 89 | + */ |
| 90 | + [[nodiscard]] PasswordHasher hasher() const; |
| 91 | + }; |
| 92 | + |
| 93 | + /** |
| 94 | + * @brief High-level authentication facade exposed through rix.auth. |
| 95 | + * |
| 96 | + * AuthModule is the simple public entry point used by the global Rix facade. |
| 97 | + * It can create memory-backed auth services for examples and tests, |
| 98 | + * database-backed auth services for durable applications, or Auth services |
| 99 | + * from custom stores. |
| 100 | + */ |
| 101 | + class AuthModule |
| 102 | + { |
| 103 | + public: |
| 104 | + /** |
| 105 | + * @brief Configuration helpers. |
| 106 | + */ |
| 107 | + AuthConfigModule config{}; |
| 108 | + |
| 109 | + /** |
| 110 | + * @brief Password hashing helpers. |
| 111 | + */ |
| 112 | + AuthPasswordModule password{}; |
| 113 | + |
| 114 | + /** |
| 115 | + * @brief Create an Auth service with caller-owned stores. |
| 116 | + * |
| 117 | + * @param users User storage backend. |
| 118 | + * @param sessions Session storage backend. |
| 119 | + * @return Auth service using development defaults. |
| 120 | + */ |
| 121 | + [[nodiscard]] Auth create( |
| 122 | + UserStore &users, |
| 123 | + SessionStore &sessions) const; |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Create an Auth service with caller-owned stores and config. |
| 127 | + * |
| 128 | + * @param users User storage backend. |
| 129 | + * @param sessions Session storage backend. |
| 130 | + * @param config Authentication configuration. |
| 131 | + * @return Auth service. |
| 132 | + */ |
| 133 | + [[nodiscard]] Auth create( |
| 134 | + UserStore &users, |
| 135 | + SessionStore &sessions, |
| 136 | + AuthConfig config) const; |
| 137 | + |
| 138 | + /** |
| 139 | + * @brief Create a memory-backed managed Auth service. |
| 140 | + * |
| 141 | + * @return Managed auth service using development defaults. |
| 142 | + */ |
| 143 | + [[nodiscard]] ManagedAuth memory() const; |
| 144 | + |
| 145 | + /** |
| 146 | + * @brief Create a memory-backed managed Auth service with config. |
| 147 | + * |
| 148 | + * @param config Authentication configuration. |
| 149 | + * @return Managed auth service. |
| 150 | + */ |
| 151 | + [[nodiscard]] ManagedAuth memory(AuthConfig config) const; |
| 152 | + |
| 153 | + /** |
| 154 | + * @brief Create a database-backed managed Auth service. |
| 155 | + * |
| 156 | + * @param database Vix database facade. |
| 157 | + * @return Managed auth service using production defaults. |
| 158 | + */ |
| 159 | + [[nodiscard]] ManagedAuth database(vix::db::Database &database) const; |
| 160 | + |
| 161 | + /** |
| 162 | + * @brief Create a database-backed managed Auth service with config. |
| 163 | + * |
| 164 | + * @param database Vix database facade. |
| 165 | + * @param config Authentication configuration. |
| 166 | + * @return Managed auth service. |
| 167 | + */ |
| 168 | + [[nodiscard]] ManagedAuth database( |
| 169 | + vix::db::Database &database, |
| 170 | + AuthConfig config) const; |
| 171 | + |
| 172 | + /** |
| 173 | + * @brief Return the package version string. |
| 174 | + * |
| 175 | + * @return Version string. |
| 176 | + */ |
| 177 | + [[nodiscard]] std::string version() const; |
| 178 | + |
| 179 | + /** |
| 180 | + * @brief Return the package major version. |
| 181 | + * |
| 182 | + * @return Major version. |
| 183 | + */ |
| 184 | + [[nodiscard]] int version_major() const noexcept; |
| 185 | + |
| 186 | + /** |
| 187 | + * @brief Return the package minor version. |
| 188 | + * |
| 189 | + * @return Minor version. |
| 190 | + */ |
| 191 | + [[nodiscard]] int version_minor() const noexcept; |
| 192 | + |
| 193 | + /** |
| 194 | + * @brief Return the package patch version. |
| 195 | + * |
| 196 | + * @return Patch version. |
| 197 | + */ |
| 198 | + [[nodiscard]] int version_patch() const noexcept; |
| 199 | + |
| 200 | + /** |
| 201 | + * @brief Return the encoded package version number. |
| 202 | + * |
| 203 | + * @return Encoded version. |
| 204 | + */ |
| 205 | + [[nodiscard]] int version_number() const noexcept; |
| 206 | + }; |
| 207 | +} // namespace rixlib::auth |
| 208 | + |
| 209 | +#endif // RIXCPP_AUTH_INCLUDE_RIX_AUTH_AUTHMODULE_HPP_INCLUDED |
0 commit comments