Skip to content

Commit

Permalink
Make OAuth optional for register/login
Browse files Browse the repository at this point in the history
  • Loading branch information
partydragen committed Dec 17, 2023
1 parent 7f211d4 commit 78bb9f3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 43 deletions.
18 changes: 0 additions & 18 deletions core/classes/Misc/NamelessOAuth.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 !== '';
Expand Down
3 changes: 2 additions & 1 deletion custom/languages/en_UK.json
Expand Up @@ -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}}",
Expand Down
31 changes: 17 additions & 14 deletions custom/panel_templates/Default/core/integrations_edit.tpl
Expand Up @@ -89,31 +89,34 @@
{/if}

{if isset($OAUTH)}
<h5>{$OAUTH}</h5>
<div class="card shadow border-left-primary">
<div class="card-body">
<h5><i class="icon fa fa-info-circle"></i> {$INFO}</h5>
{$OAUTH_INFO}
</div>
</div>
<br />
<form action="" method="post">
<div class="row">
<div class="col">
<div class="card shadow mb-4">
<div class="card-body">

<h5>{$OAUTH} {if $OAUTH_PROVIDER_DATA['logo_url']}
<img src="{$OAUTH_PROVIDER_DATA['logo_url']}" alt="{$OAUTH_PROVIDER_DATA['name']|ucfirst} Logo" style="width: 16px; height: auto;">
{elseif $OAUTH_PROVIDER_DATA['icon']}
<i class="{$OAUTH_PROVIDER_DATA['icon']}"></i>
{/if}</h5>
<hr />
<div class="card shadow border-left-primary">
<div class="card-body">
<h5><i class="icon fa fa-info-circle"></i> {$INFO}</h5>
{$OAUTH_INFO}
</div>
</div>

<br />

<div class="form-group custom-control custom-switch">
<input id="enable" name="enable"
type="checkbox" class="custom-control-input" {if $OAUTH_PROVIDER_DATA['enabled']
&& $OAUTH_PROVIDER_DATA['setup']} checked{/if} />
<label for="enable" id="enable"
class="custom-control-label">
{$OAUTH_PROVIDER_DATA['name']|ucfirst}
{if $OAUTH_PROVIDER_DATA['logo_url']}
<img src="{$OAUTH_PROVIDER_DATA['logo_url']}" alt="{$OAUTH_PROVIDER_DATA['name']|ucfirst} Logo" style="width: 16px; height: auto;">
{elseif $OAUTH_PROVIDER_DATA['icon']}
<i class="{$OAUTH_PROVIDER_DATA['icon']}"></i>
{/if}
{$REGISTER_LOGIN_WITH_OAUTH}
</label>
</div>

Expand Down
2 changes: 1 addition & 1 deletion modules/Core/classes/Integrations/GoogleIntegration.php
Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions modules/Core/pages/login.php
Expand Up @@ -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)
]);
Expand All @@ -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'),
]);
Expand Down
5 changes: 3 additions & 2 deletions modules/Core/pages/panel/integrations.php
Expand Up @@ -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'),
Expand All @@ -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
]);
Expand Down
10 changes: 7 additions & 3 deletions modules/Core/pages/register.php
Expand Up @@ -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)
]);
Expand All @@ -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,
]);

Expand Down
2 changes: 1 addition & 1 deletion modules/Discord Integration/classes/DiscordIntegration.php
Expand Up @@ -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();
}
Expand Down

0 comments on commit 78bb9f3

Please sign in to comment.