Skip to content

Commit

Permalink
VATSIM OAuth (nabeelio#1761)
Browse files Browse the repository at this point in the history
* Add socialiteproviders/vatsim

* Add vatsim_id field

* Fix OAuthTest

* Add vatsim to OAuthController

* Add vatsim login button

* Apply fixes from StyleCI

* Add vatsim profile buttons

* Add vatsim_id to sortable array in User

* Update composer.lock

* Update composer.lock

* Update composer.lock

* Update composer.lock

* Update composer.lock

* Update composer.lock

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 1, 2024
1 parent 523c78b commit d39eb77
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('vatsim_id')->default('')->after('discord_private_channel_id');
});
}

public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('vatsim_id');
});
}
};
5 changes: 5 additions & 0 deletions app/Http/Controllers/Auth/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function redirectToProvider(string $provider): RedirectResponse
abort(404);
}
return Socialite::driver('discord')->scopes(['identify'])->redirect();
case 'vatsim':
return Socialite::driver('vatsim')->scopes(['email'])->redirect();
default:
abort(404);
}
Expand All @@ -51,6 +53,9 @@ public function handleProviderCallback(string $provider, Request $request): View
case 'discord':
$providerUser = Socialite::driver('discord')->user();
break;
case 'vatsim':
$providerUser = Socialite::driver('vatsim')->user();
break;
default:
abort(404);
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function toArray($request)
'name_private' => $this->name_private,
'avatar' => $this->resolveAvatarUrl(),
'discord_id' => $this->discord_id,
'vatsim_id' => $this->vatsim_id,
'rank_id' => $this->rank_id,
'home_airport' => $this->home_airport_id,
'curr_airport' => $this->curr_airport_id,
Expand Down
3 changes: 3 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* @property Journal journal
* @property int rank_id
* @property string discord_id
* @property string vatsim_id
* @property int state
* @property string last_ip
* @property \Carbon\Carbon lastlogin_at
Expand Down Expand Up @@ -97,6 +98,7 @@ class User extends Authenticatable implements LaratrustUser, MustVerifyEmail
'rank_id',
'discord_id',
'discord_private_channel_id',
'vatsim_id',
'api_key',
'country',
'home_airport_id',
Expand Down Expand Up @@ -172,6 +174,7 @@ class User extends Authenticatable implements LaratrustUser, MustVerifyEmail
'transfer_time',
'created_at',
'state',
'vatsim_id',
];

/**
Expand Down
1 change: 1 addition & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class EventServiceProvider extends ServiceProvider
// For discord OAuth
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
\SocialiteProviders\Discord\DiscordExtendSocialite::class.'@handle',
\SocialiteProviders\Vatsim\VatsimExtendSocialite::class.'@handle',
],

];
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-sftp-v3": "^3.0",
"nikolaposa/version": "^4.2",
"spatie/laravel-activitylog": "^4.7"
"spatie/laravel-activitylog": "^4.7",
"socialiteproviders/vatsim": "^5.0"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.8.1",
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@
'allow_gif_avatars' => (bool) env('DISCORD_AVATAR_GIF', true),
'avatar_default_extension' => env('DISCORD_EXTENSION_DEFAULT', 'png'), // only pick from jpg, png, webp
],

'vatsim' => [
'enabled' => env('VATSIM_OAUTH_ENABLED', false),
'client_id' => env('VATSIM_CLIENT_ID'),
'client_secret' => env('VATSIM_CLIENT_SECRET'),
'redirect' => '/oauth/vatsim/callback',

// For local development only
'test' => env('VATSIM_TEST', false),
],
];
5 changes: 5 additions & 0 deletions resources/views/layouts/default/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class="form-control"
@lang('auth.loginwith', ['provider' => 'Discord'])
</a>
@endif
@if(config('services.vatsim.enabled'))
<a href="{{ route('oauth.redirect', ['provider' => 'vatsim']) }}" class="btn btn-round btn-lg btn-block" style="background-color:#29B473;">
@lang('auth.loginwith', ['provider' => 'VATSIM'])
</a>
@endif
<button class="btn btn-primary btn-round btn-lg btn-block">@lang('common.login')</button>
</div>
<div class="pull-left">
Expand Down
6 changes: 6 additions & 0 deletions resources/views/layouts/default/profile/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
<a href="{{ route('oauth.logout', ['provider' => 'discord']) }}" class="btn" style="background-color:#738ADB;">Unlink Discord Account</a>
@endif

@if(config('services.vatsim.enabled') && !$user->vatsim_id)
<a href="{{ route('oauth.redirect', ['provider' => 'vatsim']) }}" class="btn" style="background-color:#29B473;">Link VATSIM Account</a>
@elseif(config('services.vatsim.enabled'))
<a href="{{ route('oauth.logout', ['provider' => 'vatsim']) }}" class="btn" style="background-color:#29B473;">Unlink VATSIM Account</a>
@endif

<a href="{{ route('frontend.profile.regen_apikey') }}" class="btn btn-warning"
onclick="return confirm('Are you sure? This will reset your API key!')">@lang('profile.newapikey')</a>
&nbsp;
Expand Down
15 changes: 11 additions & 4 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class OAuthTest extends TestCase
{
/** @var array|string[] The drivers we want to test */
protected array $drivers = ['discord'];
protected array $drivers = ['discord', 'vatsim'];

protected function setUp(): void
{
Expand Down Expand Up @@ -109,6 +109,8 @@ public function testLinkAccountFromLogin(): void
$this->assertEquals('token', $tokens->token);
$this->assertEquals('refresh_token', $tokens->refresh_token);
$this->assertTrue($tokens->last_refreshed_at->diffInSeconds(now()) <= 2);

Auth::logout();
}
}

Expand All @@ -120,12 +122,15 @@ public function testLinkAccountFromLogin(): void
public function testLoginWithLinkedAccount(): void
{
$user = User::factory()->create([
'name' => 'OAuth user',
'email' => 'oauth.user@phpvms.net',
'discord_id' => 123456789,
'name' => 'OAuth user',
'email' => 'oauth.user@phpvms.net',
]);

foreach ($this->drivers as $driver) {
$user->update([
$driver.'_id' => 123456789,
]);

UserOAuthToken::create([
'user_id' => $user->id,
'provider' => $driver,
Expand All @@ -149,6 +154,8 @@ public function testLoginWithLinkedAccount(): void
$this->assertEquals('token', $tokens->token);
$this->assertEquals('refresh_token', $tokens->refresh_token);
$this->assertTrue($tokens->last_refreshed_at->diffInSeconds(now()) <= 2);

Auth::logout();
}
}

Expand Down

0 comments on commit d39eb77

Please sign in to comment.