2025-07-02 21:32:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Enums\PlatformEnum;
|
|
|
|
|
use App\Exceptions\PlatformAuthException;
|
2025-07-05 01:55:53 +02:00
|
|
|
use App\Models\PlatformAccount;
|
2025-07-02 21:32:37 +02:00
|
|
|
use App\Modules\Lemmy\Services\LemmyApiService;
|
2025-08-09 00:03:45 +02:00
|
|
|
use Exception;
|
2025-07-02 21:32:37 +02:00
|
|
|
|
|
|
|
|
class LemmyAuthService
|
|
|
|
|
{
|
2025-07-05 01:55:53 +02:00
|
|
|
/**
|
|
|
|
|
* @throws PlatformAuthException
|
|
|
|
|
*/
|
2025-08-10 15:20:28 +02:00
|
|
|
public function getToken(PlatformAccount $account): string
|
2026-02-26 01:19:38 +01:00
|
|
|
{
|
|
|
|
|
// Use cached token if available
|
|
|
|
|
$cachedToken = $account->settings['api_token'] ?? null;
|
|
|
|
|
if ($cachedToken) {
|
|
|
|
|
return $cachedToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->refreshToken($account);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear cached token and re-authenticate.
|
|
|
|
|
*
|
|
|
|
|
* @throws PlatformAuthException
|
|
|
|
|
*/
|
|
|
|
|
public function refreshToken(PlatformAccount $account): string
|
2025-07-02 21:32:37 +02:00
|
|
|
{
|
2025-07-05 01:55:53 +02:00
|
|
|
if (! $account->username || ! $account->password || ! $account->instance_url) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials for account: ' . $account->username);
|
2025-07-02 21:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-05 01:55:53 +02:00
|
|
|
$api = new LemmyApiService($account->instance_url);
|
|
|
|
|
$token = $api->login($account->username, $account->password);
|
|
|
|
|
|
2025-07-02 21:32:37 +02:00
|
|
|
if (!$token) {
|
2025-07-05 01:55:53 +02:00
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed for account: ' . $account->username);
|
2025-07-02 21:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 01:19:38 +01:00
|
|
|
// Cache the token for future use
|
|
|
|
|
$settings = $account->settings ?? [];
|
|
|
|
|
$settings['api_token'] = $token;
|
|
|
|
|
$account->update(['settings' => $settings]);
|
|
|
|
|
|
2025-07-02 21:32:37 +02:00
|
|
|
return $token;
|
|
|
|
|
}
|
2025-08-09 00:03:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authenticate with Lemmy API and return user data with JWT
|
|
|
|
|
* @throws PlatformAuthException
|
|
|
|
|
*/
|
|
|
|
|
public function authenticate(string $instanceUrl, string $username, string $password): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$api = new LemmyApiService($instanceUrl);
|
|
|
|
|
$token = $api->login($username, $password);
|
|
|
|
|
|
|
|
|
|
if (!$token) {
|
2025-08-09 00:52:14 +02:00
|
|
|
// Throw a clean exception that will be caught and handled by the controller
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Invalid credentials');
|
2025-08-09 00:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get user info with the token
|
|
|
|
|
// For now, we'll return a basic response structure
|
|
|
|
|
// In a real implementation, you might want to fetch user details
|
|
|
|
|
return [
|
|
|
|
|
'jwt' => $token,
|
|
|
|
|
'person_view' => [
|
|
|
|
|
'person' => [
|
|
|
|
|
'id' => 0, // Would need API call to get actual user info
|
|
|
|
|
'display_name' => null,
|
|
|
|
|
'bio' => null,
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
2025-08-09 00:52:14 +02:00
|
|
|
} catch (PlatformAuthException $e) {
|
|
|
|
|
// Re-throw PlatformAuthExceptions as-is to avoid nesting
|
|
|
|
|
throw $e;
|
2025-08-09 00:03:45 +02:00
|
|
|
} catch (Exception $e) {
|
2025-08-09 13:48:25 +02:00
|
|
|
// Check if it's a rate limit error
|
|
|
|
|
if (str_contains($e->getMessage(), 'Rate limited by')) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, $e->getMessage());
|
|
|
|
|
}
|
2026-02-25 20:36:34 +01:00
|
|
|
// Check if it's a connection failure
|
|
|
|
|
if (str_contains($e->getMessage(), 'Connection failed')) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed');
|
|
|
|
|
}
|
2025-08-09 00:52:14 +02:00
|
|
|
// For other exceptions, throw a clean PlatformAuthException
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed');
|
2025-08-09 00:03:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-05 01:55:53 +02:00
|
|
|
}
|