Same shape of bug as the refreshToken duplication: the regex that strips access_token / Bearer headers from logged HTTP bodies existed in three near-identical copies (HasSocialHttpClient trait, TokenRefreshClient, SocialPublishException), drifting subtly — SocialPublishException was missing the JSON "token" pattern. Extracts a TokenRedactor::redact(string) helper and routes all callers through it. Adding a new token format now means one regex in one file.
74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Social;
|
|
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Exceptions\PlatformUnavailableException;
|
|
use App\Exceptions\TokenExpiredException;
|
|
use Closure;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Client\Response;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Normalizes the failure modes of an OAuth token-refresh request:
|
|
*
|
|
* - ConnectionException (timeout / DNS / refused) → PlatformUnavailableException
|
|
* - HTTP 5xx → PlatformUnavailableException
|
|
* - HTTP 4xx → TokenExpiredException
|
|
*
|
|
* Callers configure the actual HTTP call through the closure passed to
|
|
* `send()`, so platform-specific quirks (form vs JSON body, auth headers,
|
|
* basic auth, etc.) stay where they belong — in the per-platform refresh
|
|
* method — while the failure semantics are uniform across providers.
|
|
*/
|
|
class TokenRefreshClient
|
|
{
|
|
public function __construct(public readonly Platform $platform) {}
|
|
|
|
public static function for(Platform $platform): self
|
|
{
|
|
return new self($platform);
|
|
}
|
|
|
|
/**
|
|
* @param Closure():Response $request
|
|
*
|
|
* @throws PlatformUnavailableException
|
|
* @throws TokenExpiredException
|
|
*/
|
|
public function send(Closure $request): Response
|
|
{
|
|
$name = $this->platform->label();
|
|
|
|
try {
|
|
$response = $request();
|
|
} catch (ConnectionException $e) {
|
|
throw new PlatformUnavailableException("{$name} API unreachable: {$e->getMessage()}");
|
|
}
|
|
|
|
if ($response->serverError() || $response->status() === 429) {
|
|
throw new PlatformUnavailableException(
|
|
"{$name} API returned {$response->status()} during token refresh",
|
|
$response->status(),
|
|
);
|
|
}
|
|
|
|
if ($response->failed()) {
|
|
Log::error("TokenRefreshClient: {$name} token refresh failed", [
|
|
'body' => TokenRedactor::redact($response->body()),
|
|
]);
|
|
|
|
$body = $response->json();
|
|
$message = data_get($body, 'error_description')
|
|
?? data_get($body, 'error.message')
|
|
?? "Failed to refresh {$name} token";
|
|
|
|
throw new TokenExpiredException($message, platformErrorCode: (string) $response->status());
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|