Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

refactor(social): single source of truth for token redaction

Paulo Castellanocf80e1fcaec9
diff --git a/app/Exceptions/Social/SocialPublishException.php b/app/Exceptions/Social/SocialPublishException.php
index 384695e6b..8d0077289 100644
--- a/app/Exceptions/Social/SocialPublishException.php
+++ b/app/Exceptions/Social/SocialPublishException.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Exceptions\Social;
+use App\Services\Social\TokenRedactor;
use RuntimeException;
abstract class SocialPublishException extends RuntimeException
@@ -27,32 +28,10 @@ abstract class SocialPublishException extends RuntimeException
'category' => $this->category->value,
'platform_error_code' => $this->platformErrorCode,
'user_message' => $this->userMessage,
- 'raw_response' => $this->redactTokens($this->rawResponse),
+ 'raw_response' => $this->rawResponse !== null ? TokenRedactor::redact($this->rawResponse) : null,
];
}
- private function redactTokens(?string $text): ?string
- {
- if ($text === null) {
- return null;
- }
-
- // Redact common token patterns from API error responses
- return preg_replace(
- [
- '/access_token=([^&"\s]+)/',
- '/"access_token"\s*:\s*"([^"]+)"/',
- '/Bearer\s+\S+/',
- ],
- [
- 'access_token=[REDACTED]',
- '"access_token":"[REDACTED]"',
- 'Bearer [REDACTED]',
- ],
- $text
- );
- }
-
abstract public static function fromApiResponse(mixed $response): static;
abstract public function platform(): string;
diff --git a/app/Services/Social/Concerns/HasSocialHttpClient.php b/app/Services/Social/Concerns/HasSocialHttpClient.php
index 1aa41c7c2..092ca717c 100644
--- a/app/Services/Social/Concerns/HasSocialHttpClient.php
+++ b/app/Services/Social/Concerns/HasSocialHttpClient.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Services\Social\Concerns;
use App\Models\PostPlatform;
+use App\Services\Social\TokenRedactor;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
@@ -38,20 +39,6 @@ trait HasSocialHttpClient
protected function redactResponseBody(string $body): string
{
- return preg_replace(
- [
- '/access_token=([^&"\s]+)/',
- '/"access_token"\s*:\s*"([^"]+)"/',
- '/Bearer\s+\S+/',
- '/"token"\s*:\s*"([^"]+)"/',
- ],
- [
- 'access_token=[REDACTED]',
- '"access_token":"[REDACTED]"',
- 'Bearer [REDACTED]',
- '"token":"[REDACTED]"',
- ],
- $body
- );
+ return TokenRedactor::redact($body);
}
}
diff --git a/app/Services/Social/TokenRedactor.php b/app/Services/Social/TokenRedactor.php
new file mode 100644
index 000000000..bc1540480
--- /dev/null
+++ b/app/Services/Social/TokenRedactor.php
@@ -0,0 +1,33 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services\Social;
+
+/**
+ * Strips OAuth tokens from raw HTTP bodies before they hit logs or
+ * exception messages. Centralizes the regex patterns so they evolve in
+ * one place — adding a new token format (e.g. provider-specific) means
+ * extending this list, not hunting through the codebase.
+ */
+class TokenRedactor
+{
+ public static function redact(string $body): string
+ {
+ return preg_replace(
+ [
+ '/access_token=([^&"\s]+)/',
+ '/"access_token"\s*:\s*"([^"]+)"/',
+ '/Bearer\s+\S+/',
+ '/"token"\s*:\s*"([^"]+)"/',
+ ],
+ [
+ 'access_token=[REDACTED]',
+ '"access_token":"[REDACTED]"',
+ 'Bearer [REDACTED]',
+ '"token":"[REDACTED]"',
+ ],
+ $body
+ );
+ }
+}
diff --git a/app/Services/Social/TokenRefreshClient.php b/app/Services/Social/TokenRefreshClient.php
index 861389f7b..70b023e2a 100644
--- a/app/Services/Social/TokenRefreshClient.php
+++ b/app/Services/Social/TokenRefreshClient.php
@@ -58,7 +58,7 @@ class TokenRefreshClient
if ($response->failed()) {
Log::error("TokenRefreshClient: {$name} token refresh failed", [
- 'body' => $this->redactBody($response->body()),
+ 'body' => TokenRedactor::redact($response->body()),
]);
$body = $response->json();
@@ -71,23 +71,4 @@ class TokenRefreshClient
return $response;
}
-
- private function redactBody(string $body): string
- {
- return preg_replace(
- [
- '/access_token=([^&"\s]+)/',
- '/"access_token"\s*:\s*"([^"]+)"/',
- '/Bearer\s+\S+/',
- '/"token"\s*:\s*"([^"]+)"/',
- ],
- [
- 'access_token=[REDACTED]',
- '"access_token":"[REDACTED]"',
- 'Bearer [REDACTED]',
- '"token":"[REDACTED]"',
- ],
- $body
- );
- }
}