From 78bb9f35ed15ea448f592997125f6f8636e27ce6 Mon Sep 17 00:00:00 2001 From: Partydragen Date: Sun, 17 Dec 2023 15:46:52 +0100 Subject: [PATCH] Make OAuth optional for register/login --- core/classes/Misc/NamelessOAuth.php | 18 ----------- custom/languages/en_UK.json | 3 +- .../Default/core/integrations_edit.tpl | 31 ++++++++++--------- .../Integrations/GoogleIntegration.php | 2 +- modules/Core/pages/login.php | 10 ++++-- modules/Core/pages/panel/integrations.php | 5 +-- modules/Core/pages/register.php | 10 ++++-- .../classes/DiscordIntegration.php | 2 +- 8 files changed, 38 insertions(+), 43 deletions(-) diff --git a/core/classes/Misc/NamelessOAuth.php b/core/classes/Misc/NamelessOAuth.php index b63da946db..8263e21c16 100644 --- a/core/classes/Misc/NamelessOAuth.php +++ b/core/classes/Misc/NamelessOAuth.php @@ -56,20 +56,6 @@ public function getProvider(string $provider): ?array { return null; } - /** - * Determine if OAuth is available if at least one provider is setup. - * - * @return bool If any provider is setup - */ - public function isAvailable(): bool { - foreach (array_keys($this->_providers) as $provider) { - if ($this->isSetup($provider)) { - return true; - } - } - return false; - } - /** * Get an array of provider names and their URL & icon. * @@ -180,10 +166,6 @@ public function setEnabled(string $provider, int $enabled): void { * @return bool If the provider is setup */ public function isSetup(string $provider): bool { - if (!$this->isEnabled($provider)) { - return false; - } - [$client_id, $client_secret] = $this->getCredentials($provider); return $client_id !== '' && $client_secret !== ''; diff --git a/custom/languages/en_UK.json b/custom/languages/en_UK.json index efd3fd28f1..bf015d2750 100644 --- a/custom/languages/en_UK.json +++ b/custom/languages/en_UK.json @@ -416,7 +416,8 @@ "admin/not_set": "Not set", "admin/notice_log": "Notice log", "admin/oauth": "OAuth", - "admin/oauth_info": "Configure OAuth providers to allow users to login with their social network accounts. {{docLinkStart}}Check out our documentation for help{{docLinkEnd}}.", + "admin/oauth_info": "Configure OAuth provider to allow users to link and login with their social network accounts. {{docLinkStart}}Check out our documentation for help{{docLinkEnd}}.", + "admin/register_login_with_oauth": "Allow user to register and login using this integration?", "admin/og_image_reset_successfully": "OG image reset successfully.", "admin/og_image_updated_successfully": "OG image updated successfully.", "admin/fallback_og_image_x": "Fallback OG image: {{imageName}}", diff --git a/custom/panel_templates/Default/core/integrations_edit.tpl b/custom/panel_templates/Default/core/integrations_edit.tpl index affe58db9f..332599d509 100644 --- a/custom/panel_templates/Default/core/integrations_edit.tpl +++ b/custom/panel_templates/Default/core/integrations_edit.tpl @@ -89,31 +89,34 @@ {/if} {if isset($OAUTH)} -
{$OAUTH}
-
-
-
{$INFO}
- {$OAUTH_INFO} -
-
-
+ +
{$OAUTH} {if $OAUTH_PROVIDER_DATA['logo_url']} + {$OAUTH_PROVIDER_DATA['name']|ucfirst} Logo + {elseif $OAUTH_PROVIDER_DATA['icon']} + + {/if}
+
+
+
+
{$INFO}
+ {$OAUTH_INFO} +
+
+ +
+
diff --git a/modules/Core/classes/Integrations/GoogleIntegration.php b/modules/Core/classes/Integrations/GoogleIntegration.php index b44bc9a28f..138029d11a 100644 --- a/modules/Core/classes/Integrations/GoogleIntegration.php +++ b/modules/Core/classes/Integrations/GoogleIntegration.php @@ -108,7 +108,7 @@ public function validateIdentifier(string $identifier, int $integration_user_id } public function allowLinking(): bool { - return NamelessOAuth::getInstance()->isEnabled('google'); + return NamelessOAuth::getInstance()->isSetup('google'); } public function onRegistrationPageLoad(Fields $fields) { diff --git a/modules/Core/pages/login.php b/modules/Core/pages/login.php index edc0613ed9..420823f79a 100644 --- a/modules/Core/pages/login.php +++ b/modules/Core/pages/login.php @@ -258,8 +258,12 @@ } // Add "login with..." message to provider array -$providers = NamelessOAuth::getInstance()->getProvidersAvailable(); -foreach ($providers as $name => $provider) { +$providers = []; +foreach (NamelessOAuth::getInstance()->getProvidersAvailable() as $name => $provider) { + if (!NamelessOAuth::getInstance()->isEnabled($name)) + continue; + + $providers[$name] = $provider; $providers[$name]['log_in_with'] = $language->get('user', 'log_in_with', [ 'provider' => ucfirst($name) ]); @@ -278,7 +282,7 @@ 'ERROR_TITLE' => $language->get('general', 'error'), 'ERROR' => ($return_error ?? []), 'NOT_REGISTERED_YET' => $language->get('general', 'not_registered_yet'), - 'OAUTH_AVAILABLE' => NamelessOAuth::getInstance()->isAvailable(), + 'OAUTH_AVAILABLE' => count($providers), 'OAUTH_PROVIDERS' => $providers, 'OR' => $language->get('general', 'or'), ]); diff --git a/modules/Core/pages/panel/integrations.php b/modules/Core/pages/panel/integrations.php index bb562b7920..7bb6079612 100644 --- a/modules/Core/pages/panel/integrations.php +++ b/modules/Core/pages/panel/integrations.php @@ -107,8 +107,8 @@ 'name' => $provider_name, 'enabled' => NamelessOAuth::getInstance()->isEnabled($provider_name), 'setup' => NamelessOAuth::getInstance()->isSetup($provider_name), - 'icon' => $provider_data['icon'] ?? null, - 'logo_url' => $provider_data['logo_url'] ?? null, + 'icon' => $provider['icon'] ?? null, + 'logo_url' => $provider['logo_url'] ?? null, 'client_id' => $client_id, 'client_secret' => $client_secret, 'client_url' => rtrim(URL::getSelfURL(), '/') . URL::build('/oauth', 'provider=' . $provider_name, 'non-friendly'), @@ -123,6 +123,7 @@ 'REDIRECT_URL' => $language->get('admin', 'redirect_url'), 'CLIENT_ID' => $language->get('admin', 'client_id'), 'CLIENT_SECRET' => $language->get('admin', 'client_secret'), + 'REGISTER_LOGIN_WITH_OAUTH' => $language->get('admin', 'register_login_with_oauth'), 'OAUTH_URL' => rtrim(URL::getSelfURL(), '/') . URL::build('/oauth', 'provider={{provider}}', 'non-friendly'), 'OAUTH_PROVIDER_DATA' => $oauth_provider_data ]); diff --git a/modules/Core/pages/register.php b/modules/Core/pages/register.php index 638472a859..2485996ce6 100644 --- a/modules/Core/pages/register.php +++ b/modules/Core/pages/register.php @@ -397,8 +397,12 @@ } // Add "continue with..." message to provider array -$providers = NamelessOAuth::getInstance()->getProvidersAvailable(); -foreach ($providers as $name => $provider) { +$providers = []; +foreach (NamelessOAuth::getInstance()->getProvidersAvailable() as $name => $provider) { + if (!NamelessOAuth::getInstance()->isEnabled($name)) + continue; + + $providers[$name] = $provider; $providers[$name]['continue_with'] = $language->get('user', 'continue_with', [ 'provider' => ucfirst($name) ]); @@ -421,7 +425,7 @@ 'ERROR_TITLE' => $language->get('general', 'error'), 'OR' => $language->get('general', 'or'), 'OAUTH_FLOW' => $oauth_flow, - 'OAUTH_AVAILABLE' => NamelessOAuth::getInstance()->isAvailable(), + 'OAUTH_AVAILABLE' => count($providers), 'OAUTH_PROVIDERS' => $providers, ]); diff --git a/modules/Discord Integration/classes/DiscordIntegration.php b/modules/Discord Integration/classes/DiscordIntegration.php index 88fd204d35..5b5b14fa51 100644 --- a/modules/Discord Integration/classes/DiscordIntegration.php +++ b/modules/Discord Integration/classes/DiscordIntegration.php @@ -147,7 +147,7 @@ public function validateIdentifier(string $identifier, int $integration_user_id public function allowLinking(): bool { $link_method = Settings::get('integration_link_method', 'bot', 'Discord Integration'); if ($link_method == 'oauth') { - return NamelessOAuth::getInstance()->isEnabled('discord'); + return NamelessOAuth::getInstance()->isSetup('discord'); } else { return Discord::isBotSetup(); }