This document summarizes the implementation of Issue #30: "When unix auth is turned on, use PAM as authentication method". The implementation successfully delegates Unix authentication to PAM when available, while maintaining backward compatibility.
src/user_manager.cpp, include/user_manager.h)authenticateUnix() to accept an optional password parameterAdded isPamAuthEnabled() method for checking PAM status
AuthResult UserManager::authenticateUnix(const std::string& username, const std::string& password) {
// If PAM is enabled, delegate to PAM authentication
if (m_pamAuthEnabled) {
return authenticatePam(username, password);
}
// Traditional Unix auth without PAM - fallback
// ... existing logic
}
src/auth_middleware.cpp, include/auth_middleware.h)Enhanced error handling for missing passwords when PAM is required
// Try to extract from JSON body (for login API)
if (contentType.find("application/json") != std::string::npos) {
try {
json body = json::parse(req.body);
username = body.value("username", "");
password = body.value("password", "");
} catch (const json::exception& e) {
// Invalid JSON, continue with other methods
}
}
src/server.cpp)Enhanced error responses with specific error codes
// Check if PAM is enabled - if so, password is required
if (m_userManager->isPamAuthEnabled() && password.empty()) {
sendErrorResponse(res, "Password is required for Unix authentication", 400, "MISSING_PASSWORD", requestId);
return;
}
webui/components/auth/login-form.tsx)Updated form validation to handle password requirements
<Label htmlFor="password">
Password
{authMethod === 'unix' && (
<span className="text-sm text-muted-foreground ml-2">
(Required if PAM is enabled)
</span>
)}
</Label>
webui/lib/api.ts)Maintained compatibility with all authentication methods
// For both Unix and JWT auth, send username and password
const response = await apiRequest('/auth/login', {
method: 'POST',
body: JSON.stringify({ username, password }),
});
test_unix_pam_integration.cpp for unit testingtest_unix_auth_integration.sh for integration testingPAM_AUTHENTICATION.md with Unix+PAM integration detailsClient Request → AuthMiddleware → UserManager.authenticateUnix() → PamAuth → Unix Token
Client Request → AuthMiddleware → UserManager.authenticateUnix() → Unix Token
MISSING_PASSWORD: When PAM enabled but no password providedAUTHENTICATION_FAILED: When PAM authentication failsPAM_NOT_AVAILABLE: When PAM required but not compiled in./stable-diffusion-rest-server \
--auth-method unix \
--enable-pam-auth \
--pam-service-name stable-diffusion-rest \
--port 8080
./stable-diffusion-rest-server \
--auth-method unix \
--port 8080
# Build and run unit tests
cd build
cmake -DBUILD_MODEL_DETECTOR_TEST=ON ..
cmake --build . --target test_model_detector
./test/test_model_detector
# Run integration test script
./test_unix_auth_integration.sh
The Issue #30 implementation successfully integrates PAM as the authentication backend for Unix authentication while maintaining full backward compatibility. The solution provides enhanced security for Unix authentication when PAM is available, while preserving the existing behavior when PAM is not available or disabled.
The implementation follows the project's coding standards, includes comprehensive error handling, and provides thorough documentation for both users and developers.