Don't log duplicated OauthProviders during tests (#1015)

* Make sure OauthProviders we only log if not running tests

* Dependency inject
This commit is contained in:
MartinOscar 2025-02-24 19:37:41 +01:00 committed by GitHub
parent d53820bbdc
commit 1e7a901371
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 25 deletions

View File

@ -4,10 +4,13 @@ namespace App\Extensions\OAuth\Providers;
use Filament\Forms\Components\ColorPicker; use Filament\Forms\Components\ColorPicker;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Illuminate\Foundation\Application;
use SocialiteProviders\Authentik\Provider; use SocialiteProviders\Authentik\Provider;
final class AuthentikProvider extends OAuthProvider final class AuthentikProvider extends OAuthProvider
{ {
public function __construct(protected Application $app) {}
public function getId(): string public function getId(): string
{ {
return 'authentik'; return 'authentik';
@ -61,8 +64,8 @@ final class AuthentikProvider extends OAuthProvider
return env('OAUTH_AUTHENTIK_DISPLAY_COLOR') ?? '#fd4b2d'; return env('OAUTH_AUTHENTIK_DISPLAY_COLOR') ?? '#fd4b2d';
} }
public static function register(): self public static function register(Application $app): self
{ {
return new self(); return new self($app);
} }
} }

View File

@ -2,11 +2,13 @@
namespace App\Extensions\OAuth\Providers; namespace App\Extensions\OAuth\Providers;
use Illuminate\Foundation\Application;
final class CommonProvider extends OAuthProvider final class CommonProvider extends OAuthProvider
{ {
protected function __construct(private string $id, private ?string $providerClass, private ?string $icon, private ?string $hexColor) protected function __construct(protected Application $app, private string $id, private ?string $providerClass, private ?string $icon, private ?string $hexColor)
{ {
parent::__construct(); parent::__construct($app);
} }
public function getId(): string public function getId(): string
@ -29,8 +31,8 @@ final class CommonProvider extends OAuthProvider
return $this->hexColor; return $this->hexColor;
} }
public static function register(string $id, ?string $providerClass = null, ?string $icon = null, ?string $hexColor = null): static public static function register(Application $app, string $id, ?string $providerClass = null, ?string $icon = null, ?string $hexColor = null): static
{ {
return new self($id, $providerClass, $icon, $hexColor); return new self($app, $id, $providerClass, $icon, $hexColor);
} }
} }

View File

@ -5,6 +5,7 @@ namespace App\Extensions\OAuth\Providers;
use Filament\Forms\Components\Placeholder; use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step; use Filament\Forms\Components\Wizard\Step;
use Illuminate\Foundation\Application;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use SocialiteProviders\Discord\Provider; use SocialiteProviders\Discord\Provider;
@ -12,6 +13,8 @@ use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction;
final class DiscordProvider extends OAuthProvider final class DiscordProvider extends OAuthProvider
{ {
public function __construct(protected Application $app) {}
public function getId(): string public function getId(): string
{ {
return 'discord'; return 'discord';
@ -51,8 +54,8 @@ final class DiscordProvider extends OAuthProvider
return '#5865F2'; return '#5865F2';
} }
public static function register(): self public static function register(Application $app): self
{ {
return new self(); return new self($app);
} }
} }

View File

@ -5,12 +5,15 @@ namespace App\Extensions\OAuth\Providers;
use Filament\Forms\Components\Placeholder; use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step; use Filament\Forms\Components\Wizard\Step;
use Illuminate\Foundation\Application;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction; use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction;
final class GithubProvider extends OAuthProvider final class GithubProvider extends OAuthProvider
{ {
public function __construct(protected Application $app) {}
public function getId(): string public function getId(): string
{ {
return 'github'; return 'github';
@ -50,8 +53,8 @@ final class GithubProvider extends OAuthProvider
return '#4078c0'; return '#4078c0';
} }
public static function register(): self public static function register(Application $app): self
{ {
return new self(); return new self($app);
} }
} }

View File

