3 Commits

Author SHA1 Message Date
Alexander Davis
49581da52c Broken login (#121)
* Fixing Login Die

* Fix Broken Login 2
2020-08-17 16:48:13 +01:00
57faf21a67 Fixing Login Die 2020-08-17 16:37:41 +01:00
5ff90e0d78 🐛 User not added to DB - force die() on error (#118)
* 🐛 User not added to DB - force die() on error

Signed-off-by: Luke Tainton <luke@tainton.uk>

* Restyled by php-cs-fixer (#119)

Co-authored-by: Restyled.io <commits@restyled.io>

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
Co-authored-by: Restyled.io <commits@restyled.io>
2020-08-17 16:17:06 +01:00
3 changed files with 53 additions and 43 deletions

View File

@@ -6,6 +6,22 @@
return $version; return $version;
} }
function user_exists($db, $uuid)
{
try {
$sql = $db->prepare("SELECT uuid FROM users WHERE uuid=:uuid");
$sql->bindParam(':uuid', $uuid);
$sql->execute();
} catch (PDOException $e) {
$alert = array("danger", "Error during check for user record: " . $e->getMessage());
}
if (empty($sql)) {
return false;
} else {
return true;
}
}
function get_all_users($db) function get_all_users($db)
{ {
try { try {

View File

@@ -1,9 +1,9 @@
<footer class="footer mt-auto py-3"> <footer class="footer mt-auto py-3">
<div class="text-center text-muted"> <div class="text-center text-muted">
<?php <?php
echo($_ENV['APP_NAME'] . " " . get_version()); echo($_ENV['APP_NAME'] . " " . get_version() . ".<br>");
if ($_ENV['APP_NAME'] != "FHeD") { if ($_ENV['APP_NAME'] != "FHeD") {
echo(", powered by FHeD"); echo("Powered by FHeD.");
}; };
?><br> ?><br>
<?php if (is_signed_in()) { <?php if (is_signed_in()) {

View File

@@ -4,9 +4,9 @@
// Perform the OIDC authentication // Perform the OIDC authentication
try { try {
$oidc->authenticate(); $oidc->authenticate();
$_SESSION['access_token'] = $oidc->requestClientCredentialsToken()->access_token; $_SESSION['access_token'] = $oidc->requestClientCredentialsToken()->access_token;
$oidc_user = array( $oidc_user = array(
'sub' => $oidc->requestUserInfo('sub'), 'sub' => $oidc->requestUserInfo('sub'),
'username' => $oidc->requestUserInfo('preferred_username'), 'username' => $oidc->requestUserInfo('preferred_username'),
'given_name' => $oidc->requestUserInfo('given_name'), 'given_name' => $oidc->requestUserInfo('given_name'),
@@ -14,46 +14,41 @@
'email' => $oidc->requestUserInfo('email'), 'email' => $oidc->requestUserInfo('email'),
); );
} catch (Jumbojett\OpenIDConnectClientException $e) { } catch (Jumbojett\OpenIDConnectClientException $e) {
$alert = array("danger", "Error during OpenID Connect authentication: " . $e->getMessage()); $alert = array("danger", "Error during OpenID Connect authentication: " . $e->getMessage());
} }
// Check if the user already exists if (user_exists($db, $oidc_user['sub']) == false) {
try { // User doesn't already exist
$user_exist_sql = $db->prepare("SELECT uuid FROM users WHERE uuid=:uuid"); try {
$user_exist_sql->bindParam(':uuid', $oidc_user['sub']); $stmt = "INSERT INTO users (uuid, uid, given_name, family_name, email) VALUES (:sub, :username, :given, :family, :email)";
$user_exist_sql->execute(); $sql = $db->prepare($stmt);
} catch (PDOException $e) { $sql->bindParam(':sub', $oidc_user['sub']);
$alert = array("danger", "Error during check for user record: " . $e->getMessage()); $sql->bindParam(':username', $oidc_user['username']);
} $sql->bindParam(':given', $oidc_user['given_name']);
$sql->bindParam(':family', $oidc_user['family_name']);
if (empty($user_exist_sql)) { $sql->bindParam(':email', $oidc_user['email']);
// User doesn't already exist $sql->execute();
try { } catch (PDOException $e) {
$stmt = "INSERT INTO users (uuid, uid, given_name, family_name, email) VALUES (:sub, :username, :given, :family, :email)"; echo("Error during creation of new user record: " . $e->getMessage());
$sql = $db->prepare($stmt); die();
$sql->bindParam(':sub', $oidc_user['sub']); $alert = array("danger", "Error during creation of new user record: " . $e->getMessage());
$sql->bindParam(':username', $oidc_user['username']); }
$sql->bindParam(':given', $oidc_user['given_name']);
$sql->bindParam(':family', $oidc_user['family_name']);
$sql->bindParam(':email', $oidc_user['email']);
$sql->execute();
} catch (Jumbojett\PDOException $e) {
$alert = array("danger", "Error during creation of new user record: " . $e->getMessage());
}
} else { } else {
// User already exists // User already exists
try { try {
$stmt = "UPDATE users SET uid=:username, given_name=:given, family_name=:family, email=:email WHERE uuid=:sub"; $stmt = "UPDATE users SET uid=:username, given_name=:given, family_name=:family, email=:email WHERE uuid=:sub";
$sql = $db->prepare($stmt); $sql = $db->prepare($stmt);
$sql->bindParam(':sub', $oidc_user['sub']); $sql->bindParam(':sub', $oidc_user['sub']);
$sql->bindParam(':username', $oidc_user['username']); $sql->bindParam(':username', $oidc_user['username']);
$sql->bindParam(':given', $oidc_user['given_name']); $sql->bindParam(':given', $oidc_user['given_name']);
$sql->bindParam(':family', $oidc_user['family_name']); $sql->bindParam(':family', $oidc_user['family_name']);
$sql->bindParam(':email', $oidc_user['email']); $sql->bindParam(':email', $oidc_user['email']);
$sql->execute(); $sql->execute();
} catch (Jumbojett\PDOException $e) { } catch (PDOException $e) {
$alert = array("danger", "Error during existing user record update: " . $e->getMessage()); echo("Error during existing user record update: " . $e->getMessage());
} die();
$alert = array("danger", "Error during existing user record update: " . $e->getMessage());
}
} }
oidc_set_vars($oidc_user['sub'], $oidc_user['username'], $oidc_user['given_name'], $oidc_user['family_name'], $oidc_user['email']); oidc_set_vars($oidc_user['sub'], $oidc_user['username'], $oidc_user['given_name'], $oidc_user['family_name'], $oidc_user['email']);
@@ -62,4 +57,3 @@
$_SESSION['is_signed_in'] = "true"; $_SESSION['is_signed_in'] = "true";
header('Location: /'); header('Location: /');
?>