From 77ef004d5ac39e6f2276deaee0b7298fef27819f Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 24 Jul 2022 16:35:06 +0300 Subject: add configuration variables --- init.php | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) (limited to 'init.php') diff --git a/init.php b/init.php index 4ed6c78..4ac8004 100644 --- a/init.php +++ b/init.php @@ -9,6 +9,11 @@ class Auth_OIDC extends Auth_Base { * TTRSS_AUTH_OIDC_POST_LOGOUT_URL=http://127.0.0.1/logout-redirect */ const AUTH_OIDC_POST_LOGOUT_URL = "AUTH_OIDC_POST_LOGOUT_URL"; + const AUTH_OIDC_NAME = "AUTH_OIDC_NAME"; + const AUTH_OIDC_URL = "AUTH_OIDC_URL"; + const AUTH_OIDC_CLIENT_ID = "AUTH_OIDC_CLIENT_ID"; + const AUTH_OIDC_CLIENT_SECRET = "AUTH_OIDC_CLIENT_SECRET"; + /** @var PluginHost $host */ private $host; @@ -21,35 +26,46 @@ class Auth_OIDC extends Auth_Base { } function init($host) { - $host->add_hook($host::HOOK_AUTH_USER, $this); - Config::add(self::AUTH_OIDC_POST_LOGOUT_URL, "", Config::T_STRING); + Config::add(self::AUTH_OIDC_NAME, "", Config::T_STRING); + Config::add(self::AUTH_OIDC_URL, "", Config::T_STRING); + Config::add(self::AUTH_OIDC_CLIENT_ID, "", Config::T_STRING); + Config::add(self::AUTH_OIDC_CLIENT_SECRET, "", Config::T_STRING); + + if (Config::get(self::AUTH_OIDC_URL)) { + $host->add_hook($host::HOOK_AUTH_USER, $this); + $host->add_hook($host::HOOK_LOGINFORM_ADDITIONAL_BUTTONS, $this); - if (Config::get(self::AUTH_OIDC_POST_LOGOUT_URL) != "") { - $host->add_hook($host::HOOK_POST_LOGOUT, $this); + if (Config::get(self::AUTH_OIDC_POST_LOGOUT_URL) != "") + $host->add_hook($host::HOOK_POST_LOGOUT, $this); } $this->host = $host; } function is_public_method($method) { - return $method == "callback"; + return $method == "oidc_login"; } - function callback() { - print "IN_CALLBACK"; - die; + public function oidc_login() : void { + $oidc = new OpenIDConnectClient(Config::get(self::AUTH_OIDC_URL), + Config::get(self::AUTH_OIDC_CLIENT_ID), + Config::get(self::AUTH_OIDC_CLIENT_SECRET)); + + $oidc->setRedirectURL(Config::get_self_url()); + $oidc->addScope(['openid', 'profile', 'email']); + $oidc->authenticate(); } function authenticate($login, $password, $service = '') { - $oidc = new OpenIDConnectClient('https://auth.fakecake.org', - 'dev-debian-ttrss', - 'Bu3vuCi0wBeQteJ7di4H6SKgqvYnpSludEP68SHu9wLekxXl'); + if (!($_SESSION['uid'] ?? false) && ($_REQUEST['code'] ?? false)) { - if (!($_SESSION['uid'] ?? false)) { - $oidc->setRedirectURL(Config::get_self_url()); + $oidc = new OpenIDConnectClient(Config::get(self::AUTH_OIDC_URL), + Config::get(self::AUTH_OIDC_CLIENT_ID), + Config::get(self::AUTH_OIDC_CLIENT_SECRET)); try { + $oidc->setRedirectURL(Config::get_self_url()); $oidc->addScope(['openid', 'profile', 'email']); $oidc->authenticate(); @@ -58,7 +74,6 @@ class Auth_OIDC extends Auth_Base { $user_id = $this->auto_create_user($login, $password); if ($user_id) { - $name = $oidc->requestUserInfo("name"); if ($name) { @@ -77,14 +92,22 @@ class Auth_OIDC extends Auth_Base { return $user_id; } catch (Exception $e) { - var_dump($e); - die; + $_SESSION["login_error_msg"] = 'OIDC: ' . $e->getMessage(); } } return false; } + function get_login_js() { + return file_get_contents(__DIR__ . "/init.js"); + } + + function hook_loginform_additional_buttons() { + print \Controls\button_tag(T_sprintf('Log in with %s', Config::get(self::AUTH_OIDC_NAME)), '', + ['class' => '', 'onclick' => 'Plugins.Auth_OIDC.login("'.htmlspecialchars($this->host->get_public_method_url($this, "oidc_login")).'")']); + } + function hook_post_logout($login, $user_id) { return [ Config::get(self::AUTH_OIDC_POST_LOGOUT_URL) -- cgit v1.2.3