Discord OAuth

Creates the base structure for Discord OAuth.
This commit is contained in:
Refringe 2024-09-26 16:55:44 -04:00
parent a94a1418ef
commit 88aa9b1ad8
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
16 changed files with 743 additions and 11 deletions

View File

@ -92,3 +92,8 @@ GITEA_TOKEN=
# API key for Scribe documentation. # API key for Scribe documentation.
SCRIBE_AUTH_KEY= SCRIBE_AUTH_KEY=
# Discord OAuth Credentials
DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=
DISCORD_REDIRECT_URI=${APP_URL}/login/discord/callback

View File

@ -45,3 +45,8 @@ MAIL_FROM_NAME="${APP_NAME}"
# API key for Scribe documentation. # API key for Scribe documentation.
SCRIBE_AUTH_KEY= SCRIBE_AUTH_KEY=
# Discord OAuth Credentials
DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=
DISCORD_REDIRECT_URI=${APP_URL}/login/discord/callback

View File

@ -0,0 +1,93 @@
<?php
namespace App\Http\Controllers;
use App\Models\OAuthConnection;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Socialite\Contracts\User as ProviderUser;
use Laravel\Socialite\Facades\Socialite;
class SocialiteController extends Controller
{
/**
* The providers that are supported.
*/
protected array $providers = ['discord'];
/**
* Redirect the user to the provider's authentication page.
*/
public function redirect(string $provider): RedirectResponse
{
if (! in_array($provider, $this->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;
});
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OAuthConnection extends Model
{
use HasFactory;
protected $table = 'oauth_connections';
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str; 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 protected function about(): Attribute
{ {

View File

@ -13,9 +13,12 @@ use App\Observers\ModVersionObserver;
use App\Observers\SptVersionObserver; use App\Observers\SptVersionObserver;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Number; use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use SocialiteProviders\Discord\Provider;
use SocialiteProviders\Manager\SocialiteWasCalled;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -35,8 +38,10 @@ class AppServiceProvider extends ServiceProvider
// Allow mass assignment for all models. Be careful! // Allow mass assignment for all models. Be careful!
Model::unguard(); Model::unguard();
// Register model observers.
$this->registerObservers(); $this->registerObservers();
// Register custom macros.
$this->registerNumberMacros(); $this->registerNumberMacros();
$this->registerCarbonMacros(); $this->registerCarbonMacros();
@ -44,6 +49,11 @@ class AppServiceProvider extends ServiceProvider
Gate::define('viewPulse', function (User $user) { Gate::define('viewPulse', function (User $user) {
return $user->isAdmin(); return $user->isAdmin();
}); });
// Register the Discord socialite provider.
Event::listen(function (SocialiteWasCalled $event) {
$event->extendSocialite('discord', Provider::class);
});
} }
/** /**

View File

@ -6,4 +6,5 @@ return [
App\Providers\HorizonServiceProvider::class, App\Providers\HorizonServiceProvider::class,
App\Providers\JetstreamServiceProvider::class, App\Providers\JetstreamServiceProvider::class,
App\Providers\Filament\AdminPanelProvider::class, App\Providers\Filament\AdminPanelProvider::class,
\SocialiteProviders\Manager\ServiceProvider::class,
]; ];

View File

@ -19,12 +19,14 @@
"laravel/pulse": "^1.2", "laravel/pulse": "^1.2",
"laravel/sanctum": "^4.0", "laravel/sanctum": "^4.0",
"laravel/scout": "^10.10", "laravel/scout": "^10.10",
"laravel/socialite": "^5.16",
"laravel/tinker": "^2.9", "laravel/tinker": "^2.9",
"league/flysystem-aws-s3-v3": "^3.28", "league/flysystem-aws-s3-v3": "^3.28",
"league/html-to-markdown": "^5.1", "league/html-to-markdown": "^5.1",
"livewire/livewire": "^3.5", "livewire/livewire": "^3.5",
"mchev/banhammer": "^2.3", "mchev/banhammer": "^2.3",
"meilisearch/meilisearch-php": "^1.8", "meilisearch/meilisearch-php": "^1.8",
"socialiteproviders/discord": "^4.2",
"stevebauman/purify": "^6.2" "stevebauman/purify": "^6.2"
}, },
"require-dev": { "require-dev": {

497
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "645a73e7a66339c2396753a2f35c5eea", "content-hash": "2e603ffae8f6f8a4c834c9bff55cc30f",
"packages": [ "packages": [
{ {
"name": "anourvalar/eloquent-serialize", "name": "anourvalar/eloquent-serialize",
@ -1867,6 +1867,69 @@
}, },
"time": "2024-09-23T14:10:16+00:00" "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", "name": "fruitcake/php-cors",
"version": "v1.3.0", "version": "v1.3.0",
@ -3473,6 +3536,78 @@
}, },
"time": "2024-08-02T07:48:17+00:00" "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", "name": "laravel/tinker",
"version": "v2.9.0", "version": "v2.9.0",
@ -4147,6 +4282,82 @@
], ],
"time": "2024-09-21T08:32:55+00:00" "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", "name": "league/uri",
"version": "7.4.1", "version": "7.4.1",
@ -5390,6 +5601,56 @@
}, },
"time": "2024-05-08T12:36:18+00:00" "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", "name": "php-http/discovery",
"version": "1.19.4", "version": "1.19.4",
@ -5544,6 +5805,116 @@
], ],
"time": "2024-07-20T21:41:07+00:00" "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", "name": "pragmarx/google2fa",
"version": "v8.0.3", "version": "v8.0.3",
@ -6439,6 +6810,130 @@
], ],
"time": "2024-02-26T18:08:49+00:00" "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", "name": "spatie/color",
"version": "1.6.0", "version": "1.6.0",

View File

@ -40,4 +40,12 @@ return [
'token' => env('GITEA_TOKEN', ''), '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'),
],
]; ];

View File

@ -0,0 +1,21 @@
<?php
namespace Database\Factories;
use App\Models\OAuthConnection;
use Illuminate\Database\Eloquent\Factories\Factory;
class OAuthConnectionFactory extends Factory
{
protected $model = OAuthConnection::class;
/**
* Define the model's default state.
*/
public function definition(): array
{
return [
//
];
}
}

View File

@ -14,14 +14,12 @@ return new class extends Migration
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->id(); $table->id();
$table->bigInteger('hub_id') $table->bigInteger('hub_id')->nullable()->default(null)->unique();
->nullable() $table->unsignedBigInteger('discord_id')->nullable()->default(null)->unique();
->default(null)
->unique();
$table->string('name'); $table->string('name');
$table->string('email')->unique(); $table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable(); $table->timestamp('email_verified_at')->nullable();
$table->string('password'); $table->string('password')->nullable();
$table->longText('about')->nullable()->default(null); $table->longText('about')->nullable()->default(null);
$table->foreignIdFor(UserRole::class) $table->foreignIdFor(UserRole::class)
->nullable() ->nullable()

View File

@ -0,0 +1,38 @@
<?php
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_connections', function (Blueprint $table) {
$table->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');
}
};

