// Auth test: Basic parsing, login paths, re-auth gate, route guards. #include #include #include #include #include "Authenticator.h" #include "DenialSink.h" #include "Guard.h" #include "HttpMessageView.h" #include "PasswordHasher.h" using namespace molo::security::auth; using molo::security::guard::HttpRequestView; using molo::security::guard::NullSink; using molo::security::session::Session; static int failures = 0; static void check(bool ok, const char* name) { std::printf("%s %s\n", ok ? "PASS" : "FAIL", name); if (!ok) ++failures; } struct TestUsers : IUserStore { Account acct; bool has = false; bool find(const std::string& username, Account& out) override { if (!has || username != "alice") return false; out = acct; return true; } bool store_rehash(const std::string& username, const std::string& encoded) override { if (username != "alice") return false; acct.argon2id_hash = encoded; return true; } }; int main() { NullSink sink; // Basic parsing. { HttpRequestView r; r.headers.emplace_back("authorization", "Basic YWxpY2U6czNjcml0"); // alice:s3crit auto c = Authenticator::parse_basic(r); check(c.present && c.username == "alice" && c.password == "s3crit", "basic parse"); HttpRequestView r2; check(!Authenticator::parse_basic(r2).present, "basic missing"); HttpRequestView r3; r3.headers.emplace_back("authorization", "Basic !!!"); check(!Authenticator::parse_basic(r3).present, "basic malformed"); HttpRequestView r4; r4.headers.emplace_back("authorization", "Basic bm9jb2xvbg=="); // "nocolon" check(!Authenticator::parse_basic(r4).present, "basic no colon"); } // Login ok / bad / unknown at small test params. auto users = std::make_shared(); { molo::security::crypto::PasswordHasher::Params p; p.memory_kib = 8; p.passes = 1; p.lanes = 1; std::string enc; check(molo::security::crypto::PasswordHasher::hash_password("s3crit", p, enc), "mk hash"); users->acct.user_id = "u-1"; users->acct.argon2id_hash = enc; users->acct.roles = {{"reader", {{{"read"}}}}}; users->has = true; } { Authenticator auth(users, sink); std::uint64_t now = 5000; auth.set_clock([&] { return now; }); HttpRequestView r; r.now_unix_sec = now; r.path = "/login"; BasicCredentials good{"alice", "s3crit", true}; BasicCredentials bad{"alice", "wrong", true}; BasicCredentials unknown{"mallory", "x", true}; Session s; check(auth.login(good, "10.0.0.1", s, r) == Authenticator::LoginResult::Ok && s.user_id == "u-1", "login ok"); // Rehash to OWASP params happened on login; restore small hash for speed. { molo::security::crypto::PasswordHasher::Params p; p.memory_kib = 8; p.passes = 1; p.lanes = 1; std::string enc; molo::security::crypto::PasswordHasher::hash_password("s3crit", p, enc); users->acct.argon2id_hash = enc; } Session s2; check(auth.login(bad, "10.0.0.1", s2, r) == Authenticator::LoginResult::BadCredentials, "login bad password"); Session s3; check(auth.login(unknown, "10.0.0.2", s3, r) == Authenticator::LoginResult::BadCredentials, "login unknown user"); // Rate limit: 10/min budget, 11th attempt trips. Authenticator::LoginResult last = Authenticator::LoginResult::Ok; for (int i = 0; i < 12; ++i) { Session tmp; BasicCredentials fresh{"mallory", "nope", true}; last = auth.login(fresh, "10.7.7.7", tmp, r); } check(last == Authenticator::LoginResult::RateLimited, "login rate limit trips"); } // Locked account. { auto locked = std::make_shared(); locked->has = true; locked->acct.user_id = "u-2"; locked->acct.argon2id_hash = ""; Authenticator auth(locked, sink); HttpRequestView r; Session s; BasicCredentials c{"alice", "anything", true}; check(auth.login(c, "10.0.0.9", s, r) == Authenticator::LoginResult::Locked, "locked"); } // Re-auth gate. { Session s; s.privilege = Session::Privilege::User; s.last_reauth_at = 1000; check(Authenticator::reauth_fresh(s, 1000 + 9 * 60), "reauth fresh"); check(!Authenticator::reauth_fresh(s, 1000 + 11 * 60), "reauth stale"); Authenticator::mark_reauth(s, 2000); check(Authenticator::reauth_fresh(s, 2000), "reauth marked"); Session anon; check(!Authenticator::reauth_fresh(anon, 2000), "reauth anon denied"); } // Route guards: deny by default. { Session anon; HttpRequestView r; auto v = Guard::check_logged(anon, {}, {{{"read"}}}, r, sink); check(!v.allowed && v.http_status == 401, "guard anon 401"); Session user; user.privilege = Session::Privilege::User; std::vector roles = {{"reader", {{{"read"}}}}}; auto v2 = Guard::check(user, roles, {{{"read"}}}); check(v2.allowed, "guard granted"); auto v3 = Guard::check(user, roles, {{{"admin"}}}); check(!v3.allowed && v3.http_status == 403, "guard forbidden 403"); auto v4 = Guard::check(user, {}, {}); check(v4.allowed, "guard no requirements passes authed"); } std::printf(failures == 0 ? "ALL AUTH TESTS PASSED\n" : "FAILURES: %d\n", failures); return failures == 0 ? 0 : 1; }