diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 11:06:30 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 11:06:30 +0300 |
| commit | fa1bccd968647f6ab5cce0ec4293b39c6fde7633 (patch) | |
| tree | a51b489a43bdfcc2688bd0855cf3e32382ad958c /classes/Handler/LoginHandler.php | |
| parent | ab7702f0e3dd63bdeaea3cb6db138878774c3b47 (diff) | |
| download | the-epube-fa1bccd968647f6ab5cce0ec4293b39c6fde7633.tar.gz the-epube-fa1bccd968647f6ab5cce0ec4293b39c6fde7633.tar.xz | |
spaces to tabs
Diffstat (limited to 'classes/Handler/LoginHandler.php')
| -rw-r--r-- | classes/Handler/LoginHandler.php | 314 |
1 files changed, 157 insertions, 157 deletions
diff --git a/classes/Handler/LoginHandler.php b/classes/Handler/LoginHandler.php index ded6e8c..c97a93e 100644 --- a/classes/Handler/LoginHandler.php +++ b/classes/Handler/LoginHandler.php @@ -11,162 +11,162 @@ use Response; class LoginHandler implements RequestHandlerInterface { - /** @return array<string, string>|null */ - private function doLocalLogin(string $username, string $password): ?array - { - $pass_hash = 'SHA256:' . hash('sha256', "$username:" . $password); - - $user = ORM::for_table('epube_users') - ->where('username', $username) - ->where('pass', $pass_hash) - ->find_one(); - - if (!$user) { - return null; - } - - 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)); - - return ["owner" => $username]; - } - - private function doOidcLogin(): void - { - $oidc = new OpenIDConnectClient( - Config::get(Config::OIDC_URL), - Config::get(Config::OIDC_CLIENT_ID), - Config::get(Config::OIDC_CLIENT_SECRET) - ); - - $oidc->setRedirectURL(Config::get_self_url() . "/backend.php?op=oidc-login-callback"); - $oidc->addScope(['openid', 'profile', 'email']); - $oidc->authenticate(); - } - - /** @return array<string, string> */ - private function handleOidcCallback(): array - { - $oidc = new OpenIDConnectClient( - Config::get(Config::OIDC_URL), - Config::get(Config::OIDC_CLIENT_ID), - Config::get(Config::OIDC_CLIENT_SECRET) - ); - - $oidc->setRedirectURL(Config::get_self_url() . "/backend.php?op=oidc-login-callback"); - $oidc->addScope(['openid', 'profile', 'email']); - - try { - $oidc->authenticate(); - - $username = trim(mb_strtolower($oidc->requestUserInfo("preferred_username"))); - - if (!$username) { - return ["error" => "No username received from OIDC provider."]; - } - - $user = ORM::for_table('epube_users') - ->where('username', $username) - ->find_one(); - - if (!$user) { - return ["error" => "User not found: $username"]; - } - - if (session_status() != PHP_SESSION_ACTIVE) { - session_start(); - } - - session_regenerate_id(true); - - $_SESSION["access_token"] = $oidc->getAccessToken(); - $_SESSION["access_token_last_check"] = time(); - $_SESSION["owner"] = $username; - $_SESSION["pass_hash"] = sha1($user->pass); - $_SESSION["csrf_token"] = bin2hex(random_bytes(16)); - - return ["owner" => $username]; - - } catch (\Exception $e) { - return ["error" => "OIDC Error: " . $e->getMessage()]; - } - } - - public function handle(Request $request): Response - { - $op = $request->getAttribute('op', ''); - - // Redirect to OIDC provider - if ($op === "perform-oidc-login") { - if (empty(Config::get(Config::OIDC_URL))) { - return (new Response()) - ->withStatus(400) - ->withBody(json_encode(["error" => "OIDC is not configured."])); - } - - $this->doOidcLogin(); - - // unreachable - authenticate() redirects - return new Response(); - } - - // OIDC callback - if ($op === "oidc-login-callback") { - if (empty(Config::get(Config::OIDC_URL))) { - return (new Response()) - ->withStatus(400) - ->withBody(json_encode(["error" => "OIDC is not configured."])); - } - - $result = $this->handleOidcCallback(); - - if (isset($result["error"])) { - // Logout any partial session state - if (function_exists('logout_user')) { - logout_user(); - } - return (new Response()) - ->withStatus(401) - ->withBody(json_encode($result)); - } - - return (new Response()) + /** @return array<string, string>|null */ + private function doLocalLogin(string $username, string $password): ?array + { + $pass_hash = 'SHA256:' . hash('sha256', "$username:" . $password); + + $user = ORM::for_table('epube_users') + ->where('username', $username) + ->where('pass', $pass_hash) + ->find_one(); + + if (!$user) { + return null; + } + + 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)); + + return ["owner" => $username]; + } + + private function doOidcLogin(): void + { + $oidc = new OpenIDConnectClient( + Config::get(Config::OIDC_URL), + Config::get(Config::OIDC_CLIENT_ID), + Config::get(Config::OIDC_CLIENT_SECRET) + ); + + $oidc->setRedirectURL(Config::get_self_url() . "/backend.php?op=oidc-login-callback"); + $oidc->addScope(['openid', 'profile', 'email']); + $oidc->authenticate(); + } + + /** @return array<string, string> */ + private function handleOidcCallback(): array + { + $oidc = new OpenIDConnectClient( + Config::get(Config::OIDC_URL), + Config::get(Config::OIDC_CLIENT_ID), + Config::get(Config::OIDC_CLIENT_SECRET) + ); + + $oidc->setRedirectURL(Config::get_self_url() . "/backend.php?op=oidc-login-callback"); + $oidc->addScope(['openid', 'profile', 'email']); + + try { + $oidc->authenticate(); + + $username = trim(mb_strtolower($oidc->requestUserInfo("preferred_username"))); + + if (!$username) { + return ["error" => "No username received from OIDC provider."]; + } + + $user = ORM::for_table('epube_users') + ->where('username', $username) + ->find_one(); + + if (!$user) { + return ["error" => "User not found: $username"]; + } + + if (session_status() != PHP_SESSION_ACTIVE) { + session_start(); + } + + session_regenerate_id(true); + + $_SESSION["access_token"] = $oidc->getAccessToken(); + $_SESSION["access_token_last_check"] = time(); + $_SESSION["owner"] = $username; + $_SESSION["pass_hash"] = sha1($user->pass); + $_SESSION["csrf_token"] = bin2hex(random_bytes(16)); + + return ["owner" => $username]; + + } catch (\Exception $e) { + return ["error" => "OIDC Error: " . $e->getMessage()]; + } + } + + public function handle(Request $request): Response + { + $op = $request->getAttribute('op', ''); + + // Redirect to OIDC provider + if ($op === "perform-oidc-login") { + if (empty(Config::get(Config::OIDC_URL))) { + return (new Response()) + ->withStatus(400) + ->withBody(json_encode(["error" => "OIDC is not configured."])); + } + + $this->doOidcLogin(); + + // unreachable - authenticate() redirects + return new Response(); + } + + // OIDC callback + if ($op === "oidc-login-callback") { + if (empty(Config::get(Config::OIDC_URL))) { + return (new Response()) + ->withStatus(400) + ->withBody(json_encode(["error" => "OIDC is not configured."])); + } + + $result = $this->handleOidcCallback(); + + if (isset($result["error"])) { + // Logout any partial session state + if (function_exists('logout_user')) { + logout_user(); + } + return (new Response()) + ->withStatus(401) + ->withBody(json_encode($result)); + } + + return (new Response()) ->withHeader("Location", Config::get_self_url() . "/index.php") - ->withBody(json_encode(["result" => "ok", "user" => $result["owner"]])); - } - - // Local username/password login - if ($op === "perform-login") { - $username = trim(mb_strtolower($request->getAttribute('user', ''))); - $password = $request->getAttribute('password', ''); - - if (!$username || !$password) { - return (new Response()) - ->withStatus(400) - ->withBody(json_encode(["error" => "Username and password are required."])); - } - - $result = $this->doLocalLogin($username, $password); - - if (!$result) { - return (new Response()) - ->withStatus(401) - ->withBody(json_encode(["error" => "Incorrect username or password."])); - } - - return (new Response()) - ->withBody(json_encode(["result" => "ok", "user" => $result["owner"]])); - } - - return (new Response()) - ->withStatus(404) - ->withBody("Not found."); - } + ->withBody(json_encode(["result" => "ok", "user" => $result["owner"]])); + } + + // Local username/password login + if ($op === "perform-login") { + $username = trim(mb_strtolower($request->getAttribute('user', ''))); + $password = $request->getAttribute('password', ''); + + if (!$username || !$password) { + return (new Response()) + ->withStatus(400) + ->withBody(json_encode(["error" => "Username and password are required."])); + } + + $result = $this->doLocalLogin($username, $password); + + if (!$result) { + return (new Response()) + ->withStatus(401) + ->withBody(json_encode(["error" => "Incorrect username or password."])); + } + + return (new Response()) + ->withBody(json_encode(["result" => "ok", "user" => $result["owner"]])); + } + + return (new Response()) + ->withStatus(404) + ->withBody("Not found."); + } } |
