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.

Validate the Bluesky session outside its DTO (#442)

Paulo Castellano10cacbfff7ab
diff --git a/app/Actions/SocialAccount/ReadBlueskySession.php b/app/Actions/SocialAccount/ReadBlueskySession.php
new file mode 100644
index 000000000..275639e7c
--- /dev/null
+++ b/app/Actions/SocialAccount/ReadBlueskySession.php
@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Actions\SocialAccount;
+
+use App\Dto\BlueskySession;
+use App\Http\Requests\App\Bluesky\ReadBlueskySessionRequest;
+use Illuminate\Http\Client\Response;
+use Illuminate\Support\Facades\Validator;
+
+class ReadBlueskySession
+{
+ /**
+ * The session in a Bluesky `createSession` or `getSession` response,
+ * validated against ReadBlueskySessionRequest. Null when the call failed
+ * or the body does not hold a valid session.
+ */
+ public static function execute(Response $response): ?BlueskySession
+ {
+ $data = $response->json();
+
+ if (! $response->successful() || ! is_array($data)) {
+ return null;
+ }
+
+ $validator = Validator::make($data, (new ReadBlueskySessionRequest)->rules());
+
+ if ($validator->fails()) {
+ return null;
+ }
+
+ $session = $validator->validated();
+
+ return new BlueskySession(
+ did: data_get($session, 'did'),
+ handle: data_get($session, 'handle'),
+ accessToken: data_get($session, 'accessJwt'),
+ refreshToken: data_get($session, 'refreshJwt'),
+ emailConfirmed: data_get($session, 'emailConfirmed'),
+ );
+ }
+}
diff --git a/app/Dto/BlueskySession.php b/app/Dto/BlueskySession.php
index 56b9037ce..bd44615ab 100644
--- a/app/Dto/BlueskySession.php
+++ b/app/Dto/BlueskySession.php
@@ -4,12 +4,9 @@ declare(strict_types=1);
namespace App\Dto;
-use Illuminate\Http\Client\Response;
-use Illuminate\Support\Facades\Validator;
-
final readonly class BlueskySession
{
- private function __construct(
+ public function __construct(
public string $did,
public string $handle,
public ?string $accessToken,
@@ -17,42 +14,6 @@ final readonly class BlueskySession
public ?bool $emailConfirmed,
) {}
- public static function fromResponse(Response $response): ?self
- {
- $data = $response->json();
-
- if (! $response->successful() || ! is_array($data)) {
- return null;
- }
-
- $emailConfirmed = data_get($data, 'emailConfirmed');
-
- if ($emailConfirmed !== null && ! is_bool($emailConfirmed)) {
- return null;
- }
-
- $validator = Validator::make($data, [
- 'did' => ['required', 'string'],
- 'handle' => ['required', 'string'],
- 'accessJwt' => ['sometimes', 'required', 'string'],
- 'refreshJwt' => ['sometimes', 'required', 'string'],
- ]);
-
- if ($validator->fails()) {
- return null;
- }
-
- $data = $validator->validated();
-
- return new self(
- did: $data['did'],
- handle: $data['handle'],
- accessToken: data_get($data, 'accessJwt'),
- refreshToken: data_get($data, 'refreshJwt'),
- emailConfirmed: $emailConfirmed,
- );
- }
-
public function hasTokens(): bool
{
return $this->accessToken !== null && $this->refreshToken !== null;
diff --git a/app/Http/Controllers/Auth/BlueskyController.php b/app/Http/Controllers/Auth/BlueskyController.php
index b74d77225..11666dd07 100644
--- a/app/Http/Controllers/Auth/BlueskyController.php
+++ b/app/Http/Controllers/Auth/BlueskyController.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace App\Http\Controllers\Auth;
-use App\Dto\BlueskySession;
+use App\Actions\SocialAccount\ReadBlueskySession;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use App\Exceptions\SocialAccount\ConnectFlowException;
use App\Models\SocialAccount;
@@ -74,7 +74,7 @@ class BlueskyController extends SocialController
throw ValidationException::withMessages(['password' => __('accounts.bluesky.invalid_credentials')]);
}
- $session = BlueskySession::fromResponse($response);
+ $session = ReadBlueskySession::execute($response);
if ($session === null || ! $session->hasTokens()) {
return $this->failConnection('error_connecting');
@@ -135,7 +135,7 @@ class BlueskyController extends SocialController
->timeout(30)
->get(config('trypost.platforms.bluesky.default_service').'/xrpc/'.BlueskyLexicon::GET_SESSION);
- $session = BlueskySession::fromResponse($response);
+ $session = ReadBlueskySession::execute($response);
if ($session === null || $session->did !== data_get($identity, 'platform_user_id')) {
throw new ConnectFlowException('error_connecting', $this->platform);
diff --git a/app/Http/Requests/App/Bluesky/ReadBlueskySessionRequest.php b/app/Http/Requests/App/Bluesky/ReadBlueskySessionRequest.php
new file mode 100644
index 000000000..a2757c82e
--- /dev/null
+++ b/app/Http/Requests/App/Bluesky/ReadBlueskySessionRequest.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Http\Requests\App\Bluesky;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * A Bluesky session as `createSession` or `getSession` answers it. A session
+ * lookup carries no tokens; a value is never cast into its type.
+ */
+class ReadBlueskySessionRequest extends FormRequest
+{
+ public function authorize(): bool
+ {
+ return true;
+ }
+
+ /**
+ * `emailConfirmed` may be missing or null; `required_unless` makes a blank
+ * string fail, since `boolean:strict` alone skips empty values.
+ *
+ * @return array<string, mixed>
+ */
+ public function rules(): array
+ {
+ return [
+ 'did' => ['required', 'string'],
+ 'handle' => ['required', 'string'],
+ 'accessJwt' => ['sometimes', 'required', 'string'],
+ 'refreshJwt' => ['sometimes', 'required', 'string'],
+ 'emailConfirmed' => ['nullable', 'required_unless:emailConfirmed,null', 'boolean:strict'],
+ ];
+ }
+}
diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.php
index 53daeb0e9..3c02ac654 100644
--- a/app/Services/Social/ConnectionVerifier.php
+++ b/app/Services/Social/ConnectionVerifier.php
@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace App\Services\Social;
+use App\Actions\SocialAccount\ReadBlueskySession;
use App\Actions\SocialAccount\SyncXSubscription;
-use App\Dto\BlueskySession;
use App\Enums\SocialAccount\Platform;
use App\Exceptions\PlatformUnavailableException;
use App\Exceptions\Social\BlueskyPublishException;
@@ -372,7 +372,7 @@ class ConnectionVerifier
private function updateBlueskySession(SocialAccount $account, Response $response): void
{
- $session = BlueskySession::fromResponse($response);
+ $session = ReadBlueskySession::execute($response);
if ($session === null || ! $session->hasTokens() || $session->did !== $account->platform_user_id) {
throw new PlatformUnavailableException('Bluesky returned an invalid session response.');
diff --git a/tests/Unit/Dto/BlueskySessionTest.php b/tests/Feature/Actions/SocialAccount/ReadBlueskySessionTest.php
similarity index 91%
rename from tests/Unit/Dto/BlueskySessionTest.php
rename to tests/Feature/Actions/SocialAccount/ReadBlueskySessionTest.php
index 820a94200..7926c48e3 100644
--- a/tests/Unit/Dto/BlueskySessionTest.php
+++ b/tests/Feature/Actions/SocialAccount/ReadBlueskySessionTest.php
@@ -2,6 +2,7 @@
declare(strict_types=1);
+use App\Actions\SocialAccount\ReadBlueskySession;
use App\Dto\BlueskySession;
use Illuminate\Support\Facades\Http;
@@ -14,7 +15,7 @@ test('bluesky session maps the authentication response without coercing values',
'emailConfirmed' => $emailConfirmed,
])])->post('https://bsky.social/xrpc/com.atproto.server.createSession');
- $session = BlueskySession::fromResponse($response);
+ $session = ReadBlueskySession::execute($response);
expect($session)->toBeInstanceOf(BlueskySession::class)
->and($session->did)->toBe('did:plc:account')
@@ -32,7 +33,7 @@ test('bluesky session accepts a session lookup without tokens or email confirmat
'handle' => 'account.bsky.social',
])])->get('https://bsky.social/xrpc/com.atproto.server.getSession');
- $session = BlueskySession::fromResponse($response);
+ $session = ReadBlueskySession::execute($response);
expect($session->accessToken)->toBeNull()
->and($session->refreshToken)->toBeNull()
@@ -53,7 +54,7 @@ test('bluesky session rejects malformed fields instead of casting them', functio
$response = Http::fake(['*' => Http::response($body)])
->post('https://bsky.social/xrpc/com.atproto.server.createSession');
- expect(BlueskySession::fromResponse($response))->toBeNull();
+ expect(ReadBlueskySession::execute($response))->toBeNull();
})->with([
'string true' => ['emailConfirmed', 'true'],
'string false' => ['emailConfirmed', 'false'],
@@ -78,7 +79,7 @@ test('bluesky session rejects incomplete or unsuccessful responses', function (a
$response = Http::fake(['*' => Http::response($body, $status)])
->get('https://bsky.social/xrpc/com.atproto.server.getSession');
- expect(BlueskySession::fromResponse($response))->toBeNull();
+ expect(ReadBlueskySession::execute($response))->toBeNull();
})->with([
'missing identity' => [[], 200],
'missing handle' => [['did' => 'did:plc:account'], 200],
@@ -95,5 +96,5 @@ test('bluesky session does not consider a partial token pair authenticated', fun
'accessJwt' => 'access-token',
])])->post('https://bsky.social/xrpc/com.atproto.server.createSession');
- expect(BlueskySession::fromResponse($response)->hasTokens())->toBeFalse();
+ expect(ReadBlueskySession::execute($response)->hasTokens())->toBeFalse();
});