A free open source IT asset/license management system
A bit more polish
snipefe013b5ea0f4
diff --git a/app/Livewire/OauthClients.php b/app/Livewire/OauthClients.phpindex 633d27a432..cdbfe00828 100644--- a/app/Livewire/OauthClients.php+++ b/app/Livewire/OauthClients.php@@ -6,7 +6,6 @@ use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Log;use Laravel\Passport\Client;use Laravel\Passport\ClientRepository;-use Laravel\Passport\TokenRepository;use Livewire\Component;class OauthClients extends Component@@ -44,28 +43,57 @@ class OauthClients extends Componentpublic function render(){- return view('livewire.oauth-clients', [- 'clients' => app(ClientRepository::class)->activeForUser(auth()->id()),- 'authorized_tokens' => app(TokenRepository::class)->forUser(auth()->id())->where('revoked', false),- 'authorizedApplications' => DB::table('oauth_access_tokens as tokens')- ->join('oauth_clients as clients', 'tokens.client_id', '=', 'clients.id')- ->leftJoin('users as creators', 'clients.user_id', '=', 'creators.id')+ $clients = collect();+ if ($this->showOauthClients()) {+ $clients = app(ClientRepository::class)->activeForUser(auth()->id());++ if ($clients->isNotEmpty()) {+ $tokenCountsByClientId = DB::table('oauth_access_tokens')+ ->whereIn('client_id', $clients->pluck('id')->all())+ ->selectRaw('client_id, COUNT(*) as token_count')+ ->groupBy('client_id')+ ->pluck('token_count', 'client_id');++ $clients->each(function ($client) use ($tokenCountsByClientId): void {+ $client->setAttribute('associated_token_count', (int) ($tokenCountsByClientId[$client->id] ?? 0));+ });+ }+ }++ $authorizedApplications = collect();+ if ($this->showAuthorizedApplications()) {+ $authorizedTokenSummary = DB::table('oauth_access_tokens as tokens')->where('tokens.user_id', auth()->id())->where('tokens.revoked', false)+ ->selectRaw('tokens.client_id')+ ->selectRaw('MAX(tokens.scopes) as scopes')+ ->selectRaw('MAX(tokens.created_at) as created_at')+ ->selectRaw('MAX(tokens.expires_at) as expires_at')+ ->groupBy('tokens.client_id');++ $authorizedApplications = DB::table('oauth_clients as clients')+ ->joinSub($authorizedTokenSummary, 'token_summary', function ($join) {+ $join->on('clients.id', '=', 'token_summary.client_id');+ })+ ->leftJoin('users as creators', 'clients.user_id', '=', 'creators.id')->select([- 'tokens.id as token_id',- 'tokens.name as token_name',- 'tokens.scopes',- 'tokens.created_at',- 'tokens.expires_at',+ 'clients.id as client_id','clients.name as client_name','clients.user_id as client_owner_id','creators.display_name as client_owner_display_name','creators.username as client_owner_username','creators.deleted_at as client_owner_deleted_at',+ 'token_summary.scopes',+ 'token_summary.created_at',+ 'token_summary.expires_at',])- ->orderByDesc('tokens.created_at')- ->get(),+ ->orderByDesc('token_summary.created_at')+ ->get();+ }++ return view('livewire.oauth-clients', [+ 'clients' => $clients,+ 'authorizedApplications' => $authorizedApplications,]);}@@ -90,7 +118,7 @@ class OauthClients extends Component{// test for safety// ->delete must be of type Client - thus the model binding- if ((auth()->id()->isSuperUser()) || ($clientId->user_id == auth()->id())) {+ if ((auth()->user()?->isSuperUser()) || ($clientId->user_id == auth()->id())) {app(ClientRepository::class)->delete($clientId);session()->flash('success', trans('admin/settings/message.oauth.client_deleted'));} else {@@ -99,14 +127,18 @@ class OauthClients extends Component}}- public function deleteToken($tokenId): void+ public function deleteAuthorizedApplication(int $clientId): void{- $token = app(TokenRepository::class)->find($tokenId);- if ($token->created_by == auth()->id()) {- app(TokenRepository::class)->revokeAccessToken($tokenId);+ $revokedTokenCount = DB::table('oauth_access_tokens')+ ->where('client_id', $clientId)+ ->where('user_id', auth()->id())+ ->where('revoked', false)+ ->update(['revoked' => true]);++ if ($revokedTokenCount > 0) {session()->flash('success', trans('admin/settings/message.oauth.token_deleted'));} else {- Log::warning('User '.auth()->id().' attempted to delete token '.$tokenId.' which belongs to user '.$token->created_by);+ Log::warning('User '.auth()->id().' attempted to revoke authorized application client '.$clientId.' without matching active tokens.');$this->authorizationError = trans('admin/settings/message.oauth.token_delete_denied');}}diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.phpindex 6223c1b646..80d725ce13 100644--- a/resources/lang/en-US/admin/settings/general.php+++ b/resources/lang/en-US/admin/settings/general.php@@ -332,6 +332,7 @@ return ['oauth_redirect_url' => 'Redirect URL','oauth_name_help' => ' Something your users will recognize and trust.','oauth_scopes' => 'Scopes',+ 'oauth_associated_token_count' => 'Token Count','oauth_callback_url' => 'Your application authorization callback URL.','oauth_personal_access_tokens' => 'User API Tokens (Personal Access Tokens)','oauth_personal_access_tokens_none' => 'No personal access tokens found.',diff --git a/resources/views/livewire/oauth-clients.blade.php b/resources/views/livewire/oauth-clients.blade.phpindex 767251193d..b756575d2f 100644--- a/resources/views/livewire/oauth-clients.blade.php+++ b/resources/views/livewire/oauth-clients.blade.php@@ -32,6 +32,7 @@<th data-field="name" data-sortable="true">{{ trans('general.name') }}</th><th data-field="redirect" data-sortable="true">{{ trans('admin/settings/general.oauth_redirect_url') }}</th><th data-field="secret" data-sortable="true">{{ trans('admin/settings/general.oauth_secret') }}</th>+ <th data-field="associated_token_count" data-sortable="true">{{ trans('admin/settings/general.oauth_associated_token_count') }}</th><th data-field="created_at" data-sortable="true">{{ trans('general.created_at') }}</th><th data-field="updated_at" data-sortable="true">{{ trans('general.updated_at') }}</th><th>@@ -46,6 +47,7 @@<td>{{ $client->name }}</td><td><code>{{ $client->redirect }}</code></td><td><code>{{ $client->secret }}</code></td>+ <td>{{ $client->associated_token_count ?? 0 }}</td><td>{{ $client->created_at ? Helper::getFormattedDateObject($client->created_at, 'datetime', false) : '' }}</td><td>@if ($client->created_at != $client->updated_at)@@ -87,8 +89,7 @@<thead><tr><th data-field="name" data-sortable="true">{{ trans('general.name') }}</th>- <th data-field="client_owner" data-sortable="true">{{ trans('general.user') }}</th>- <th data-field="personal_access_token" data-sortable="true">{{ trans('account/general.personal_access_token') }}</th>+ <th data-field="client_owner" data-sortable="true">{{ trans('general.created_by') }}</th><th data-field="oauth_scopes" data-sortable="true">{{ trans('admin/settings/general.oauth_scopes') }}</th><th data-field="created_at" data-sortable="true">{{ trans('general.created_at') }}</th><th data-field="expires" data-sortable="true">{{ trans('general.expires') }}</th>@@ -118,7 +119,6 @@{{ trans('general.na') }}@endif</td>- <td>{{ $application->token_name }}</td><td>@if(!$application->scopes)<span class="label label-default">{{ trans('admin/settings/general.no_scopes') }}</span>@@ -127,7 +127,7 @@<td>{{ $application->created_at ? Helper::getFormattedDateObject($application->created_at, 'datetime', false) : '' }}</td><td>{{ $application->expires_at ? Helper::getFormattedDateObject($application->expires_at, 'datetime', false) : '' }}</td><td>- <a class="btn btn-sm btn-danger pull-right" wire:click="deleteToken('{{ $application->token_id }}')">+ <a class="btn btn-sm btn-danger pull-right" wire:click="deleteAuthorizedApplication('{{ $application->client_id }}')"><i class="fas fa-trash" aria-hidden="true"></i><span class="sr-only">{{ trans('general.delete') }}</span></a>diff --git a/resources/views/settings/api.blade.php b/resources/views/settings/api.blade.phpindex 60ca3e313b..a4354e2238 100644--- a/resources/views/settings/api.blade.php+++ b/resources/views/settings/api.blade.php@@ -99,12 +99,14 @@@if((int) $token->revoked === 1)<form method="POST" action="{{ route('settings.oauth.tokens.unrevoke', ['token' => $token->id]) }}" style="display: inline;">@csrf- <button type="submit" class="btn btn-sm btn-success">{{ trans('admin/settings/general.oauth_unrevoke') }}</button>+ <button type="submit" class="btn btn-sm btn-default text-success" data-tooltip="true" title="{{ trans('admin/settings/general.oauth_unrevoke') }}">+ <i class="fa-solid fa-toggle-off"></i></button></form>@else<form method="POST" action="{{ route('settings.oauth.tokens.revoke', ['token' => $token->id]) }}" style="display: inline;">@csrf- <button type="submit" class="btn btn-sm btn-danger">{{ trans('admin/settings/general.oauth_revoke') }}</button>+ <button type="submit" class="btn btn-sm btn-default text-danger" data-tooltip="true" title="{{ trans('admin/settings/general.oauth_revoke') }}">+ <i class="fa-solid fa-toggle-on"></i></button></form>@endif</td>diff --git a/tests/Feature/Settings/OauthSettingsTest.php b/tests/Feature/Settings/OauthSettingsTest.phpindex e585f5febb..6e81226474 100644--- a/tests/Feature/Settings/OauthSettingsTest.php+++ b/tests/Feature/Settings/OauthSettingsTest.php@@ -179,6 +179,28 @@ class OauthSettingsTest extends TestCase'updated_at' => now(),'expires_at' => now()->addDay(),],+ [+ 'id' => 'authorized-active-token-2',+ 'user_id' => $superuser->id,+ 'client_id' => $activeClientId,+ 'name' => 'Authorized Active Token Two',+ 'scopes' => '[]',+ 'revoked' => 0,+ 'created_at' => now(),+ 'updated_at' => now(),+ 'expires_at' => now()->addDay(),+ ],+ [+ 'id' => 'authorized-revoked-token',+ 'user_id' => $superuser->id,+ 'client_id' => $activeClientId,+ 'name' => 'Authorized Revoked Token',+ 'scopes' => '[]',+ 'revoked' => 1,+ 'created_at' => now(),+ 'updated_at' => now(),+ 'expires_at' => now()->addDay(),+ ],['id' => 'authorized-deleted-token','user_id' => $superuser->id,@@ -195,14 +217,93 @@ class OauthSettingsTest extends TestCase$this->actingAs($superuser)->get(route('settings.oauth.index'))->assertOk()+ ->assertDontSee(trans('admin/settings/general.oauth_associated_token_count'))->assertSee('Active Client Creator')->assertSee(route('users.show', $activeCreator))->assertSee('<del>Deleted Client Creator</del>', false)->assertSee(route('users.show', $softDeletedCreator))+ ->assertDontSee('Authorized Active Token')+ ->assertDontSee('Authorized Active Token Two')+ ->assertDontSee('Authorized Deleted Token')+ ->assertDontSee('Authorized Revoked Token')->assertSee('data-field="client_owner"', false)->assertSee('data-sortable="true"', false);}+ public function test_oauth_clients_table_shows_associated_token_count_per_client_id(): void+ {+ $superuser = User::factory()->superuser()->create();++ $ownedClientId = DB::table('oauth_clients')->insertGetId([+ 'user_id' => $superuser->id,+ 'name' => 'Owned OAuth Client',+ 'secret' => 'secret',+ 'redirect' => 'http://localhost/callback',+ 'personal_access_client' => 0,+ 'password_client' => 0,+ 'revoked' => 0,+ 'created_at' => now(),+ 'updated_at' => now(),+ ]);++ $otherClientId = DB::table('oauth_clients')->insertGetId([+ 'user_id' => User::factory()->create()->id,+ 'name' => 'Other OAuth Client',+ 'secret' => 'secret',+ 'redirect' => 'http://localhost/callback',+ 'personal_access_client' => 0,+ 'password_client' => 0,+ 'revoked' => 0,+ 'created_at' => now(),+ 'updated_at' => now(),+ ]);++ DB::table('oauth_access_tokens')->insert([+ [+ 'id' => 'owned-client-token-1',+ 'user_id' => $superuser->id,+ 'client_id' => $ownedClientId,+ 'name' => 'Owned Client Token One',+ 'scopes' => '[]',+ 'revoked' => 0,+ 'created_at' => now(),+ 'updated_at' => now(),+ 'expires_at' => now()->addDay(),+ ],+ [+ 'id' => 'owned-client-token-2',+ 'user_id' => $superuser->id,+ 'client_id' => $ownedClientId,+ 'name' => 'Owned Client Token Two',+ 'scopes' => '[]',+ 'revoked' => 1,+ 'created_at' => now(),+ 'updated_at' => now(),+ 'expires_at' => now()->addDay(),+ ],+ [+ 'id' => 'other-client-token-1',+ 'user_id' => $superuser->id,+ 'client_id' => $otherClientId,+ 'name' => 'Other Client Token One',+ 'scopes' => '[]',+ 'revoked' => 0,+ 'created_at' => now(),+ 'updated_at' => now(),+ 'expires_at' => now()->addDay(),+ ],+ ]);++ $this->actingAs($superuser)+ ->get(route('settings.oauth.index'))+ ->assertOk()+ ->assertSee(trans('admin/settings/general.oauth_associated_token_count'))+ ->assertSee('Owned OAuth Client')+ ->assertSee('data-field="associated_token_count"', false)+ ->assertSeeInOrder(['Owned OAuth Client', '<td>2</td>'], false)+ ->assertDontSee('Other Client Token One');+ }+public function test_superuser_can_revoke_and_unrevoke_personal_access_token(): void{$superuser = User::factory()->superuser()->create();