2025-07-02 21:32:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Enums\PlatformEnum;
|
|
|
|
|
use App\Exceptions\PlatformAuthException;
|
|
|
|
|
use App\Modules\Lemmy\Services\LemmyApiService;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
|
|
class LemmyAuthService
|
|
|
|
|
{
|
|
|
|
|
public static function getToken(): string
|
|
|
|
|
{
|
|
|
|
|
$cachedToken = Cache::get('lemmy_jwt_token');
|
|
|
|
|
|
|
|
|
|
if ($cachedToken) {
|
|
|
|
|
return $cachedToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$username = config('lemmy.username');
|
|
|
|
|
$password = config('lemmy.password');
|
|
|
|
|
$instance = config('lemmy.instance');
|
|
|
|
|
|
|
|
|
|
if (!$username || !$password || !$instance) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials or instance');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$api = new LemmyApiService($instance);
|
|
|
|
|
$token = $api->login($username, $password);
|
|
|
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 21:34:39 +02:00
|
|
|
// Cache for 50 minutes (3000 seconds) to allow buffer before token expires
|
|
|
|
|
Cache::put('lemmy_jwt_token', $token, config('lemmy.token_ttl', 3000));
|
2025-07-02 21:32:37 +02:00
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
}
|