Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f7a2d2d61 | ||
|
|
fb80cb78eb | ||
|
|
c06e1bd64b |
@@ -22,6 +22,12 @@ CREATE TABLE IF NOT EXISTS `dummy` (
|
|||||||
`id` int(11) DEFAULT NULL
|
`id` int(11) DEFAULT NULL
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- Create a dedicated DB user for the web application (least privilege).
|
||||||
|
-- Grant only the required privileges on the application database.
|
||||||
|
CREATE USER IF NOT EXISTS 'passman_app'@'%' IDENTIFIED BY 'passman_app_pw';
|
||||||
|
GRANT SELECT, INSERT, UPDATE, DELETE ON pwd_mgr.* TO 'passman_app'@'%';
|
||||||
|
FLUSH PRIVILEGES;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `login_users` (
|
CREATE TABLE IF NOT EXISTS `login_users` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`username` varchar(20) NOT NULL,
|
`username` varchar(20) NOT NULL,
|
||||||
@@ -31,7 +37,7 @@ CREATE TABLE IF NOT EXISTS `login_users` (
|
|||||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
INSERT INTO `login_users` (`id`, `username`, `password`) VALUES
|
INSERT INTO `login_users` (`id`, `username`, `password`) VALUES
|
||||||
(1, 'u1', 'p1');
|
(1, 'u1', '$2y$10$L18u5/PyVkDgsce/DsUOQu0sKhTzh854Euhog3cVb1W4YAfgRzY8W'); -- php -r 'echo password_hash("p1", PASSWORD_DEFAULT), PHP_EOL;'
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `notes` (
|
CREATE TABLE IF NOT EXISTS `notes` (
|
||||||
`notesid` int(11) NOT NULL AUTO_INCREMENT,
|
`notesid` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ services:
|
|||||||
- ./php:/var/www/html
|
- ./php:/var/www/html
|
||||||
environment:
|
environment:
|
||||||
DB_HOST: db
|
DB_HOST: db
|
||||||
DB_USER: root
|
|
||||||
DB_PASS: rootpass
|
|
||||||
DB_NAME: pwd_mgr
|
DB_NAME: pwd_mgr
|
||||||
|
DB_USER: passman_app
|
||||||
|
DB_PASS: passman_app_pw
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
// NOTE: In Docker, the DB host is the service name (e.g., "db"), not "localhost".
|
// NOTE: In Docker, the DB host is the service name (e.g., "db"), not "localhost".
|
||||||
|
|
||||||
$DB_HOST = getenv('DB_HOST') ?: 'db';
|
$DB_HOST = getenv('DB_HOST') ?: 'db';
|
||||||
$DB_USER = getenv('DB_USER') ?: 'root';
|
$DB_USER = getenv('DB_USER') ?: 'passman_app';
|
||||||
$DB_PASS = getenv('DB_PASS') ?: 'rootpass';
|
$DB_PASS = getenv('DB_PASS') ?: 'passman_app_pw';
|
||||||
$DB_NAME = getenv('DB_NAME') ?: 'pwd_mgr';
|
$DB_NAME = getenv('DB_NAME') ?: 'pwd_mgr';
|
||||||
|
|
||||||
// Create a DB connection.
|
// Create a DB connection.
|
||||||
|
|||||||
@@ -26,22 +26,30 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
// }
|
// }
|
||||||
require_once __DIR__ . "/config.php";
|
require_once __DIR__ . "/config.php";
|
||||||
|
|
||||||
|
// Authentication with hashed passwords:
|
||||||
|
// 1) Fetch the stored hash by username
|
||||||
// SQL injection mitigation: use a prepared statement with bound parameters.
|
// SQL injection mitigation: use a prepared statement with bound parameters.
|
||||||
// User input is treated strictly as data, not as part of the SQL syntax.
|
// User input is treated strictly as data, not as part of the SQL syntax.
|
||||||
$stmt = $conn->prepare("SELECT id FROM login_users WHERE username = ? AND password = ?");
|
// 2) Verify the submitted password with password_verify()
|
||||||
|
$stmt = $conn->prepare("SELECT id, password FROM login_users WHERE username = ?");
|
||||||
if ($stmt === false) {
|
if ($stmt === false) {
|
||||||
// Fail closed (do not leak details in production).
|
// Fail closed (do not leak details in production).
|
||||||
die("Prepare failed.");
|
die("Prepare failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->bind_param("ss", $username, $password);
|
$stmt->bind_param("s", $username);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->store_result(); // Needed to use $stmt->num_rows
|
|
||||||
|
$result = $stmt->get_result(); // Requires mysqlnd (usually enabled)
|
||||||
unset($_POST['username']);
|
unset($_POST['username']);
|
||||||
unset($_POST['password']);
|
unset($_POST['password']);
|
||||||
|
|
||||||
if ($stmt->num_rows >= 1) {
|
if ($result && $result->num_rows === 1) {
|
||||||
|
$row = $result->fetch_assoc();
|
||||||
|
$stored_hash = $row["password"];
|
||||||
|
|
||||||
|
// Verify password against the stored hash.
|
||||||
|
if (password_verify($password, $stored_hash)) {
|
||||||
// Regenerate session ID to prevent session fixation!
|
// Regenerate session ID to prevent session fixation!
|
||||||
//session_regenerate_id(true);
|
//session_regenerate_id(true);
|
||||||
|
|
||||||
@@ -49,21 +57,18 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
$_SESSION['username'] = $username;
|
$_SESSION['username'] = $username;
|
||||||
$_SESSION['loggedin'] = true;
|
$_SESSION['loggedin'] = true;
|
||||||
|
|
||||||
//while ($row = $result -> fetch_assoc()) {
|
|
||||||
// print_r($row);
|
|
||||||
// $_SESSION['user_id'] = $row['id'];
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Close
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
$conn->close();
|
$conn->close();
|
||||||
|
|
||||||
// Redirect to a dashboard page
|
|
||||||
header("Location: dashboard.php");
|
header("Location: dashboard.php");
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$login_message = "Invalid username or password";
|
$login_message = "Invalid username or password";
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$login_message = "Invalid username or password";
|
||||||
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
$conn->close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,17 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
$login_message = "Database error (prepare failed).";
|
$login_message = "Database error (prepare failed).";
|
||||||
$result = false;
|
$result = false;
|
||||||
} else {
|
} else {
|
||||||
$stmt->bind_param("ss", $new_username, $new_password);
|
// Hash the password before storing it.
|
||||||
|
// Never store login passwords in plaintext.
|
||||||
|
$password_hash = password_hash($new_password, PASSWORD_DEFAULT);
|
||||||
|
if ($password_hash === false) {
|
||||||
|
$login_message = "Password hashing failed.";
|
||||||
|
$result = false;
|
||||||
|
} else {
|
||||||
|
// Store the hash (not the plaintext password).
|
||||||
|
$stmt->bind_param("ss", $new_username, $password_hash);
|
||||||
$result = $stmt->execute();
|
$result = $stmt->execute();
|
||||||
|
}
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user