Ports the fewo-webapp AuthInterceptor + requireAdmin onto three abstract
interfaces so consumer apps plug in their own user store, public paths,
and runtime config without forking:
auth/AuthPrincipal.hpp library-owned {id, username, role} value
auth/IAuthBackend.hpp resolveBy{Session,ApiKey,Cert}, hasActiveUsers,
deleteExpiredSessions
auth/IAuthPolicy.hpp isPublicPath, adminRoles, readonlyRoles,
setupModeActive (defaults: admin/readonly,
no public paths, setup off)
auth/IRuntimeConfig.hpp bindAddress, isLoopback
auth/AuthInterceptor.hpp intercept() running the same 6-step ladder as
fewo's original (public → setup → cert DN →
session/API key → CSRF → readonly)
auth/RequireRole.hpp requireUser + requireAdmin helpers reading
bundle data (config-driven role sets, not
hard-coded 'admin')
TokenHasher is passed in so the library doesn't prescribe SHA-256 vs.
whatever. Bundle keys match fewo's existing controllers so the consumer
migration in #418 is a straightforward adapter swap.
Smoke-compiled against oatpp 1.3.0 headers.
Closes fewo-webapp#413
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
50 lines
1.6 KiB
C++
50 lines
1.6 KiB
C++
#ifndef OATPP_AUTHKIT_AUTH_IAUTH_BACKEND_HPP
|
|
#define OATPP_AUTHKIT_AUTH_IAUTH_BACKEND_HPP
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include "AuthPrincipal.hpp"
|
|
|
|
namespace oatpp_authkit {
|
|
|
|
/**
|
|
* @brief Consumer-supplied adapter from library primitives → user store.
|
|
*
|
|
* The library never reads the database directly. The interceptor calls
|
|
* these methods, the concrete implementation (owned by the consumer app)
|
|
* wraps `UserDb` / `CertificateDb` / whatever and returns library-owned
|
|
* `AuthPrincipal` structs.
|
|
*
|
|
* All methods must be thread-safe (the interceptor is invoked from oatpp
|
|
* worker threads).
|
|
*/
|
|
class IAuthBackend {
|
|
public:
|
|
virtual ~IAuthBackend() = default;
|
|
|
|
/** @brief Look up an active session by its hashed token. */
|
|
virtual std::optional<AuthPrincipal> resolveBySessionHash(const std::string& hash) = 0;
|
|
|
|
/** @brief Look up an API key by its hashed token; also touch `last_used_at`. */
|
|
virtual std::optional<AuthPrincipal> resolveByApiKeyHash(const std::string& hash) = 0;
|
|
|
|
/**
|
|
* @brief Look up a user by TLS client cert DN. Return nullopt if your
|
|
* app doesn't support cert auth — the interceptor silently skips
|
|
* this step.
|
|
*/
|
|
virtual std::optional<AuthPrincipal> resolveByCertDn(const std::string& /*dn*/) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
/** @brief True iff at least one active user exists. Used for setup-mode gate. */
|
|
virtual bool hasActiveUsers() = 0;
|
|
|
|
/** @brief Delete expired session rows. Called periodically by the interceptor. */
|
|
virtual void deleteExpiredSessions() = 0;
|
|
};
|
|
|
|
} // namespace oatpp_authkit
|
|
|
|
#endif
|