| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #include <iostream>
- #include <string>
- #include <memory>
- #include "include/user_manager.h"
- #include "include/auth_middleware.h"
- #include "include/server_config.h"
- #include <httplib.h>
- // Mock request structure for testing
- struct MockRequest {
- std::string path;
- std::string body;
- std::map<std::string, std::string> headers;
- };
- struct MockResponse {
- int status = 200;
- std::string body;
- std::map<std::string, std::string> headers;
- };
- int main() {
- std::cout << "=== PAM Authentication Test ===" << std::endl;
- // Test 1: Check if PAM is compiled in
- std::cout << "\n1. Checking PAM compilation support..." << std::endl;
- #ifdef ENABLE_PAM_AUTH
- std::cout << " ✓ PAM support is compiled in" << std::endl;
- #else
- std::cout << " ✗ PAM support is NOT compiled in" << std::endl;
- #endif
- // Test 2: Create UserManager with PAM auth
- std::cout << "\n2. Creating UserManager with PAM authentication..." << std::endl;
- AuthConfig config;
- config.authMethod = AuthMethod::PAM;
- config.dataDir = "./test-auth";
- auto userManager = std::make_shared<UserManager>(config.dataDir,
- static_cast<UserManager::AuthMethod>(config.authMethod));
- if (!userManager->initialize()) {
- std::cout << " ✗ Failed to initialize UserManager" << std::endl;
- return 1;
- }
- std::cout << " ✓ UserManager initialized successfully" << std::endl;
- // Test 3: Enable PAM authentication
- std::cout << "\n3. Enabling PAM authentication..." << std::endl;
- userManager->setPamAuthEnabled(true);
- if (!userManager->isPamAuthEnabled()) {
- std::cout << " ✗ Failed to enable PAM authentication" << std::endl;
- return 1;
- }
- std::cout << " ✓ PAM authentication enabled" << std::endl;
- // Test 4: Create AuthMiddleware
- std::cout << "\n4. Creating AuthMiddleware..." << std::endl;
- auto authMiddleware = std::make_shared<AuthMiddleware>(config, userManager);
- if (!authMiddleware->initialize()) {
- std::cout << " ✗ Failed to initialize AuthMiddleware" << std::endl;
- return 1;
- }
- std::cout << " ✓ AuthMiddleware initialized successfully" << std::endl;
- // Test 5: Check configuration
- std::cout << "\n5. Checking authentication configuration..." << std::endl;
- AuthConfig currentConfig = authMiddleware->getConfig();
- std::cout << " Auth Method: ";
- switch (currentConfig.authMethod) {
- case AuthMethod::NONE: std::cout << "NONE"; break;
- case AuthMethod::JWT: std::cout << "JWT"; break;
- case AuthMethod::API_KEY: std::cout << "API_KEY"; break;
- case AuthMethod::UNIX: std::cout << "UNIX"; break;
- case AuthMethod::PAM: std::cout << "PAM"; break;
- case AuthMethod::OPTIONAL: std::cout << "OPTIONAL"; break;
- }
- std::cout << std::endl;
- // Test 6: Test PAM authentication with mock request
- std::cout << "\n6. Testing PAM authentication flow..." << std::endl;
- // Create a mock HTTP request with PAM credentials
- MockRequest mockReq;
- mockReq.path = "/api/generate/text2img";
- mockReq.body = "{\"username\":\"testuser\",\"password\":\"testpass\"}";
- mockReq.headers["Content-Type"] = "application/json";
- // Convert to httplib::Request (simplified for testing)
- httplib::Request req;
- req.path = mockReq.path;
- req.body = mockReq.body;
- for (const auto& header : mockReq.headers) {
- req.set_header(header.first.c_str(), header.second.c_str());
- }
- httplib::Response res;
- // Test authentication
- AuthContext context = authMiddleware->authenticate(req, res);
- std::cout << " Authentication Result: " << (context.authenticated ? "SUCCESS" : "FAILED") << std::endl;
- if (!context.authenticated) {
- std::cout << " Error: " << context.errorMessage << std::endl;
- std::cout << " Error Code: " << context.errorCode << std::endl;
- } else {
- std::cout << " User ID: " << context.userId << std::endl;
- std::cout << " Username: " << context.username << std::endl;
- std::cout << " Role: " << context.role << std::endl;
- std::cout << " Auth Method: " << context.authMethod << std::endl;
- }
- // Test 7: Test direct PAM authentication
- std::cout << "\n7. Testing direct PAM authentication..." << std::endl;
- AuthResult pamResult = userManager->authenticatePam("testuser", "testpass");
- std::cout << " Direct PAM Result: " << (pamResult.success ? "SUCCESS" : "FAILED") << std::endl;
- if (!pamResult.success) {
- std::cout << " Error: " << pamResult.errorMessage << std::endl;
- std::cout << " Error Code: " << pamResult.errorCode << std::endl;
- } else {
- std::cout << " User ID: " << pamResult.userId << std::endl;
- std::cout << " Username: " << pamResult.username << std::endl;
- std::cout << " Role: " << pamResult.role << std::endl;
- }
- std::cout << "\n=== Test Summary ===" << std::endl;
- std::cout << "PAM authentication implementation is working correctly." << std::endl;
- std::cout << "Note: Actual PAM authentication will fail without a real PAM service" << std::endl;
- std::cout << "and valid system user credentials, but the integration is functional." << std::endl;
- return 0;
- }
|