2023-12-05 12:52:14 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
2023-12-06 14:20:30 -08:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-12-05 18:22:20 -08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-12-05 14:41:25 -08:00
|
|
|
use Laravel\Passport\Client;
|
2023-12-05 12:52:14 -08:00
|
|
|
use Laravel\Passport\ClientRepository;
|
2023-12-06 14:20:30 -08:00
|
|
|
use Laravel\Passport\TokenRepository;
|
2023-12-05 12:52:14 -08:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class OauthClients extends Component
|
|
|
|
{
|
2023-12-05 15:39:35 -08:00
|
|
|
public $name;
|
|
|
|
public $redirect;
|
|
|
|
public $editClientId;
|
|
|
|
public $editName;
|
|
|
|
public $editRedirect;
|
|
|
|
|
2023-12-05 18:22:20 -08:00
|
|
|
public $authorizationError;
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
protected $clientRepository;
|
2023-12-06 14:20:30 -08:00
|
|
|
protected $tokenRepository;
|
2023-12-05 15:39:35 -08:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->clientRepository = app(ClientRepository::class);
|
2023-12-06 14:20:30 -08:00
|
|
|
$this->tokenRepository = app(TokenRepository::class);
|
2023-12-05 15:39:35 -08:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2023-12-05 12:52:14 -08:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.oauth-clients', [
|
2023-12-05 15:39:35 -08:00
|
|
|
'clients' => $this->clientRepository->activeForUser(auth()->user()->id),
|
2023-12-06 14:20:30 -08:00
|
|
|
'authorized_tokens' => $this->tokenRepository->forUser(auth()->user()->id)->where('revoked', false),
|
2023-12-05 12:52:14 -08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
public function createClient(): void
|
2023-12-05 12:52:14 -08:00
|
|
|
{
|
2023-12-05 15:39:35 -08:00
|
|
|
$this->validate([
|
2023-12-05 12:52:14 -08:00
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'redirect' => 'required|url|max:255',
|
2023-12-05 15:39:35 -08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$newClient = $this->clientRepository->create(
|
|
|
|
auth()->user()->id,
|
|
|
|
$this->name,
|
|
|
|
$this->redirect,
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->dispatchBrowserEvent('clientCreated');
|
2023-12-05 12:52:14 -08:00
|
|
|
}
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
public function deleteClient(Client $clientId): void
|
2023-12-05 12:52:14 -08:00
|
|
|
{
|
2023-12-05 15:39:35 -08:00
|
|
|
// test for safety
|
|
|
|
// ->delete must be of type Client - thus the model binding
|
2023-12-05 18:36:59 -08:00
|
|
|
if ($clientId->user_id == auth()->user()->id) {
|
|
|
|
$this->clientRepository->delete($clientId);
|
|
|
|
} else {
|
|
|
|
Log::warning('User ' . auth()->user()->id . ' attempted to delete client ' . $clientId->id . ' which belongs to user ' . $clientId->user_id);
|
|
|
|
$this->authorizationError = 'You are not authorized to delete this client.';
|
|
|
|
}
|
2023-12-05 15:39:35 -08:00
|
|
|
}
|
2023-12-05 12:52:14 -08:00
|
|
|
|
2023-12-06 14:20:30 -08:00
|
|
|
public function deleteToken($tokenId): void
|
|
|
|
{
|
|
|
|
$token = $this->tokenRepository->find($tokenId);
|
|
|
|
if ($token->user_id == auth()->user()->id) {
|
|
|
|
$this->tokenRepository->revokeAccessToken($tokenId);
|
|
|
|
} else {
|
|
|
|
Log::warning('User ' . auth()->user()->id . ' attempted to delete token ' . $tokenId . ' which belongs to user ' . $token->user_id);
|
|
|
|
$this->authorizationError = 'You are not authorized to delete this token.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
public function editClient(Client $editClientId): void
|
|
|
|
{
|
|
|
|
$this->editName = $editClientId->name;
|
|
|
|
$this->editRedirect = $editClientId->redirect;
|
2023-12-05 12:52:14 -08:00
|
|
|
|
2023-12-05 18:22:20 -08:00
|
|
|
$this->editClientId = $editClientId->id;
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
$this->dispatchBrowserEvent('editClient');
|
2023-12-05 12:52:14 -08:00
|
|
|
}
|
|
|
|
|
2023-12-05 15:39:35 -08:00
|
|
|
public function updateClient(Client $editClientId): void
|
2023-12-05 12:52:14 -08:00
|
|
|
{
|
2023-12-05 15:39:35 -08:00
|
|
|
$this->validate([
|
|
|
|
'editName' => 'required|string|max:255',
|
|
|
|
'editRedirect' => 'required|url|max:255',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$client = $this->clientRepository->find($editClientId->id);
|
|
|
|
if ($client->user_id == auth()->user()->id) {
|
|
|
|
$client->name = $this->editName;
|
|
|
|
$client->redirect = $this->editRedirect;
|
|
|
|
$client->save();
|
|
|
|
} else {
|
2023-12-05 18:22:20 -08:00
|
|
|
Log::warning('User ' . auth()->user()->id . ' attempted to edit client ' . $editClientId->id . ' which belongs to user ' . $client->user_id);
|
|
|
|
$this->authorizationError = 'You are not authorized to edit this client.';
|
2023-12-05 15:39:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->dispatchBrowserEvent('clientUpdated');
|
|
|
|
|
2023-12-05 12:52:14 -08:00
|
|
|
}
|
|
|
|
}
|