Adding passwords and versionning
This commit is contained in:
57
includes/auth.php
Normal file
57
includes/auth.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../config.php';
|
||||
|
||||
define('AUTH_PASSWD_FILE', __DIR__ . '/../auth.passwd');
|
||||
|
||||
/**
|
||||
* Returns the active password hash.
|
||||
* auth.passwd (written by the web interface) takes precedence over
|
||||
* the UI_PASS_HASH constant in config.php.
|
||||
*/
|
||||
function active_pass_hash(): string {
|
||||
if (is_readable(AUTH_PASSWD_FILE)) {
|
||||
return trim(file_get_contents(AUTH_PASSWD_FILE));
|
||||
}
|
||||
return UI_PASS_HASH;
|
||||
}
|
||||
|
||||
function auth_enabled(): bool {
|
||||
return active_pass_hash() !== '';
|
||||
}
|
||||
|
||||
function require_login(): void {
|
||||
if (!auth_enabled()) {
|
||||
return;
|
||||
}
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
if (empty($_SESSION['authenticated'])) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function attempt_login(string $username, string $password): bool {
|
||||
if (!auth_enabled()) {
|
||||
return true;
|
||||
}
|
||||
return $username === UI_USER && password_verify($password, active_pass_hash());
|
||||
}
|
||||
|
||||
function logout(): void {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash $new_password and write it to auth.passwd.
|
||||
* Returns true on success, false if the file could not be written.
|
||||
*/
|
||||
function save_password(string $new_password): bool {
|
||||
$hash = password_hash($new_password, PASSWORD_DEFAULT);
|
||||
return file_put_contents(AUTH_PASSWD_FILE, $hash . PHP_EOL, LOCK_EX) !== false;
|
||||
}
|
||||
4
includes/footer.php
Normal file
4
includes/footer.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php require_once __DIR__ . '/../version.php'; ?>
|
||||
<footer>
|
||||
portspoof<span>concentrator</span> · v<?= APP_VERSION ?>
|
||||
</footer>
|
||||
@@ -86,3 +86,10 @@ button[type=submit]:hover { opacity: .85; }
|
||||
.bar { height: 6px; background: var(--accent); border-radius: 3px; min-width: 2px; }
|
||||
|
||||
form { max-width: 480px; }
|
||||
|
||||
footer {
|
||||
text-align: center; padding: 1.25rem; margin-top: 1rem;
|
||||
font-size: .75rem; color: var(--muted);
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
footer span { color: var(--accent); }
|
||||
|
||||
Reference in New Issue
Block a user