Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace App\Models;

use App\Enums\AirportView;
use App\Services\OAuth\VatsimProvider;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use League\OAuth2\Client\Token\AccessToken;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

Expand All @@ -22,9 +20,6 @@
* @property AirportView $airport_view
* @property bool $use_monospace_font
* @property string|null $remember_token
* @property string|null $access_token
* @property string|null $refresh_token
* @property int|null $token_expires
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Spatie\Activitylog\Models\Activity> $activities
Expand All @@ -39,17 +34,14 @@
* @method static \Illuminate\Database\Eloquent\Builder<static>|User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|User newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|User query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereAccessToken($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereAirportView($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereIsAdmin($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereNameFirst($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereNameLast($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereRefreshToken($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereRememberToken($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereTokenExpires($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|User whereUseMonospaceFont($value)
* @mixin \Eloquent
Expand All @@ -73,9 +65,6 @@ class User extends Authenticatable
*/
protected $hidden = [
'remember_token',
'access_token',
'refresh_token',
'token_expires',
];

public function getActivitylogOptions(): LogOptions
Expand Down Expand Up @@ -109,36 +98,6 @@ protected function pic(): Attribute
);
}

/**
* Returns a valid access token, refreshing it via the OAuth provider if it has expired.
* Persists updated token fields to the database when a refresh occurs.
*/
public function refreshTokenIfExpired(): ?AccessToken
{
if ($this->access_token === null) {
return null;
}

$token = new AccessToken([
'access_token' => $this->access_token,
'refresh_token' => $this->refresh_token,
'expires' => $this->token_expires,
]);

if ($token->hasExpired()) {
$refreshedToken = resolve(VatsimProvider::class)->updateToken($token);
$token = $refreshedToken instanceof AccessToken ? $refreshedToken : null;

$this->update([
'access_token' => $token?->getToken(),
'refresh_token' => $token?->getRefreshToken(),
'token_expires' => $token?->getExpires(),
]);
}

return $token;
}

/**
* The attributes that should be cast to native types.
*
Expand Down
17 changes: 2 additions & 15 deletions app/Services/Auth/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public function authenticateFromOAuth(Request $request): ?array
return null;
}

$user = $this->upsertUser($data, $accessToken);
$user = $this->upsertUser($data);

return ['user' => $user, 'data' => $data];
}

/**
* Create or update the user record and log them in.
*/
protected function upsertUser(array $data, $token): User
protected function upsertUser(array $data): User
{
$account = User::updateOrCreate(
['id' => $data['cid']],
Expand All @@ -61,19 +61,6 @@ protected function upsertUser(array $data, $token): User
]
);

if ($token->getToken() !== null) {
$account->access_token = $token->getToken();
}

if ($token->getRefreshToken() !== null) {
$account->refresh_token = $token->getRefreshToken();
}

if ($token->getExpires() !== null) {
$account->token_expires = $token->getExpires();
}

$account->save();
auth()->loginUsingId($data['cid'], true);
activity()->log('Login');

Expand Down
15 changes: 2 additions & 13 deletions app/Services/OAuth/VatsimProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Services\OAuth;

use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessTokenInterface;

class VatsimProvider extends GenericProvider
{
Expand All @@ -25,17 +25,6 @@ public function __construct()
]);
}

public function updateToken(AccessTokenInterface $token): ?AccessTokenInterface
{
try {
return $this->getAccessToken('refresh_token', [
'refresh_token' => $token->getRefreshToken(),
]);
} catch (IdentityProviderException) {
return null;
}
}

public function getOAuthProperty(string $property, mixed $data): mixed
{
return data_get($data, str_replace('-', '.', $property)) ?: false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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::table('users', function (Blueprint $table): void {
$table->dropColumn(['access_token', 'refresh_token', 'token_expires']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table): void {
$table->text('access_token')->after('remember_token')->nullable();
$table->text('refresh_token')->after('access_token')->nullable();
$table->unsignedBigInteger('token_expires')->after('refresh_token')->nullable();
});
}
};
37 changes: 0 additions & 37 deletions tests/Unit/UserTest.php

This file was deleted.

Loading