View File

@ -44,5 +44,10 @@
</x-button> </x-button>
</div> </div>
</form> </form>
<ul>
<li><a href="{{ route('login.socialite', ['provider' => 'discord']) }}">{{ __('Login with Discord') }}</a></li>
</ul>
</x-authentication-card> </x-authentication-card>
</x-guest-layout> </x-guest-layout>

View File

@ -2,11 +2,17 @@
use App\Http\Controllers\ModController; use App\Http\Controllers\ModController;
use App\Http\Controllers\ModVersionController; use App\Http\Controllers\ModVersionController;
use App\Http\Controllers\SocialiteController;
use App\Http\Controllers\UserController; use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
Route::middleware(['auth.banned'])->group(function () { 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 () { Route::get('/', function () {
return view('home'); return view('home');
})->name('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'); Route::get('/mod/{mod}/{slug}', 'show')->where(['mod' => '[0-9]+'])->name('mod.show');
}); });
// Download Link
Route::controller(ModVersionController::class)->group(function () { Route::controller(ModVersionController::class)->group(function () {
Route::get('/mod/download/{mod}/{slug}/{version}', 'show') Route::get('/mod/download/{mod}/{slug}/{version}', 'show')
->where([ ->where(['mod' => '[0-9]+', 'slug' => '[a-z0-9-]+'])
'mod' => '[0-9]+',
'slug' => '[a-z0-9-]+',
])
->name('mod.version.download'); ->name('mod.version.download');
}); });

View File

@ -30,3 +30,22 @@ test('users cannot authenticate with invalid password', function () {
$this->assertGuest(); $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');
});