diff --git a/app/Livewire/RegistrationToken.php b/app/Livewire/RegistrationToken.php new file mode 100644 index 0000000..05bc145 --- /dev/null +++ b/app/Livewire/RegistrationToken.php @@ -0,0 +1,58 @@ +synapseService = $synapseService; + } + + public function mount() + { + $this->fetchTokens(); + } + + public function fetchTokens() + { + $this->tokens = $this->synapseService->fetchTokens(); + } + + public function revokeToken(string $token) + { + if ($this->synapseService->revokeToken($token)) { + $this->fetchTokens(); + } + } + + public function createToken() + { + $this->registrationToken = $this->synapseService->createRegistrationToken(); + + // Open modal + $this->modal('registration-token')->show(); + } + + public function resetToken() + { + $this->registrationToken = null; + $this->fetchTokens(); + } + + public function render() + { + return view('livewire.registration-token'); + } +} diff --git a/app/Services/SynapseService.php b/app/Services/SynapseService.php index c15bdf7..a1c042d 100644 --- a/app/Services/SynapseService.php +++ b/app/Services/SynapseService.php @@ -9,15 +9,24 @@ use Illuminate\Support\Facades\Log; class SynapseService { + protected string $AUTH_TOKEN; + protected string $SYNAPSE_ENDPOINT; + + public function __construct() + { + $this->AUTH_TOKEN = Settings::get('synapse_access_token', ''); + $this->SYNAPSE_ENDPOINT = Settings::get('synapse_endpoint', ''); + } + + /** + * Create registration token + */ public function createRegistrationToken(): ?string { - $token = Settings::get('synapse_access_token', ''); - $endpoint = Settings::get('synapse_endpoint', ''); - $response = Http::withHeaders([ - 'Authorization' => "Bearer $token", + 'Authorization' => "Bearer $this->AUTH_TOKEN", 'Content-Type' => 'application/json' - ])->post($endpoint . '/_synapse/admin/v1/registration_tokens/new', [ + ])->post("$this->SYNAPSE_ENDPOINT/_synapse/admin/v1/registration_tokens/new", [ 'uses_allowed' => 1, 'length' => 12, 'expiry_time' => now()->addWeek()->valueOf(), @@ -34,4 +43,34 @@ class SynapseService return null; } + + /** + * Fetch all active registration tokens from Synapse + */ + public function fetchTokens(): ?array + { + $response = Http::withHeaders([ + 'Authorization' => "Bearer $this->AUTH_TOKEN", + 'Content-Type' => 'application/json' + ])->get("$this->SYNAPSE_ENDPOINT/_synapse/admin/v1/registration_tokens?valid=true"); + + if ($response->successful()) { + return $response->json()['registration_tokens'] ?? []; + } + + return null; + } + + /** + * Revoke registration token + */ + public function revokeToken(string $token): ?bool + { + $response = Http::withHeaders([ + 'Authorization' => "Bearer $this->AUTH_TOKEN", + 'Content-Type' => 'application/json' + ])->delete("$this->SYNAPSE_ENDPOINT/_synapse/admin/v1/registration_tokens/$token"); + + return $response->successful(); + } } diff --git a/resources/views/components/layouts/app/sidebar.blade.php b/resources/views/components/layouts/app/sidebar.blade.php index 4583fb8..79f4ef6 100644 --- a/resources/views/components/layouts/app/sidebar.blade.php +++ b/resources/views/components/layouts/app/sidebar.blade.php @@ -10,6 +10,7 @@ {{ __('Applications') }} + {{ __('Tokens') }} {{ __('Synapse') }} diff --git a/resources/views/livewire/registration-token.blade.php b/resources/views/livewire/registration-token.blade.php new file mode 100644 index 0000000..a60184d --- /dev/null +++ b/resources/views/livewire/registration-token.blade.php @@ -0,0 +1,66 @@ +
+ +
+
+ {{ __('Registration Tokens') }} + {{ __('Manage matrix registration tokens') }} + + +
+ +
+ Create token +
+ + +
+
+ Registration token +
+ + +
+
+ +
+ + + + + + + + + + + + + @forelse ($tokens as $token) + + + + + + + + + @empty + + + + @endforelse + +
Uses allowedPendingCompletedExpiry timeActions
+ {{ $token['uses_allowed'] }} + {{ $token['pending'] }}{{ $token['completed'] }} + {{ $token['expiry_time'] ? Carbon\Carbon::createFromTimestampMs($token['expiry_time'])->toDateTimeString() : 'Never' }} + + + Revoke + +
No tokens found.
+
+
+
diff --git a/routes/web.php b/routes/web.php index d456f0c..0ce25de 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ middleware(['auth', 'verified', App\Http\Middleware\IsAdmin::class]) ->name('dashboard'); -Route::middleware(['auth'])->group(function () { +Route::middleware(['auth', App\Http\Middleware\IsAdmin::class])->group(function () { Route::redirect('settings', 'settings/profile'); Route::get('settings/profile', Profile::class)->name('settings.profile'); Route::get('settings/password', Password::class)->name('settings.password'); Route::get('settings/appearance', Appearance::class)->name('settings.appearance'); Route::get('settings/synapse', Synapse::class)->name('settings.synapse'); + Route::get('registration-tokens', RegistrationToken::class)->name('registration.tokens'); }); require __DIR__ . '/auth.php';