Add registration token
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled

This commit is contained in:
weeb 2025-05-02 00:10:40 +02:00
parent 5a463656a7
commit fd7d2f8068
7 changed files with 106 additions and 29 deletions

View File

@ -1,14 +0,0 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
class HomeController extends Controller
{
public function index(): View
{
return view('index');
}
}

View File

@ -3,6 +3,8 @@
namespace App\Livewire;
use App\Models\Application;
use App\Services\SynapseService;
use Livewire\Component;
use Livewire\WithPagination;
@ -12,6 +14,8 @@ class ApplicationList extends Component
public $hideApproved = true;
public $error = false;
public function updatingHideApproved()
{
$this->resetPage();
@ -19,7 +23,14 @@ class ApplicationList extends Component
public function approveApplication($uuid)
{
Application::where('uuid', $uuid)->update(['status' => 1]);
$service = new SynapseService();
$token = $service->createRegistrationToken();
if (!$token) {
$this->error = true;
return;
}
Application::where('uuid', $uuid)->update(['status' => 1, 'registration_token' => $token]);
}
public function rejectApplication($uuid)

View File

@ -14,6 +14,7 @@ class Application extends Model
protected $fillable = [
'uuid',
'message',
'registration_token',
];
public function uploads()

View File

@ -0,0 +1,35 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SynapseService
{
public function createRegistrationToken(): ?string
{
$token = config('synapse.admin_api_token');
$endpoint = config('synapse.admin_api_url');
$response = Http::withHeaders([
'Authorization' => "Bearer $token",
'Content-Type' => 'application/json'
])->post($endpoint . '/_synapse/admin/v1/registration_tokens/new', [
'uses_allowed' => 1,
'length' => 12,
'expiry_time' => now()->addWeek()->valueOf(),
]);
if ($response->successful()) {
return $response->json()['token'] ?? null;
}
Log::error('Failed to create Synapse registration token', [
'status' => $response->status(),
'body' => $response->body()
]);
return null;
}
}

27
config/synapse.php Normal file
View File

@ -0,0 +1,27 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Admin API Token
|--------------------------------------------------------------------------
|
| This variable contains the authorization bearer for synapse.
|
*/
'admin_api_token' => env('SYNAPSE_ADMIN_API_TOKEN'),
/*
|--------------------------------------------------------------------------
| Admin API Endpoint
|--------------------------------------------------------------------------
|
| This variable contains the admin endpoint for synapse.
|
*/
'admin_api_url' => env('SYNAPSE_ADMIN_API_URL'),
];

View File

@ -16,6 +16,7 @@ return new class extends Migration
$table->uuid('uuid');
$table->tinyInteger('status')->default(0);
$table->string('message');
$table->string('registration_token')->nullable();
$table->timestamps();
});
}

View File

@ -8,7 +8,7 @@
<div class="p-5 mb-4 dark:bg-red-950/50 rounded-lg">
@endif
<flux:heading size="xl">Application Status</flux:heading>
<flux:heading size="xl">Application</flux:heading>
@php
$status = [
0 => 'Pending Review',
@ -21,10 +21,25 @@
<strong>Status:</strong>
{{ $status[$application->status] }}
</p>
<p>
<strong>Created:</strong>
{{ Carbon\Carbon::parse($application->created_at)->diffForHumans(['parts' => 2]) }}
@if ($application->registration_token)
<p class="pt-2 pb-2">
<strong>Registration Token (valid until
{{ $application->updated_at->addWeek()->format('Y-m-d') }})</strong>
</p>
<flux:input class="mb-2" icon="key" value="{{ $application->registration_token }}" readonly
copyable />
To sign up, you should use <flux:link href="https://element.lolispace.moe">Element Web</flux:link> or
Element Desktop, as the authentication token is not compatible
with the mobile clients.
@endif
</flux:text>
</div>
@if (!$application->registration_token)
<div class="p-5 mb-4 dark:bg-pink-950/50 rounded-lg">
<flux:heading size="xl">Application</flux:heading>
<flux:text class="mt-2">
{{ $application->message }}
</flux:text>
</div>
@ -37,5 +52,6 @@
</div>
@endforeach
</div>
@endif
</div>
</div>