From 88aa9b1ad84841d05740a468ff2d96cc4a2d5e6d Mon Sep 17 00:00:00 2001 From: Refringe Date: Thu, 26 Sep 2024 16:55:44 -0400 Subject: [PATCH] Discord OAuth Creates the base structure for Discord OAuth. --- .env.full | 5 + .env.light | 5 + app/Http/Controllers/SocialiteController.php | 93 ++++ app/Models/OAuthConnection.php | 19 + app/Models/User.php | 11 +- app/Providers/AppServiceProvider.php | 10 + bootstrap/providers.php | 1 + composer.json | 2 + composer.lock | 497 +++++++++++++++++- config/services.php | 8 + database/factories/OAuthConnectionFactory.php | 21 + .../0001_01_01_000000_create_users_table.php | 8 +- ...26_164844_create_oauth_providers_table.php | 38 ++ resources/views/auth/login.blade.php | 5 + routes/web.php | 12 +- tests/Feature/User/AuthenticationTest.php | 19 + 16 files changed, 743 insertions(+), 11 deletions(-) create mode 100644 app/Http/Controllers/SocialiteController.php create mode 100644 app/Models/OAuthConnection.php create mode 100644 database/factories/OAuthConnectionFactory.php create mode 100644 database/migrations/2024_09_26_164844_create_oauth_providers_table.php diff --git a/.env.full b/.env.full index 5696afa..48ef9ae 100644 --- a/.env.full +++ b/.env.full @@ -92,3 +92,8 @@ GITEA_TOKEN= # API key for Scribe documentation. SCRIBE_AUTH_KEY= + +# Discord OAuth Credentials +DISCORD_CLIENT_ID= +DISCORD_CLIENT_SECRET= +DISCORD_REDIRECT_URI=${APP_URL}/login/discord/callback diff --git a/.env.light b/.env.light index 947270c..3b84afc 100644 --- a/.env.light +++ b/.env.light @@ -45,3 +45,8 @@ MAIL_FROM_NAME="${APP_NAME}" # API key for Scribe documentation. SCRIBE_AUTH_KEY= + +# Discord OAuth Credentials +DISCORD_CLIENT_ID= +DISCORD_CLIENT_SECRET= +DISCORD_REDIRECT_URI=${APP_URL}/login/discord/callback diff --git a/app/Http/Controllers/SocialiteController.php b/app/Http/Controllers/SocialiteController.php new file mode 100644 index 0000000..3f5c5b5 --- /dev/null +++ b/app/Http/Controllers/SocialiteController.php @@ -0,0 +1,93 @@ +providers)) { + return redirect()->route('login')->withErrors(__('Unsupported OAuth provider.')); + } + + return Socialite::driver('discord') + ->scopes([ + 'identify', + 'email', + ]) + ->redirect(); + } + + /** + * Obtain the user information from the provider. + */ + public function callback(string $provider): RedirectResponse + { + if (! in_array($provider, $this->providers)) { + return redirect()->route('login')->withErrors(__('Unsupported OAuth provider.')); + } + + try { + $providerUser = Socialite::driver($provider)->user(); + } catch (\Exception $e) { + return redirect()->route('login')->withErrors('Unable to login using '.$provider.'. Please try again.'); + } + + $user = $this->findOrCreateUser($provider, $providerUser); + + Auth::login($user, remember: true); + + return redirect()->route('dashboard'); + } + + protected function findOrCreateUser(string $provider, ProviderUser $providerUser): User + { + $oauthConnection = OAuthConnection::whereProvider($provider) + ->whereProviderId($providerUser->getId()) + ->first(); + + if ($oauthConnection) { + $oauthConnection->update([ + 'token' => $providerUser->token, + 'refresh_token' => $providerUser->refreshToken ?? null, + ]); + + return $oauthConnection->user; + } + + // The user has not connected their account with this OAuth provider before, so a new connection needs to be + // established. Check if the user has an account with the same email address that's passed in from the provider. + // If one exists, connect that account. Otherwise, create a new one. + + return DB::transaction(function () use ($providerUser, $provider) { + $user = User::firstOrCreate(['email' => $providerUser->getEmail()], [ + 'name' => $providerUser->getName() ?? $providerUser->getNickname(), + 'password' => null, + ]); + $user->oAuthConnections()->create([ + 'provider' => $provider, + 'provider_id' => $providerUser->getId(), + 'token' => $providerUser->token, + 'refresh_token' => $providerUser->refreshToken ?? null, + ]); + + return $user; + }); + } +} diff --git a/app/Models/OAuthConnection.php b/app/Models/OAuthConnection.php new file mode 100644 index 0000000..e406fb4 --- /dev/null +++ b/app/Models/OAuthConnection.php @@ -0,0 +1,19 @@ +belongsTo(User::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 8acc7db..21715d9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Str; @@ -211,7 +212,15 @@ class User extends Authenticatable implements MustVerifyEmail } /** - * Handle the about default value if empty. Thanks MySQL! + * The relationship between a user and their OAuth providers. + */ + public function oAuthConnections(): HasMany + { + return $this->hasMany(OAuthConnection::class); + } + + /** + * Handle the about default value if empty. Thanks, MySQL! */ protected function about(): Attribute { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 0b307fd..83058d0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -13,9 +13,12 @@ use App\Observers\ModVersionObserver; use App\Observers\SptVersionObserver; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Number; use Illuminate\Support\ServiceProvider; +use SocialiteProviders\Discord\Provider; +use SocialiteProviders\Manager\SocialiteWasCalled; class AppServiceProvider extends ServiceProvider { @@ -35,8 +38,10 @@ class AppServiceProvider extends ServiceProvider // Allow mass assignment for all models. Be careful! Model::unguard(); + // Register model observers. $this->registerObservers(); + // Register custom macros. $this->registerNumberMacros(); $this->registerCarbonMacros(); @@ -44,6 +49,11 @@ class AppServiceProvider extends ServiceProvider Gate::define('viewPulse', function (User $user) { return $user->isAdmin(); }); + + // Register the Discord socialite provider. + Event::listen(function (SocialiteWasCalled $event) { + $event->extendSocialite('discord', Provider::class); + }); } /** diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 95d193c..843676d 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -6,4 +6,5 @@ return [ App\Providers\HorizonServiceProvider::class, App\Providers\JetstreamServiceProvider::class, App\Providers\Filament\AdminPanelProvider::class, + \SocialiteProviders\Manager\ServiceProvider::class, ]; diff --git a/composer.json b/composer.json index 9de99a3..e30016e 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,14 @@ "laravel/pulse": "^1.2", "laravel/sanctum": "^4.0", "laravel/scout": "^10.10", + "laravel/socialite": "^5.16", "laravel/tinker": "^2.9", "league/flysystem-aws-s3-v3": "^3.28", "league/html-to-markdown": "^5.1", "livewire/livewire": "^3.5", "mchev/banhammer": "^2.3", "meilisearch/meilisearch-php": "^1.8", + "socialiteproviders/discord": "^4.2", "stevebauman/purify": "^6.2" }, "require-dev": { diff --git a/composer.lock b/composer.lock index ab20e13..38b0b5e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "645a73e7a66339c2396753a2f35c5eea", + "content-hash": "2e603ffae8f6f8a4c834c9bff55cc30f", "packages": [ { "name": "anourvalar/eloquent-serialize", @@ -1867,6 +1867,69 @@ }, "time": "2024-09-23T14:10:16+00:00" }, + { + "name": "firebase/php-jwt", + "version": "v6.10.1", + "source": { + "type": "git", + "url": "https://github.com/firebase/php-jwt.git", + "reference": "500501c2ce893c824c801da135d02661199f60c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/firebase/php-jwt/zipball/500501c2ce893c824c801da135d02661199f60c5", + "reference": "500501c2ce893c824c801da135d02661199f60c5", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^7.4", + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^9.5", + "psr/cache": "^2.0||^3.0", + "psr/http-client": "^1.0", + "psr/http-factory": "^1.0" + }, + "suggest": { + "ext-sodium": "Support EdDSA (Ed25519) signatures", + "paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present" + }, + "type": "library", + "autoload": { + "psr-4": { + "Firebase\\JWT\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Neuman Vong", + "email": "neuman+pear@twilio.com", + "role": "Developer" + }, + { + "name": "Anant Narayanan", + "email": "anant@php.net", + "role": "Developer" + } + ], + "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", + "homepage": "https://github.com/firebase/php-jwt", + "keywords": [ + "jwt", + "php" + ], + "support": { + "issues": "https://github.com/firebase/php-jwt/issues", + "source": "https://github.com/firebase/php-jwt/tree/v6.10.1" + }, + "time": "2024-05-18T18:05:11+00:00" + }, { "name": "fruitcake/php-cors", "version": "v1.3.0", @@ -3473,6 +3536,78 @@ }, "time": "2024-08-02T07:48:17+00:00" }, + { + "name": "laravel/socialite", + "version": "v5.16.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/socialite.git", + "reference": "40a2dc98c53d9dc6d55eadb0d490d3d72b73f1bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/socialite/zipball/40a2dc98c53d9dc6d55eadb0d490d3d72b73f1bf", + "reference": "40a2dc98c53d9dc6d55eadb0d490d3d72b73f1bf", + "shasum": "" + }, + "require": { + "ext-json": "*", + "firebase/php-jwt": "^6.4", + "guzzlehttp/guzzle": "^6.0|^7.0", + "illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "league/oauth1-client": "^1.10.1", + "php": "^7.2|^8.0", + "phpseclib/phpseclib": "^3.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0|^9.0", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^8.0|^9.3|^10.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Socialite\\SocialiteServiceProvider" + ], + "aliases": { + "Socialite": "Laravel\\Socialite\\Facades\\Socialite" + } + } + }, + "autoload": { + "psr-4": { + "Laravel\\Socialite\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel wrapper around OAuth 1 & OAuth 2 libraries.", + "homepage": "https://laravel.com", + "keywords": [ + "laravel", + "oauth" + ], + "support": { + "issues": "https://github.com/laravel/socialite/issues", + "source": "https://github.com/laravel/socialite" + }, + "time": "2024-09-03T09:46:57+00:00" + }, { "name": "laravel/tinker", "version": "v2.9.0", @@ -4147,6 +4282,82 @@ ], "time": "2024-09-21T08:32:55+00:00" }, + { + "name": "league/oauth1-client", + "version": "v1.10.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/oauth1-client.git", + "reference": "d6365b901b5c287dd41f143033315e2f777e1167" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/oauth1-client/zipball/d6365b901b5c287dd41f143033315e2f777e1167", + "reference": "d6365b901b5c287dd41f143033315e2f777e1167", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-openssl": "*", + "guzzlehttp/guzzle": "^6.0|^7.0", + "guzzlehttp/psr7": "^1.7|^2.0", + "php": ">=7.1||>=8.0" + }, + "require-dev": { + "ext-simplexml": "*", + "friendsofphp/php-cs-fixer": "^2.17", + "mockery/mockery": "^1.3.3", + "phpstan/phpstan": "^0.12.42", + "phpunit/phpunit": "^7.5||9.5" + }, + "suggest": { + "ext-simplexml": "For decoding XML-based responses." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev", + "dev-develop": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "League\\OAuth1\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ben Corlett", + "email": "bencorlett@me.com", + "homepage": "http://www.webcomm.com.au", + "role": "Developer" + } + ], + "description": "OAuth 1.0 Client Library", + "keywords": [ + "Authentication", + "SSO", + "authorization", + "bitbucket", + "identity", + "idp", + "oauth", + "oauth1", + "single sign on", + "trello", + "tumblr", + "twitter" + ], + "support": { + "issues": "https://github.com/thephpleague/oauth1-client/issues", + "source": "https://github.com/thephpleague/oauth1-client/tree/v1.10.1" + }, + "time": "2022-04-15T14:02:14+00:00" + }, { "name": "league/uri", "version": "7.4.1", @@ -5390,6 +5601,56 @@ }, "time": "2024-05-08T12:36:18+00:00" }, + { + "name": "paragonie/random_compat", + "version": "v9.99.100", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a", + "shasum": "" + }, + "require": { + "php": ">= 7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/random_compat/issues", + "source": "https://github.com/paragonie/random_compat" + }, + "time": "2020-10-15T08:29:30+00:00" + }, { "name": "php-http/discovery", "version": "1.19.4", @@ -5544,6 +5805,116 @@ ], "time": "2024-07-20T21:41:07+00:00" }, + { + "name": "phpseclib/phpseclib", + "version": "3.0.42", + "source": { + "type": "git", + "url": "https://github.com/phpseclib/phpseclib.git", + "reference": "db92f1b1987b12b13f248fe76c3a52cadb67bb98" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/db92f1b1987b12b13f248fe76c3a52cadb67bb98", + "reference": "db92f1b1987b12b13f248fe76c3a52cadb67bb98", + "shasum": "" + }, + "require": { + "paragonie/constant_time_encoding": "^1|^2|^3", + "paragonie/random_compat": "^1.4|^2.0|^9.99.99", + "php": ">=5.6.1" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "suggest": { + "ext-dom": "Install the DOM extension to load XML formatted public keys.", + "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.", + "ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.", + "ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.", + "ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations." + }, + "type": "library", + "autoload": { + "files": [ + "phpseclib/bootstrap.php" + ], + "psr-4": { + "phpseclib3\\": "phpseclib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jim Wigginton", + "email": "terrafrost@php.net", + "role": "Lead Developer" + }, + { + "name": "Patrick Monnerat", + "email": "pm@datasphere.ch", + "role": "Developer" + }, + { + "name": "Andreas Fischer", + "email": "bantu@phpbb.com", + "role": "Developer" + }, + { + "name": "Hans-Jürgen Petrich", + "email": "petrich@tronic-media.com", + "role": "Developer" + }, + { + "name": "Graham Campbell", + "email": "graham@alt-three.com", + "role": "Developer" + } + ], + "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", + "homepage": "http://phpseclib.sourceforge.net", + "keywords": [ + "BigInteger", + "aes", + "asn.1", + "asn1", + "blowfish", + "crypto", + "cryptography", + "encryption", + "rsa", + "security", + "sftp", + "signature", + "signing", + "ssh", + "twofish", + "x.509", + "x509" + ], + "support": { + "issues": "https://github.com/phpseclib/phpseclib/issues", + "source": "https://github.com/phpseclib/phpseclib/tree/3.0.42" + }, + "funding": [ + { + "url": "https://github.com/terrafrost", + "type": "github" + }, + { + "url": "https://www.patreon.com/phpseclib", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpseclib/phpseclib", + "type": "tidelift" + } + ], + "time": "2024-09-16T03:06:04+00:00" + }, { "name": "pragmarx/google2fa", "version": "v8.0.3", @@ -6439,6 +6810,130 @@ ], "time": "2024-02-26T18:08:49+00:00" }, + { + "name": "socialiteproviders/discord", + "version": "4.2.0", + "source": { + "type": "git", + "url": "https://github.com/SocialiteProviders/Discord.git", + "reference": "c71c379acfdca5ba4aa65a3db5ae5222852a919c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/SocialiteProviders/Discord/zipball/c71c379acfdca5ba4aa65a3db5ae5222852a919c", + "reference": "c71c379acfdca5ba4aa65a3db5ae5222852a919c", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.4 || ^8.0", + "socialiteproviders/manager": "~4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "SocialiteProviders\\Discord\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christopher Eklund", + "email": "eklundchristopher@gmail.com" + } + ], + "description": "Discord OAuth2 Provider for Laravel Socialite", + "keywords": [ + "discord", + "laravel", + "oauth", + "provider", + "socialite" + ], + "support": { + "docs": "https://socialiteproviders.com/discord", + "issues": "https://github.com/socialiteproviders/providers/issues", + "source": "https://github.com/socialiteproviders/providers" + }, + "time": "2023-07-24T23:28:47+00:00" + }, + { + "name": "socialiteproviders/manager", + "version": "v4.6.0", + "source": { + "type": "git", + "url": "https://github.com/SocialiteProviders/Manager.git", + "reference": "dea5190981c31b89e52259da9ab1ca4e2b258b21" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/SocialiteProviders/Manager/zipball/dea5190981c31b89e52259da9ab1ca4e2b258b21", + "reference": "dea5190981c31b89e52259da9ab1ca4e2b258b21", + "shasum": "" + }, + "require": { + "illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "laravel/socialite": "^5.5", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.2", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "SocialiteProviders\\Manager\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\Manager\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Andy Wendt", + "email": "andy@awendt.com" + }, + { + "name": "Anton Komarev", + "email": "a.komarev@cybercog.su" + }, + { + "name": "Miguel Piedrafita", + "email": "soy@miguelpiedrafita.com" + }, + { + "name": "atymic", + "email": "atymicq@gmail.com", + "homepage": "https://atymic.dev" + } + ], + "description": "Easily add new or override built-in providers in Laravel Socialite.", + "homepage": "https://socialiteproviders.com", + "keywords": [ + "laravel", + "manager", + "oauth", + "providers", + "socialite" + ], + "support": { + "issues": "https://github.com/socialiteproviders/manager/issues", + "source": "https://github.com/socialiteproviders/manager" + }, + "time": "2024-05-04T07:57:39+00:00" + }, { "name": "spatie/color", "version": "1.6.0", diff --git a/config/services.php b/config/services.php index beca5d4..89df54e 100644 --- a/config/services.php +++ b/config/services.php @@ -40,4 +40,12 @@ return [ 'token' => env('GITEA_TOKEN', ''), ], + 'discord' => [ + 'client_id' => env('DISCORD_CLIENT_ID'), + 'client_secret' => env('DISCORD_CLIENT_SECRET'), + 'redirect' => env('DISCORD_REDIRECT_URI'), + 'allow_gif_avatars' => (bool) env('DISCORD_AVATAR_GIF', true), + 'avatar_default_extension' => env('DISCORD_EXTENSION_DEFAULT', 'png'), + ], + ]; diff --git a/database/factories/OAuthConnectionFactory.php b/database/factories/OAuthConnectionFactory.php new file mode 100644 index 0000000..7338ea3 --- /dev/null +++ b/database/factories/OAuthConnectionFactory.php @@ -0,0 +1,21 @@ +id(); - $table->bigInteger('hub_id') - ->nullable() - ->default(null) - ->unique(); + $table->bigInteger('hub_id')->nullable()->default(null)->unique(); + $table->unsignedBigInteger('discord_id')->nullable()->default(null)->unique(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); + $table->string('password')->nullable(); $table->longText('about')->nullable()->default(null); $table->foreignIdFor(UserRole::class) ->nullable() diff --git a/database/migrations/2024_09_26_164844_create_oauth_providers_table.php b/database/migrations/2024_09_26_164844_create_oauth_providers_table.php new file mode 100644 index 0000000..9184073 --- /dev/null +++ b/database/migrations/2024_09_26_164844_create_oauth_providers_table.php @@ -0,0 +1,38 @@ +id(); + $table->foreignIdFor(User::class) + ->constrained('users') + ->cascadeOnDelete() + ->cascadeOnUpdate(); + $table->string('provider'); + $table->string('provider_id'); + $table->string('token')->default(''); + $table->string('refresh_token')->default(''); + $table->timestamps(); + + $table->unique(['provider', 'provider_id']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('oauth_providers'); + } +}; diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index e435017..c244022 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -44,5 +44,10 @@ + + + diff --git a/routes/web.php b/routes/web.php index d718b8b..a47e9eb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,11 +2,17 @@ use App\Http\Controllers\ModController; use App\Http\Controllers\ModVersionController; +use App\Http\Controllers\SocialiteController; use App\Http\Controllers\UserController; use Illuminate\Support\Facades\Route; Route::middleware(['auth.banned'])->group(function () { + Route::controller(SocialiteController::class)->group(function () { + Route::get('/login/{provider}/redirect', 'redirect')->name('login.socialite'); + Route::get('/login/{provider}/callback', 'callback'); + }); + Route::get('/', function () { return view('home'); })->name('home'); @@ -16,12 +22,10 @@ Route::middleware(['auth.banned'])->group(function () { Route::get('/mod/{mod}/{slug}', 'show')->where(['mod' => '[0-9]+'])->name('mod.show'); }); + // Download Link Route::controller(ModVersionController::class)->group(function () { Route::get('/mod/download/{mod}/{slug}/{version}', 'show') - ->where([ - 'mod' => '[0-9]+', - 'slug' => '[a-z0-9-]+', - ]) + ->where(['mod' => '[0-9]+', 'slug' => '[a-z0-9-]+']) ->name('mod.version.download'); }); diff --git a/tests/Feature/User/AuthenticationTest.php b/tests/Feature/User/AuthenticationTest.php index 9244fff..9f705eb 100755 --- a/tests/Feature/User/AuthenticationTest.php +++ b/tests/Feature/User/AuthenticationTest.php @@ -30,3 +30,22 @@ test('users cannot authenticate with invalid password', function () { $this->assertGuest(); }); + +test('users can authenticate using Discord', function () { + $response = $this->get('/auth/discord/redirect'); + + $response->assertStatus(302); + $response->assertSessionHas('url.intended', route('dashboard', absolute: false)); +}); + +test('user can not authenticate using a null password', function () { + $user = User::factory()->create(); + + $response = $this->post('/login', [ + 'email' => $user->email, + 'password' => null, + ]); + + $this->assertGuest(); + $response->assertSessionHasErrors('password'); +});