about summary refs log tree commit diff stats
path: root/login.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2024-01-23 16:58:26 +0300
committerAndrew Dolgov <fox@fakecake.org>2024-01-23 16:58:26 +0300
commit3946e9546604e531b0f3eb7849dd6d2dea247d9c (patch)
tree1e77eac130594be9682b0b41a1c270057faa0785 /login.php
parent452ad8003719bb467d9c8eb041f726aa19f95125 (diff)
downloadthe-epube-3946e9546604e531b0f3eb7849dd6d2dea247d9c.tar.gz
the-epube-3946e9546604e531b0f3eb7849dd6d2dea247d9c.tar.xz
add basic oidc support
Diffstat (limited to 'login.php')
-rw-r--r--login.php63
1 files changed, 60 insertions, 3 deletions
diff --git a/login.php b/login.php
index 37bed4b..a8df8ac 100644
--- a/login.php
+++ b/login.php
@@ -1,10 +1,11 @@
 <?php
-	set_include_path(__DIR__ ."/include" . PATH_SEPARATOR .
-	get_include_path());
+	set_include_path(__DIR__ . "/include" . PATH_SEPARATOR . get_include_path());
 
 	require_once "common.php";
 	require_once "sessions.php";
 
+	use Jumbojett\OpenIDConnectClient;
+
 	Config::sanity_check();
 
 	$op = $_REQUEST["op"] ?? "";
@@ -36,6 +37,54 @@
 		} else {
 			$login_notice = "Incorrect username or password";
 		}
+	} else if ($op == "perform-oidc-login") {
+      $oidc = new OpenIDConnectClient(Config::get(Config::OIDC_URL),
+         Config::get(Config::OIDC_CLIENT_ID),
+         Config::get(Config::OIDC_CLIENT_SECRET));
+
+      $oidc->setRedirectURL(Config::make_self_url() . "/login.php");
+      $oidc->addScope(['openid', 'profile', 'email']);
+      $oidc->authenticate();
+
+		exit;
+	} else if ($_REQUEST['code'] ?? false) {
+
+      $oidc = new OpenIDConnectClient(Config::get(Config::OIDC_URL),
+         Config::get(Config::OIDC_CLIENT_ID),
+         Config::get(Config::OIDC_CLIENT_SECRET));
+
+		try {
+			$oidc->setRedirectURL(Config::make_self_url() . "/login.php");
+			$oidc->addScope(['openid', 'profile', 'email']);
+			$oidc->authenticate();
+
+			$username = trim(mb_strtolower($oidc->requestUserInfo("preferred_username")));
+
+			if ($username) {
+				$user = ORM::for_table('epube_users')
+					->where('user', $username)
+					->find_one();
+
+				if ($user) {
+					if (session_status() != PHP_SESSION_ACTIVE)
+						session_start();
+
+					session_regenerate_id(true);
+
+					$_SESSION["owner"] = $username;
+					$_SESSION["pass_hash"] = sha1($user->pass);
+					$_SESSION["csrf_token"] = bin2hex(random_bytes(16));
+
+					header("Location: index.php");
+					exit;
+				}
+			}
+
+		} catch (Exception $e) {
+			$login_notice = 'OIDC Error: ' . $e->getMessage();
+		}
+
+		logout_user();
 	} else {
 		logout_user();
 	}
@@ -54,6 +103,11 @@
 		<link rel="manifest" href="manifest.json">
 		<meta name="mobile-web-app-capable" content="yes">
 		<script type="text/javascript">
+			function oidc_login() {
+				window.location.href = '?op=perform-oidc-login';
+				return false;
+			}
+
 			$(document).ready(function() {
 					/* global EpubeApp */
 
@@ -88,7 +142,10 @@
 					<label>Password</label>
 					<input type="password" class="form-control" name="password" required="true">
 				</div>
-				<button type="submit" class="btn btn-default">Log in</button>
+				<button type="submit" class="btn btn-primary">Log in</button>
+				<?php if (Config::get(Config::OIDC_URL) != "") { ?>
+					<button type="submit" onclick="return oidc_login()" class="btn btn-default">Log in with <?= Config::get(Config::OIDC_NAME) ?></button>
+				<?php } ?>
 			</form>
 		</div>
 	</body>