Clean up constructors

This commit is contained in:
Vehikl 2025-05-08 16:12:20 -04:00
parent 305374192f
commit e349f3fc58
9 changed files with 26 additions and 53 deletions

View File

@ -10,10 +10,10 @@ class OAuthProvider
/** @var OAuthSchemaInterface[] */ /** @var OAuthSchemaInterface[] */
private array $providers = []; private array $providers = [];
/** @return OAuthSchemaInterface[] */ /** @return OAuthSchemaInterface[] | OAuthSchemaInterface */
public function get(): array public function get(?string $id = null): array|OAuthSchemaInterface
{ {
return $this->providers; return $id ? $this->providers[$id] : $this->providers;
} }
/** @return OAuthSchemaInterface[] */ /** @return OAuthSchemaInterface[] */
@ -32,8 +32,8 @@ class OAuthProvider
config()->set('services.' . $provider->getId(), array_merge($provider->getServiceConfig(), ['redirect' => '/auth/oauth/callback/' . $provider->getId()])); config()->set('services.' . $provider->getId(), array_merge($provider->getServiceConfig(), ['redirect' => '/auth/oauth/callback/' . $provider->getId()]));
if ($provider->getProviderClass()) { if ($provider->getSocialiteProvider()) {
Event::listen(fn (SocialiteWasCalled $event) => $event->extendSocialite($provider->getId(), $provider->getProviderClass())); Event::listen(fn (SocialiteWasCalled $event) => $event->extendSocialite($provider->getId(), $provider->getSocialiteProvider()));
} }
$this->providers[$provider->getId()] = $provider; $this->providers[$provider->getId()] = $provider;

View File

@ -10,7 +10,7 @@ interface OAuthSchemaInterface
public function getId(): string; public function getId(): string;
/** @return ?class-string */ /** @return ?class-string */
public function getProviderClass(): ?string; public function getSocialiteProvider(): ?string;
/** /**
* @return array<string, string|string[]|bool|null> * @return array<string, string|string[]|bool|null>

View File

@ -13,7 +13,7 @@ final class AuthentikSchema extends OAuthSchema
return 'authentik'; return 'authentik';
} }
public function getProviderClass(): string public function getSocialiteProvider(): string
{ {
return Provider::class; return Provider::class;
} }

View File

@ -4,21 +4,17 @@ namespace App\Extensions\OAuth\Schemas;
final class CommonSchema extends OAuthSchema final class CommonSchema extends OAuthSchema
{ {
public function __construct(private string $id, private ?string $providerClass, private ?string $icon, private ?string $hexColor) public function __construct(
{ private readonly string $id,
parent::__construct(); private readonly ?string $icon,
} private readonly ?string $hexColor
) {}
public function getId(): string public function getId(): string
{ {
return $this->id; return $this->id;
} }
public function getProviderClass(): ?string
{
return $this->providerClass;
}
public function getIcon(): ?string public function getIcon(): ?string
{ {
return $this->icon; return $this->icon;

View File

@ -17,7 +17,7 @@ final class DiscordSchema extends OAuthSchema
return 'discord'; return 'discord';
} }
public function getProviderClass(): string public function getSocialiteProvider(): string
{ {
return Provider::class; return Provider::class;
} }

View File

@ -6,37 +6,13 @@ use App\Extensions\OAuth\OAuthSchemaInterface;
use Filament\Forms\Components\Component; use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step; use Filament\Forms\Components\Wizard\Step;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use SocialiteProviders\Manager\SocialiteWasCalled;
abstract class OAuthSchema implements OAuthSchemaInterface abstract class OAuthSchema implements OAuthSchemaInterface
{ {
/**
* @var array<string, static>
*/
protected static array $providers = [];
/**
* @return self|static[]
*/
public static function get(?string $id = null): array|self
{
return $id ? static::$providers[$id] : static::$providers;
}
public function __construct()
{
if ($this->getProviderClass()) {
Event::listen(function (SocialiteWasCalled $event) {
$event->extendSocialite($this->getId(), $this->getProviderClass());
});
}
}
abstract public function getId(): string; abstract public function getId(): string;
public function getProviderClass(): ?string public function getSocialiteProvider(): ?string
{ {
return null; return null;
} }

