lolispace-applications/app/Services/SynapseService.php

77 lines
2.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Settings;
use Illuminate\Support\Facades\Http;
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
{
$response = Http::withHeaders([
'Authorization' => "Bearer $this->AUTH_TOKEN",
'Content-Type' => 'application/json'
])->post("$this->SYNAPSE_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;
}
/**
* 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();
}
}