Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Session manager & dedicated login #1735

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions ajax/session/do_check_session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

require '../../includes/csrf.php';
require_once '../../includes/session.php';
require_once '../../includes/config.php';
require_once '../../src/RaspAP/Auth/HTTPAuth.php';
require_once '../../includes/authenticate.php';

$lastActivity = $_SESSION['lastActivity'] ?? time();
$sessionLifetime = time() - $lastActivity;
$status = $sessionLifetime >= RASPI_SESSION_TIMEOUT ? 'session_expired' : 'active';

// send response
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
header('Pragma: no-cache');

$response = [
'status' => $status,
'last_activity' => $lastActivity,
'session_lifetime' => $sessionLifetime,
'timeout_duration' => RASPI_SESSION_TIMEOUT
];

echo json_encode($response);
exit();

22 changes: 22 additions & 0 deletions app/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,28 @@ window.addEventListener('load', function() {
});
}, false);

let sessionCheckInterval = setInterval(checkSession, 5000);

function checkSession() {
$.get('ajax/session/do_check_session.php', function (data) {
if (data.status === 'session_expired') {
clearInterval(sessionCheckInterval);
showSessionExpiredModal();
}
}).fail(function (jqXHR, status, err) {
console.error("Error checking session status:", status, err);
});
}

function showSessionExpiredModal() {
$('#sessionTimeoutModal').modal('show');
}

$(document).on("click", "#js-session-expired-login", function(e) {
console.log('clicked!');
window.location.href = '/login';
});

// DHCP or Static IP option group
$('#chkstatic').on('change', function() {
if (this.checked) {
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
define('RASPI_ERROR_LOG', sys_get_temp_dir() . '/raspap_error.log');
define('RASPI_DEBUG_LOG', 'raspap_debug.log');
define('RASPI_LOG_SIZE_LIMIT', 64);
define('RASPI_SESSION_TIMEOUT', 1440);

// Constants for configuration file paths.
// These are typical for default RPi installs. Modify if needed.
Expand Down
1 change: 0 additions & 1 deletion includes/csrf.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

require_once 'functions.php';
require_once 'session.php';

if (csrfValidateRequest() && !CSRFValidate()) {
handleInvalidCSRFToken();
Expand Down
1 change: 1 addition & 0 deletions includes/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'RASPI_ERROR_LOG' => sys_get_temp_dir() . '/raspap_error.log',
'RASPI_DEBUG_LOG' => 'raspap_debug.log',
'RASPI_LOG_SIZE_LIMIT' => 64,
'RASPI_SESSION_TIMEOUT' => 1440,

// Constants for configuration file paths.
// These are typical for default RPi installs. Modify if needed.
Expand Down
18 changes: 18 additions & 0 deletions includes/footer.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php $_SESSION['lastActivity'] = time(); ?>

<div class="d-flex align-items-center justify-content-between small">
<div class="text-muted">
<span class="pe-2"><a href="/about">v<?php echo RASPI_VERSION; ?></a></span> |
Expand All @@ -8,3 +10,19 @@
</div>
</div>

<div class="modal fade" id="sessionTimeoutModal" tabindex="-1" aria-labelledby="sessionTimeoutLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title" id="sessionTimeoutLabel"><i class="fa fa-clock me-2"></i><?php echo _("Session Expired"); ?></div>
</div>
<div class="modal-body">
<?php echo _("Your session has expired. Please login to continue.") ?>
</div>
<div class="modal-footer">
<button type="button" id="js-session-expired-login" class="btn btn-outline btn-primary"><?php echo _("Login"); ?></button>
</div>
</div>
</div>
</div>

5 changes: 5 additions & 0 deletions includes/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
if (session_status() == PHP_SESSION_NONE) {
session_start();
}

if (!isset($_SESSION['lastActivity'])) {
$_SESSION['lastActivity'] = time();
}

1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* as you leave these references intact in the header comments of your source files.
*/

require 'includes/session.php';
require 'includes/csrf.php';
ensureCSRFSessionToken();

Expand Down
Loading