From 6b5b05199c2473642ddb7900656f37ed17c46ef6 Mon Sep 17 00:00:00 2001 From: Vichingo455 Date: Fri, 3 Oct 2025 09:01:59 +0200 Subject: [PATCH] prova di integrazione SSO Google + riscrizionamento di alcune parti --- docker/php/config.php | 46 ++++++++---- htdocs/admin/login.php | 149 ++++++++++++++++++++++++++++++++++++--- htdocs/admin/logout.php | 2 +- htdocs/config/config.php | 47 ++++++++---- 4 files changed, 203 insertions(+), 41 deletions(-) diff --git a/docker/php/config.php b/docker/php/config.php index 4ff6a9c..5999ea6 100644 --- a/docker/php/config.php +++ b/docker/php/config.php @@ -21,21 +21,39 @@ if (!defined('YEAR')) { } // Impostazioni autenticazione dashboard amministrativa if (!defined('AUTH_TYPE')) { - define('AUTH_TYPE','local'); // Può essere keycloak o local (integrata) -} -if (!defined('KEYCLOAK_DOMAIN')) { - define('KEYCLOAK_DOMAIN',''); -} -if (!defined('KEYCLOAK_REALM')) { - define('KEYCLOAK_REALM',''); -} -if (!defined('KEYCLOAK_CLIENT_ID')) { - define('KEYCLOAK_CLIENT_ID',''); -} -if (!defined('KEYCLOAK_CLIENT_SECRET')) { - define('KEYCLOAK_CLIENT_SECRET',''); + define('AUTH_TYPE','local'); // Può essere local (integrata), keycloak, google } if (!defined('APP_DOMAIN')) { - define('APP_DOMAIN',''); + define('APP_DOMAIN',''); // Dominio del sito (ad esempio orario.yourdomain.com), richiesto per autenticazioni non local +} +// Impostazioni autenticazione via Keycloak (richiesto solo se AUTH_TYPE sta impostato su keycloak) +if (AUTH_TYPE === 'keycloak') { + if (!defined('KEYCLOAK_DOMAIN')) { + define('KEYCLOAK_DOMAIN',''); // Dominio di Keycloak (ad esempio auth.yourdomain.com) + } + if (!defined('KEYCLOAK_REALM')) { + define('KEYCLOAK_REALM',''); // Realm di Keycloak (ad esempio master) + } + if (!defined('KEYCLOAK_CLIENT_ID')) { + define('KEYCLOAK_CLIENT_ID',''); // Client ID per Keycloak (ad esempio orario) + } + if (!defined('KEYCLOAK_CLIENT_SECRET')) { + define('KEYCLOAK_CLIENT_SECRET',''); // Client Secret per Keycloak (ad esempio abcdefghijklm) + } +} +// Impostazioni autenticazione con Google (richieste solo se AUTH_TYPE sta impostato su google) +if (AUTH_TYPE === 'google') { + if (!defined('GOOGLE_CLIENT_ID')) { + define('GOOGLE_CLIENT_ID',''); // Client ID fornito da Google + } + if (!defined('GOOGLE_CLIENT_SECRET')) { + define('GOOGLE_CLIENT_SECRET',''); // Client Secret fornito da Google + } + if (!defined('GOOGLE_ONLY_ALLOWED_DOMAINS')) { + define('GOOGLE_ONLY_ALLOWED_DOMAINS', false); // Attivare (impostare su true) per impostare restrizioni sui domini e-mail consentiti + } + if (!defined('GOOGLE_ALLOWED_DOMAINS')) { + define('GOOGLE_ALLOWED_DOMAINS', ['']); // Domini E-Mail consentiti. Serve abilitare l'opzione GOOGLE_ONLY_ALLOWED_DOMAINS + } } ?> \ No newline at end of file diff --git a/htdocs/admin/login.php b/htdocs/admin/login.php index b774176..de984f7 100644 --- a/htdocs/admin/login.php +++ b/htdocs/admin/login.php @@ -3,22 +3,27 @@ use Jumbojett\OpenIDConnectClient; require 'vendor/autoload.php'; session_start(); include("../lib/db.php"); +if (isset($_SESSION['admin'])) { header("Location: index.php"); exit; } if ($_SERVER["REQUEST_METHOD"] == "POST" && AUTH_TYPE == 'local') { - $username = $_POST['username']; - $password = $_POST['password']; - $stmt = $conn->prepare("SELECT * FROM admin WHERE username = ?"); - $stmt->bind_param("s", $username); - $stmt->execute(); - $res = $stmt->get_result(); - if ($row = $res->fetch_assoc()) { - if (password_verify($password, $row['password'])) { + try { + $username = $_POST['username']; + $password = $_POST['password']; + $stmt = $conn->prepare("SELECT * FROM admin WHERE username = ?"); + $stmt->bind_param("s", $username); + $stmt->execute(); + $res = $stmt->get_result(); + if ($row = $res->fetch_assoc()) { + if (password_verify($password, $row['password'])) { $_SESSION['admin'] = $row['username']; $_SESSION['auth_type'] = 'local'; header("Location: index.php"); exit; - } + } + } + $error = "Credenziali non valide"; + } catch { + $error = "Errore durante l'autenticazione. Potrebbe essere un problema con PHP oppure col database."; } - $error = "Credenziali non valide"; } if (AUTH_TYPE == 'local') { echo << + + + Login Admin + + + + + + + + +
+

Login Admin

+HTML; +echo "
Errore durante l'autenticazione con Keycloak. Assicurati di avere impostato i vari parametri correttamente.
"; +echo << +

Copyright (C) 2025 EmmeV. - Released under GNU AGPL 3.0 License.

+ + +HTML; + exit; + } +} +else if (AUTH_TYPE === 'google') { + try { + $oidc = new OpenIDConnectClient( + 'https://accounts.google.com', + GOOGLE_CLIENT_ID, + GOOGLE_CLIENT_SECRET + ); + + $oidc->setRedirectURL(GOOGLE_REDIRECT_URI); + $oidc->addScope(['openid', 'email', 'profile']); + + // Callback da Google + if (isset($_GET['code'])) { + $oidc->authenticate(); + $email = $oidc->requestUserInfo('email'); + + $domain = substr(strrchr($email, "@"), 1); + + if (!GOOGLE_ONLY_ALLOWED_DOMAINS || in_array($domain, GOOGLE_ALLOWED_DOMAINS)) { + $_SESSION['admin'] = $email; + $_SESSION['auth_type'] = 'google'; + header("Location: index.php"); + exit; + } else { + http_response_code(403); + echo << + + + Login Admin + + + + + + + + +