@ -4,6 +4,7 @@ namespace App\Extensions\OAuth\Providers;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step; use Filament\Forms\Components\Wizard\Step;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use SocialiteProviders\Manager\SocialiteWasCalled; use SocialiteProviders\Manager\SocialiteWasCalled;
@ -17,10 +18,12 @@ abstract class OAuthProvider
return $id ? static::$providers[$id] : static::$providers; return $id ? static::$providers[$id] : static::$providers;
} }
protected function __construct() protected function __construct(protected Application $app)
{ {
if (array_key_exists($this->getId(), static::$providers)) { if (array_key_exists($this->getId(), static::$providers)) {
logger()->warning("Tried to create duplicate OAuth provider with id '{$this->getId()}'"); if (!$this->app->runningUnitTests()) {
logger()->warning("Tried to create duplicate OAuth provider with id '{$this->getId()}'");
}
return; return;
} }

View File

@ -5,11 +5,14 @@ namespace App\Extensions\OAuth\Providers;
use Filament\Forms\Components\Placeholder; use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard\Step; use Filament\Forms\Components\Wizard\Step;
use Illuminate\Foundation\Application;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use SocialiteProviders\Steam\Provider; use SocialiteProviders\Steam\Provider;
final class SteamProvider extends OAuthProvider final class SteamProvider extends OAuthProvider
{ {
public function __construct(protected Application $app) {}
public function getId(): string public function getId(): string
{ {
return 'steam'; return 'steam';
@ -67,8 +70,8 @@ final class SteamProvider extends OAuthProvider
return '#00adee'; return '#00adee';
} }
public static function register(): self public static function register(Application $app): self
{ {
return new self(); return new self($app);
} }
} }

View File

@ -86,19 +86,19 @@ class AppServiceProvider extends ServiceProvider
Scramble::registerApi('remote', ['api_path' => 'api/remote', 'info' => ['version' => '1.0']])->afterOpenApiGenerated($bearerTokens); Scramble::registerApi('remote', ['api_path' => 'api/remote', 'info' => ['version' => '1.0']])->afterOpenApiGenerated($bearerTokens);
// Default OAuth providers included with Socialite // Default OAuth providers included with Socialite
CommonProvider::register('facebook', null, 'tabler-brand-facebook-f', '#1877f2'); CommonProvider::register($app, 'facebook', null, 'tabler-brand-facebook-f', '#1877f2');
CommonProvider::register('x', null, 'tabler-brand-x-f', '#1da1f2'); CommonProvider::register($app, 'x', null, 'tabler-brand-x-f', '#1da1f2');
CommonProvider::register('linkedin', null, 'tabler-brand-linkedin-f', '#0a66c2'); CommonProvider::register($app, 'linkedin', null, 'tabler-brand-linkedin-f', '#0a66c2');
CommonProvider::register('google', null, 'tabler-brand-google-f', '#4285f4'); CommonProvider::register($app, 'google', null, 'tabler-brand-google-f', '#4285f4');
GithubProvider::register(); GithubProvider::register($app);
CommonProvider::register('gitlab', null, 'tabler-brand-gitlab', '#fca326'); CommonProvider::register($app, 'gitlab', null, 'tabler-brand-gitlab', '#fca326');
CommonProvider::register('bitbucket', null, 'tabler-brand-bitbucket-f', '#205081'); CommonProvider::register($app, 'bitbucket', null, 'tabler-brand-bitbucket-f', '#205081');
CommonProvider::register('slack', null, 'tabler-brand-slack', '#6ecadc'); CommonProvider::register($app, 'slack', null, 'tabler-brand-slack', '#6ecadc');
// Additional OAuth providers from socialiteproviders.com // Additional OAuth providers from socialiteproviders.com
AuthentikProvider::register(); AuthentikProvider::register($app);
DiscordProvider::register(); DiscordProvider::register($app);
SteamProvider::register(); SteamProvider::register($app);
FilamentColor::register([ FilamentColor::register([
'danger' => Color::Red, 'danger' => Color::Red,