View File

@ -16,7 +16,7 @@ final class SteamSchema extends OAuthSchema
return 'steam'; return 'steam';
} }
public function getProviderClass(): string public function getSocialiteProvider(): string
{ {
return Provider::class; return Provider::class;
} }

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers\Auth; namespace App\Http\Controllers\Auth;
use App\Extensions\OAuth\Schemas\OAuthSchema; use App\Extensions\OAuth\OAuthProvider;
use App\Filament\Pages\Auth\EditProfile; use App\Filament\Pages\Auth\EditProfile;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\User; use App\Models\User;
@ -18,7 +18,8 @@ class OAuthController extends Controller
{ {
public function __construct( public function __construct(
private readonly AuthManager $auth, private readonly AuthManager $auth,
private readonly UserUpdateService $updateService private readonly UserUpdateService $updateService,
private readonly OAuthProvider $oauthProvider
) {} ) {}
/** /**
@ -27,7 +28,7 @@ class OAuthController extends Controller
public function redirect(string $driver): RedirectResponse public function redirect(string $driver): RedirectResponse
{ {
// Driver is disabled - redirect to normal login // Driver is disabled - redirect to normal login
if (!OAuthSchema::get($driver)->isEnabled()) { if (!$this->oauthProvider->get($driver)->isEnabled()) {
return redirect()->route('auth.login'); return redirect()->route('auth.login');
} }
@ -40,7 +41,7 @@ class OAuthController extends Controller
public function callback(Request $request, string $driver): RedirectResponse public function callback(Request $request, string $driver): RedirectResponse
{ {
// Driver is disabled - redirect to normal login // Driver is disabled - redirect to normal login
if (!OAuthSchema::get($driver)->isEnabled()) { if (!$this->oauthProvider->get($driver)->isEnabled()) {
return redirect()->route('auth.login'); return redirect()->route('auth.login');
} }

View File

@ -18,14 +18,14 @@ class OAuthServiceProvider extends ServiceProvider
$this->app->singleton(OAuthProvider::class, function ($app) { $this->app->singleton(OAuthProvider::class, function ($app) {
$provider = new OAuthProvider(); $provider = new OAuthProvider();
// Default OAuth providers included with Socialite // Default OAuth providers included with Socialite
$provider->register(new CommonSchema('facebook', null, 'tabler-brand-facebook-f', '#1877f2')); $provider->register(new CommonSchema('facebook', 'tabler-brand-facebook-f', '#1877f2'));
$provider->register(new CommonSchema('x', null, 'tabler-brand-x-f', '#1da1f2')); $provider->register(new CommonSchema('x', 'tabler-brand-x-f', '#1da1f2'));
$provider->register(new CommonSchema('linkedin', null, 'tabler-brand-linkedin-f', '#0a66c2')); $provider->register(new CommonSchema('linkedin', 'tabler-brand-linkedin-f', '#0a66c2'));
$provider->register(new CommonSchema('google', null, 'tabler-brand-google-f', '#4285f4')); $provider->register(new CommonSchema('google', 'tabler-brand-google-f', '#4285f4'));
$provider->register(new GithubSchema()); $provider->register(new GithubSchema());
$provider->register(new GitlabSchema()); $provider->register(new GitlabSchema());
$provider->register(new CommonSchema('bitbucket', null, 'tabler-brand-bitbucket-f', '#205081')); $provider->register(new CommonSchema('bitbucket', 'tabler-brand-bitbucket-f', '#205081'));
$provider->register(new CommonSchema('slack', null, 'tabler-brand-slack', '#6ecadc')); $provider->register(new CommonSchema('slack', 'tabler-brand-slack', '#6ecadc'));
// Additional OAuth providers from socialiteproviders.com // Additional OAuth providers from socialiteproviders.com
$provider->register(new AuthentikSchema()); $provider->register(new AuthentikSchema());